- Python 3.6.4
- mypy 0.570-dev-3545a71ba11576b08007f70d8407bf7924738886
Last active
February 20, 2018 16:36
-
-
Save konradhalas/32146e047b4c4c5dd6f7475d2a7f39ed to your computer and use it in GitHub Desktop.
Mypy shows the error of an unsupported comparison operator but this class has `@functools.total_ordering`, `==` and `<`, so `<=` is implemented for this type.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import decimal | |
import functools | |
import typing | |
@functools.total_ordering | |
class Money: | |
def __init__(self, amount: str) -> None: | |
self.amount = decimal.Decimal(amount) | |
def __eq__(self, other: typing.Any) -> bool: | |
if isinstance(other, Money): | |
return self.amount == other.amount | |
return NotImplemented | |
def __lt__(self, other: typing.Any) -> bool: | |
if isinstance(other, Money): | |
return self.amount < other.amount | |
return NotImplemented | |
assert Money('10.00') <= Money('10.00') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mypy --strict money.py | |
money.py:23: error: Unsupported left operand type for <= ("Money") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment