Skip to content

Commit 73c44fa

Browse files
committed
assert expr.literal_hash is never None
1 parent 755abd7 commit 73c44fa

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

mypy/binder.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
BindableExpression = Union[IndexExpr, MemberExpr, NameExpr]
1616

1717

18-
class Frame(Dict[Optional[Key], Type]):
18+
class Frame(Dict[Key, Type]):
1919
"""A Frame represents a specific point in the execution of a program.
2020
It carries information about the current types of expressions at
2121
that point, arising either from assignments to those expressions
@@ -31,7 +31,7 @@ def __init__(self) -> None:
3131
self.unreachable = False
3232

3333

34-
class DeclarationsFrame(Dict[Optional[Key], Optional[Type]]):
34+
class DeclarationsFrame(Dict[Key, Optional[Type]]):
3535
"""Same as above, but allowed to have None values."""
3636

3737
def __init__(self) -> None:
@@ -80,7 +80,7 @@ def __init__(self) -> None:
8080
self.declarations = DeclarationsFrame()
8181
# Set of other keys to invalidate if a key is changed, e.g. x -> {x.a, x[0]}
8282
# Whenever a new key (e.g. x.a.b) is added, we update this
83-
self.dependencies = {} # type: Dict[Optional[Key], Set[Key]]
83+
self.dependencies = {} # type: Dict[Key, Set[Key]]
8484

8585
# Whether the last pop changed the newly top frame on exit
8686
self.last_pop_changed = False
@@ -105,10 +105,10 @@ def push_frame(self) -> Frame:
105105
self.options_on_return.append([])
106106
return f
107107

108-
def _put(self, key: Optional[Key], type: Type, index: int=-1) -> None:
108+
def _put(self, key: Key, type: Type, index: int=-1) -> None:
109109
self.frames[index][key] = type
110110

111-
def _get(self, key: Optional[Key], index: int=-1) -> Optional[Type]:
111+
def _get(self, key: Key, index: int=-1) -> Optional[Type]:
112112
if index < 0:
113113
index += len(self.frames)
114114
for i in range(index, -1, -1):
@@ -122,17 +122,18 @@ def put(self, expr: Expression, typ: Type) -> None:
122122
if not expr.literal:
123123
return
124124
key = expr.literal_hash
125+
assert key is not None
125126
if key not in self.declarations:
126127
assert isinstance(expr, BindableTypes)
127128
self.declarations[key] = get_declaration(expr)
128-
if key is not None:
129-
self._add_dependencies(key)
129+
self._add_dependencies(key)
130130
self._put(key, typ)
131131

132132
def unreachable(self) -> None:
133133
self.frames[-1].unreachable = True
134134

135135
def get(self, expr: Expression) -> Optional[Type]:
136+
assert expr.literal_hash is not None
136137
return self._get(expr.literal_hash)
137138

138139
def is_unreachable(self) -> bool:
@@ -142,9 +143,10 @@ def is_unreachable(self) -> bool:
142143

143144
def cleanse(self, expr: Expression) -> None:
144145
"""Remove all references to a Node from the binder."""
146+
assert expr.literal_hash is not None
145147
self._cleanse_key(expr.literal_hash)
146148

147-
def _cleanse_key(self, key: Optional[Key]) -> None:
149+
def _cleanse_key(self, key: Key) -> None:
148150
"""Remove all references to a key from the binder."""
149151
for frame in self.frames:
150152
if key in frame:
@@ -259,13 +261,15 @@ def invalidate_dependencies(self, expr: BindableExpression) -> None:
259261
It is overly conservative: it invalidates globally, including
260262
in code paths unreachable from here.
261263
"""
264+
assert expr.literal_hash is not None
262265
for dep in self.dependencies.get(expr.literal_hash, set()):
263266
self._cleanse_key(dep)
264267

265268
def most_recent_enclosing_type(self, expr: BindableExpression, type: Type) -> Optional[Type]:
266269
if isinstance(type, AnyType):
267270
return get_declaration(expr)
268271
key = expr.literal_hash
272+
assert key is not None
269273
enclosers = ([get_declaration(expr)] +
270274
[f[key] for f in self.frames
271275
if key in f and is_subtype(type, f[key])])

0 commit comments

Comments
 (0)