2121
2222import mypy .nodes
2323from mypy .bogus_type import Bogus
24- from mypy .nodes import ARG_POS , ARG_STAR , ARG_STAR2 , INVARIANT , ArgKind , FakeInfo , SymbolNode
24+ from mypy .nodes import ARG_KINDS , ARG_POS , ARG_STAR , ARG_STAR2 , INVARIANT , ArgKind , SymbolNode
2525from mypy .options import Options
2626from mypy .state import state
2727from mypy .util import IdMapper
@@ -538,6 +538,10 @@ def __repr__(self) -> str:
538538 return self .raw_id .__repr__ ()
539539
540540 def __eq__ (self , other : object ) -> bool :
541+ # Although this call is not expensive (like UnionType or TypedDictType),
542+ # most of the time we get the same object here, so add a fast path.
543+ if self is other :
544+ return True
541545 return (
542546 isinstance (other , TypeVarId )
543547 and self .raw_id == other .raw_id
@@ -1780,7 +1784,9 @@ def deserialize(cls, data: JsonDict) -> Parameters:
17801784 assert data [".class" ] == "Parameters"
17811785 return Parameters (
17821786 [deserialize_type (t ) for t in data ["arg_types" ]],
1783- [ArgKind (x ) for x in data ["arg_kinds" ]],
1787+ # This is a micro-optimization until mypyc gets dedicated enum support. Otherwise,
1788+ # we would spend ~20% of types deserialization time in Enum.__call__().
1789+ [ARG_KINDS [x ] for x in data ["arg_kinds" ]],
17841790 data ["arg_names" ],
17851791 variables = [cast (TypeVarLikeType , deserialize_type (v )) for v in data ["variables" ]],
17861792 imprecise_arg_kinds = data ["imprecise_arg_kinds" ],
@@ -1797,7 +1803,7 @@ def __hash__(self) -> int:
17971803 )
17981804
17991805 def __eq__ (self , other : object ) -> bool :
1800- if isinstance (other , ( Parameters , CallableType ) ):
1806+ if isinstance (other , Parameters ):
18011807 return (
18021808 self .arg_types == other .arg_types
18031809 and self .arg_names == other .arg_names
@@ -2210,15 +2216,9 @@ def with_normalized_var_args(self) -> Self:
22102216 )
22112217
22122218 def __hash__ (self ) -> int :
2213- # self.is_type_obj() will fail if self.fallback.type is a FakeInfo
2214- if isinstance (self .fallback .type , FakeInfo ):
2215- is_type_obj = 2
2216- else :
2217- is_type_obj = self .is_type_obj ()
22182219 return hash (
22192220 (
22202221 self .ret_type ,
2221- is_type_obj ,
22222222 self .is_ellipsis_args ,
22232223 self .name ,
22242224 tuple (self .arg_types ),
@@ -2236,7 +2236,6 @@ def __eq__(self, other: object) -> bool:
22362236 and self .arg_names == other .arg_names
22372237 and self .arg_kinds == other .arg_kinds
22382238 and self .name == other .name
2239- and self .is_type_obj () == other .is_type_obj ()
22402239 and self .is_ellipsis_args == other .is_ellipsis_args
22412240 and self .type_guard == other .type_guard
22422241 and self .type_is == other .type_is
@@ -2271,10 +2270,10 @@ def serialize(self) -> JsonDict:
22712270 @classmethod
22722271 def deserialize (cls , data : JsonDict ) -> CallableType :
22732272 assert data [".class" ] == "CallableType"
2274- # TODO: Set definition to the containing SymbolNode?
2273+ # The . definition link is set in fixup.py.
22752274 return CallableType (
22762275 [deserialize_type (t ) for t in data ["arg_types" ]],
2277- [ArgKind ( x ) for x in data ["arg_kinds" ]],
2276+ [ARG_KINDS [ x ] for x in data ["arg_kinds" ]],
22782277 data ["arg_names" ],
22792278 deserialize_type (data ["ret_type" ]),
22802279 Instance .deserialize (data ["fallback" ]),
@@ -2931,6 +2930,8 @@ def __hash__(self) -> int:
29312930 def __eq__ (self , other : object ) -> bool :
29322931 if not isinstance (other , UnionType ):
29332932 return NotImplemented
2933+ if self is other :
2934+ return True
29342935 return frozenset (self .items ) == frozenset (other .items )
29352936
29362937 @overload
0 commit comments