22from contextlib import contextmanager
33
44from mypy .types import Type , AnyType , PartialType , UnionType , TypeOfAny
5- from mypy .nodes import (Key , Expression , Var , RefExpr )
6-
75from mypy .subtypes import is_subtype
86from mypy .join import join_simple
97from mypy .sametypes import is_same_type
10-
8+ from mypy .nodes import Expression , Var , RefExpr
9+ from mypy .literals import Key , literal , literal_hash , subkeys
1110from mypy .nodes import IndexExpr , MemberExpr , NameExpr
1211
1312
@@ -61,7 +60,7 @@ class A:
6160
6261 def __init__ (self ) -> None :
6362 # The stack of frames currently used. These map
64- # expr. literal_hash -- literals like 'foo.bar' --
63+ # literal_hash(expr) -- literals like 'foo.bar' --
6564 # to types. The last element of this list is the
6665 # top-most, current frame. Each earlier element
6766 # records the state as of when that frame was last
@@ -75,7 +74,7 @@ def __init__(self) -> None:
7574 # has no corresponding element in this list.
7675 self .options_on_return = [] # type: List[List[Frame]]
7776
78- # Maps expr. literal_hash to get_declaration(expr)
77+ # Maps literal_hash(expr) to get_declaration(expr)
7978 # for every expr stored in the binder
8079 self .declarations = DeclarationsFrame ()
8180 # Set of other keys to invalidate if a key is changed, e.g. x -> {x.a, x[0]}
@@ -94,9 +93,8 @@ def _add_dependencies(self, key: Key, value: Optional[Key] = None) -> None:
9493 value = key
9594 else :
9695 self .dependencies .setdefault (key , set ()).add (value )
97- for elt in key :
98- if isinstance (elt , Key ):
99- self ._add_dependencies (elt , value )
96+ for elt in subkeys (key ):
97+ self ._add_dependencies (elt , value )
10098
10199 def push_frame (self ) -> Frame :
102100 """Push a new frame into the binder."""
@@ -119,12 +117,11 @@ def _get(self, key: Key, index: int=-1) -> Optional[Type]:
119117 def put (self , expr : Expression , typ : Type ) -> None :
120118 if not isinstance (expr , BindableTypes ):
121119 return
122- if not expr . literal :
120+ if not literal ( expr ) :
123121 return
124- key = expr . literal_hash
122+ key = literal_hash ( expr )
125123 assert key is not None , 'Internal error: binder tried to put non-literal'
126124 if key not in self .declarations :
127- assert isinstance (expr , BindableTypes )
128125 self .declarations [key ] = get_declaration (expr )
129126 self ._add_dependencies (key )
130127 self ._put (key , typ )
@@ -133,8 +130,9 @@ def unreachable(self) -> None:
133130 self .frames [- 1 ].unreachable = True
134131
135132 def get (self , expr : Expression ) -> Optional [Type ]:
136- assert expr .literal_hash is not None , 'Internal error: binder tried to get non-literal'
137- return self ._get (expr .literal_hash )
133+ key = literal_hash (expr )
134+ assert key is not None , 'Internal error: binder tried to get non-literal'
135+ return self ._get (key )
138136
139137 def is_unreachable (self ) -> bool :
140138 # TODO: Copy the value of unreachable into new frames to avoid
@@ -143,8 +141,9 @@ def is_unreachable(self) -> bool:
143141
144142 def cleanse (self , expr : Expression ) -> None :
145143 """Remove all references to a Node from the binder."""
146- assert expr .literal_hash is not None , 'Internal error: binder tried cleanse non-literal'
147- self ._cleanse_key (expr .literal_hash )
144+ key = literal_hash (expr )
145+ assert key is not None , 'Internal error: binder tried cleanse non-literal'
146+ self ._cleanse_key (key )
148147
149148 def _cleanse_key (self , key : Key ) -> None :
150149 """Remove all references to a key from the binder."""
@@ -217,7 +216,7 @@ def assign_type(self, expr: Expression,
217216 restrict_any : bool = False ) -> None :
218217 if not isinstance (expr , BindableTypes ):
219218 return None
220- if not expr . literal :
219+ if not literal ( expr ) :
221220 return
222221 self .invalidate_dependencies (expr )
223222
@@ -266,14 +265,15 @@ def invalidate_dependencies(self, expr: BindableExpression) -> None:
266265 It is overly conservative: it invalidates globally, including
267266 in code paths unreachable from here.
268267 """
269- assert expr .literal_hash is not None
270- for dep in self .dependencies .get (expr .literal_hash , set ()):
268+ key = literal_hash (expr )
269+ assert key is not None
270+ for dep in self .dependencies .get (key , set ()):
271271 self ._cleanse_key (dep )
272272
273273 def most_recent_enclosing_type (self , expr : BindableExpression , type : Type ) -> Optional [Type ]:
274274 if isinstance (type , AnyType ):
275275 return get_declaration (expr )
276- key = expr . literal_hash
276+ key = literal_hash ( expr )
277277 assert key is not None
278278 enclosers = ([get_declaration (expr )] +
279279 [f [key ] for f in self .frames
0 commit comments