Skip to content

Only concrete class can be decorated with @total_ordering #8539

Closed
@mthuurne

Description

@mthuurne

When I check this code with mypy:

from abc import abstractmethod
from functools import total_ordering

@total_ordering
class PriorityMixin:

    @abstractmethod
    def getName(self) -> str: ...

    @abstractmethod
    def getPriority(self) -> int: ...

    def __hash__(self) -> int:
        return hash(self.getName())

    def __eq__(self, other: object) -> bool:
        if isinstance(other, PriorityMixin):
            return self.getName() == other.getName()
        else:
            return NotImplemented

    def __lt__(self, other: object) -> bool:
        if isinstance(other, PriorityMixin):
            prioCmp = self.getPriority() - other.getPriority()
            if prioCmp == 0:
                return self.getName() < other.getName()
            else:
                return prioCmp < 0
        else:
            return NotImplemented

It reports:

testcase.py:4: error: Only concrete class can be given where "Type[PriorityMixin]" is expected  [misc]
    @total_ordering
     ^

I'm using mypy 0.770 under Python 3.7.3 on openSUSE Linux.

I don't see a reason why an abstract class cannot be decorated with @total_ordering.

In typeshed commit e404e15 the signature of total_ordering was changed to preserve the original type. I think that change makes sense until full total_ordering support is implemented (#4610), but it does trigger the issue described above.

I think this issue may be a variation of #5374, since that describes a similar problem when using @dataclass as an annotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions