Message397278
Consider the following example that attempts to make a series of types sortable by their class name:
from functools import total_ordering
@total_ordering
class SortableMeta(type):
def __new__(cls, name, bases, ns):
return super().__new__(cls, name, bases, ns)
def __lt__(self, other):
if not isinstance(other, SortableMeta):
pass
return self.__name__ < other.__name__
def __eq__(self, other):
if not isinstance(other, SortableMeta):
pass
return self.__name__ == other.__name__
class B(metaclass=SortableMeta):
pass
class A(metaclass=SortableMeta):
pass
print(A < B)
print(A > B)
This should just print "True", "False", but instead the second comparison raises this exception:
Traceback (most recent call last):
File "total_ordering_metaclass.py", line 27, in <module>
print(A > B)
File "lib/python3.9/functools.py", line 91, in _gt_from_lt
op_result = self.__lt__(other)
TypeError: expected 1 argument, got 0
The problem here is that functools considers .__special__ to be equivalent to operator.special. I'm pretty sure this should be invoking "self < other" rather than self.__lt__(other). |
|
Date |
User |
Action |
Args |
2021-07-12 05:21:05 | glyph | set | recipients:
+ glyph |
2021-07-12 05:21:05 | glyph | set | messageid: <1626067265.0.0.995880932731.issue44605@roundup.psfhosted.org> |
2021-07-12 05:21:04 | glyph | link | issue44605 messages |
2021-07-12 05:21:04 | glyph | create | |
|