Skip to content

Commit 5a046ee

Browse files
elazargilevkivskyi
authored andcommitted
Declare literal_hash as Optional[Key] (#3658)
* declare expr.literal_hash as Optional[Key] * assert expr.literal_hash is never None * add error messages to assertion
1 parent d3afe68 commit 5a046ee

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

mypy/binder.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ 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, 'Internal error: binder tried to put non-literal'
125126
if key not in self.declarations:
126127
assert isinstance(expr, BindableTypes)
127128
self.declarations[key] = get_declaration(expr)
@@ -132,6 +133,7 @@ def unreachable(self) -> None:
132133
self.frames[-1].unreachable = True
133134

134135
def get(self, expr: Expression) -> Optional[Type]:
136+
assert expr.literal_hash is not None, 'Internal error: binder tried to get non-literal'
135137
return self._get(expr.literal_hash)
136138

137139
def is_unreachable(self) -> bool:
@@ -141,6 +143,7 @@ def is_unreachable(self) -> bool:
141143

142144
def cleanse(self, expr: Expression) -> None:
143145
"""Remove all references to a Node from the binder."""
146+
assert expr.literal_hash is not None, 'Internal error: binder tried cleanse non-literal'
144147
self._cleanse_key(expr.literal_hash)
145148

146149
def _cleanse_key(self, key: Key) -> None:
@@ -258,13 +261,15 @@ def invalidate_dependencies(self, expr: BindableExpression) -> None:
258261
It is overly conservative: it invalidates globally, including
259262
in code paths unreachable from here.
260263
"""
264+
assert expr.literal_hash is not None
261265
for dep in self.dependencies.get(expr.literal_hash, set()):
262266
self._cleanse_key(dep)
263267

264268
def most_recent_enclosing_type(self, expr: BindableExpression, type: Type) -> Optional[Type]:
265269
if isinstance(type, AnyType):
266270
return get_declaration(expr)
267271
key = expr.literal_hash
272+
assert key is not None
268273
enclosers = ([get_declaration(expr)] +
269274
[f[key] for f in self.frames
270275
if key in f and is_subtype(type, f[key])])

mypy/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def accept(self, visitor: StatementVisitor[T]) -> T:
156156
class Expression(Node):
157157
"""An expression node."""
158158
literal = LITERAL_NO
159-
literal_hash = None # type: Key
159+
literal_hash = None # type: Optional[Key]
160160

161161
def accept(self, visitor: ExpressionVisitor[T]) -> T:
162162
raise RuntimeError('Not implemented')

0 commit comments

Comments
 (0)