Skip to content

error with __gt__ method accepting literal type #2727

@carljm

Description

@carljm

I have a Money class where I want to support comparisons with literal zero as a shorthand (e.g., money > 0) so I've defined overloads like this:

from typing import Literal, Union, overload

class Money:
    @overload
    def __gt__(self, other: "Money") -> bool: ...

    @overload
    def __gt__(self, other: Literal[0]) -> bool: ...

    def __gt__(self, other: Union["Money", Literal[0]]) -> bool:
        ...

When I try to test it:

positive_money` = Money(100)
assert positive_money > 0

ty reports:

error[unsupported-operator]: Unsupported `>` operation
   --> tests/utils/test_money.py:285:16
    |
283 |         negative_money = Money(-50)
284 |
285 |         assert positive_money > 0
    |                --------------^^^-
    |                |                |
    |                |                Has type `Literal[0]`
    |                Has type `Money`
286 |         assert not (zero_money > 0)
287 |         assert not (negative_money > 0)
    |
info: rule `unsupported-operator` is enabled by default

I've confirmed this pattern works fine with mypy. Is this an explicitly "implementation skipped" behavior?

Thank you for all the work on ty!

Originally posted by @flpStrri in ruff#15383

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions