Skip to content
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

@total_ordering Unsupported left operand type for <= #10334

Closed
belm0 opened this issue Jun 20, 2023 · 3 comments
Closed

@total_ordering Unsupported left operand type for <= #10334

belm0 opened this issue Jun 20, 2023 · 3 comments

Comments

@belm0
Copy link

belm0 commented Jun 20, 2023

Defining __lt__ and __eq__ doesn't work:

@total_ordering
class OrderedEnum(Enum):

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value < other.value
        return NotImplemented

    def __eq__(self, other):
        if self.__class__ is other.__class__:
            return self.value == other.value
        return NotImplemented

class Foo(OrderedEnum):
    A = 10
    B = 20

Foo.A <= Foo.B  # error: Unsupported left operand type for <= ("Foo")  [operator]

Defining __lt__ and __le__ works:

@total_ordering
class OrderedEnum(Enum):

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            return self.value < other.value
        return NotImplemented

    def __le__(self, other):
        if self.__class__ is other.__class__:
            return self.value <= other.value
        return NotImplemented

class Foo(OrderedEnum):
    A = 10
    B = 20

Foo.A <= Foo.B

Defining __lt__ alone also fails. What am I missing?

@srittau
Copy link
Collaborator

srittau commented Jun 20, 2023

@total_ordering can't be implemented in typeshed, type checkers need to special case it. In this case, I suspect this could be a bug in mypy.

@JelleZijlstra
Copy link
Member

Linking python/mypy#3135

@srittau
Copy link
Collaborator

srittau commented Jun 20, 2023

Closing this here as there's nothing to be done for typeshed. I believe an intersection type (python/typing#213) could help here in the future, as it should be able to add attributes/methods to existing types.

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants