Skip to content

Commit a940c8f

Browse files
authored
Optimise LiteralType.__eq__ and __hash__ (#20423)
If I'm doing it right, this should be 1-2% on a profile I'm looking at
1 parent 299e6d0 commit a940c8f

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

mypy/types.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,7 +3223,6 @@ def __init__(
32233223
super().__init__(line, column)
32243224
self.value = value
32253225
self.fallback = fallback
3226-
self._hash = -1 # Cached hash value
32273226

32283227
# NOTE: Enum types are always truthy by default, but this can be changed
32293228
# in subclasses, so we need to get the truthyness from the Enum
@@ -3253,13 +3252,12 @@ def accept(self, visitor: TypeVisitor[T]) -> T:
32533252
return visitor.visit_literal_type(self)
32543253

32553254
def __hash__(self) -> int:
3256-
if self._hash == -1:
3257-
self._hash = hash((self.value, self.fallback))
3258-
return self._hash
3255+
# Intentionally a subset of __eq__ for perf
3256+
return hash(self.value)
32593257

32603258
def __eq__(self, other: object) -> bool:
32613259
if isinstance(other, LiteralType):
3262-
return self.fallback == other.fallback and self.value == other.value
3260+
return self.value == other.value and self.fallback == other.fallback
32633261
else:
32643262
return NotImplemented
32653263

0 commit comments

Comments
 (0)