Options: use isinstance instead of type for Choice comparison

This commit is contained in:
Fabian Dill 2021-08-30 23:07:19 +02:00
parent fd6e009c4b
commit d859cecffb
1 changed files with 14 additions and 3 deletions

View File

@ -148,17 +148,28 @@ class Choice(Option):
return cls.from_text(str(data))
def __eq__(self, other):
if type(other) == str:
if isinstance(other, str):
assert other in self.options
return other == self.current_key
elif type(other) == int:
elif isinstance(other, int):
assert other in self.name_lookup
return other == self.value
elif type(other) == bool:
elif isinstance(other, bool):
return other == bool(self.value)
else:
raise TypeError(f"Can't compare {self.__class__.__name__} with {other.__class__.__name__}")
def __ne__(self, other):
if isinstance(other, str):
assert other in self.options
return other != self.current_key
elif isinstance(other, int):
assert other in self.name_lookup
return other != self.value
elif isinstance(other, bool):
return other != bool(self.value)
else:
raise TypeError(f"Can't compare {self.__class__.__name__} with {other.__class__.__name__}")
class Range(Option, int):
range_start = 0