build: Fix type signature of rich comparison dunders

There are three problems:

1) Dunders like `__lt__` and `__gt__` don't return bool, they return
   either a bool or the NotImplemented singleton to signal that they don't
   know how to be compared.
2) The don't take type object, the take `typing.Any`
3) They need to return NotImplemented if the comparison is not
   implemented, this allows python to try the inverse dunder from the
   other object. If that object returns NotImplemented as well a
   TypeError is raised.
pull/6301/head
Dylan Baker 5 years ago committed by Jussi Pakkanen
parent c616686d5d
commit e88b3c8022
  1. 16
      mesonbuild/build.py

@ -355,16 +355,24 @@ a hard error in the future.''' % name)
if not hasattr(self, 'typename'):
raise RuntimeError('Target type is not set for target class "{}". This is a bug'.format(type(self).__name__))
def __lt__(self, other: object) -> bool:
def __lt__(self, other: typing.Any) -> typing.Union[bool, 'NotImplemented']:
if not hasattr(other, 'get_id') and not callable(other.get_id):
return NotImplemented
return self.get_id() < other.get_id()
def __le__(self, other: object) -> bool:
def __le__(self, other: typing.Any) -> typing.Union[bool, 'NotImplemented']:
if not hasattr(other, 'get_id') and not callable(other.get_id):
return NotImplemented
return self.get_id() <= other.get_id()
def __gt__(self, other: object) -> bool:
def __gt__(self, other: typing.Any) -> typing.Union[bool, 'NotImplemented']:
if not hasattr(other, 'get_id') and not callable(other.get_id):
return NotImplemented
return self.get_id() > other.get_id()
def __ge__(self, other: object) -> bool:
def __ge__(self, other: typing.Any) -> typing.Union[bool, 'NotImplemented']:
if not hasattr(other, 'get_id') and not callable(other.get_id):
return NotImplemented
return self.get_id() >= other.get_id()
def get_install_dir(self, environment):

Loading…
Cancel
Save