Skip to content

Instantly share code, notes, and snippets.

@konradhalas
Last active February 20, 2018 16:36
Show Gist options
  • Save konradhalas/32146e047b4c4c5dd6f7475d2a7f39ed to your computer and use it in GitHub Desktop.
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.
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')
# mypy --strict money.py
money.py:23: error: Unsupported left operand type for <= ("Money")
  • Python 3.6.4
  • mypy 0.570-dev-3545a71ba11576b08007f70d8407bf7924738886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment