-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected TypeError when using natsorted on a custom class #60
Comments
You can check out the function that initiates the transformation to get an idea where the error is coming from. You can also inspect what >>> import natsort
>>> ns_key = natsort.natsort_ keygen()
>>> print(repr(ns_key(terminal)))
>>> print(repr(ns_key(production))) I suspect that because one class defines an iterator interface and the other does not they are going down different branches of code, resulting in non-comparable types. Having said all this, the approach being used to sort the classes in the above implementation is not the preferred method by the python community - one should instead use a key function (all the modern examples in https://wiki.python.org/moin/HowTo/Sorting show using a key function rather than relying on >>> sorted(trouble_set, key=str) is what should be used. In the implementation above, for each pair of inputs If the code is changed to use a key function, then TL;DRThis will fix it >>> natsort.natsorted(trouble_set, key=str) |
between instances of 'tuple' and 'str', issue: SethMMorton/natsort#60
Sorry, I did not mean to be rude. In my understanding, I only had to override the For the built-in Python Now, I understand why |
There are a few things to address. I want to be very clear that To sort naturally without using a key function, the correct approach would be to insert the import natsort
natsort_key = natsort.natsort_keygen()
class Myclass(object):
def __init__(self, arg):
self.arg = arg
def __eq__(self, other):
return str(self) == str(other)
def __lt__(self, other):
return natsort_key(str(self)) < natsort_key(str(other)) If this approach is taken for both classes, then one can use
Please describe what behavior you would want instead. Keep in mind the following when coming up with a behavior:
While I empathize with having to spend an hour trying to debug the |
First, thanks for your dedication into writing
When I first got the
The first thing which bothered me was the fact that the builtin python
The README page could always use show the use of After understanding how
|
I will update the documentation to make aspects more clear. |
This gives a high-level description of how natsort works, and also some caveats so that users have a better idea of what natsort is doing under the hood. This address and closes issue #60.
Minimum, Complete, Verifiable Example
Using my custom class with python builtin
sorted()
works 100%. But withnatsorted
, Python throws this insane type error. This happens when one of my classes on the list has an iterator and the other don't.Error message, Traceback
This error also comes up if you replace the iterator by the
__getitem__
:The text was updated successfully, but these errors were encountered: