Skip to content

Commit 0467d28

Browse files
committed
Optimise LiteralType.__eq__ and __hash__
If I'm doing it right, this should be 2% on a profile I'm looking at
1 parent eb144ce commit 0467d28

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

mypy/types.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,7 +3242,6 @@ def __init__(
32423242
super().__init__(line, column)
32433243
self.value = value
32443244
self.fallback = fallback
3245-
self._hash = -1 # Cached hash value
32463245

32473246
# NOTE: Enum types are always truthy by default, but this can be changed
32483247
# in subclasses, so we need to get the truthyness from the Enum
@@ -3272,13 +3271,11 @@ def accept(self, visitor: TypeVisitor[T]) -> T:
32723271
return visitor.visit_literal_type(self)
32733272

32743273
def __hash__(self) -> int:
3275-
if self._hash == -1:
3276-
self._hash = hash((self.value, self.fallback))
3277-
return self._hash
3274+
return hash(self.value)
32783275

32793276
def __eq__(self, other: object) -> bool:
32803277
if isinstance(other, LiteralType):
3281-
return self.fallback == other.fallback and self.value == other.value
3278+
return self.value == other.value and type(self.value) is type(other.value)
32823279
else:
32833280
return NotImplemented
32843281

0 commit comments

Comments
 (0)