From f3f79f68689e246f4b85c6b7fbdfb62b7a41ba7c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 15 Dec 2025 22:32:20 -0800 Subject: [PATCH] Optimise LiteralType.__eq__ and __hash__ If I'm doing it right, this should be 2% on a profile I'm looking at --- mypy/types.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mypy/types.py b/mypy/types.py index 207e87984bed..a945ce044136 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -3242,7 +3242,6 @@ def __init__( super().__init__(line, column) self.value = value self.fallback = fallback - self._hash = -1 # Cached hash value # NOTE: Enum types are always truthy by default, but this can be changed # in subclasses, so we need to get the truthyness from the Enum @@ -3272,13 +3271,11 @@ def accept(self, visitor: TypeVisitor[T]) -> T: return visitor.visit_literal_type(self) def __hash__(self) -> int: - if self._hash == -1: - self._hash = hash((self.value, self.fallback)) - return self._hash + return hash(self.value) def __eq__(self, other: object) -> bool: if isinstance(other, LiteralType): - return self.fallback == other.fallback and self.value == other.value + return self.value == other.value and self.fallback == other.fallback else: return NotImplemented