Skip to content

Commit 755abd7

Browse files
committed
declare expr.literal_hash as Optional[Key]
1 parent 5928551 commit 755abd7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

mypy/binder.py

Lines changed: 9 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[Key, Type]):
18+
class Frame(Dict[Optional[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[Key, Optional[Type]]):
34+
class DeclarationsFrame(Dict[Optional[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[Key, Set[Key]]
83+
self.dependencies = {} # type: Dict[Optional[Key], Set[Key]]
8484

8585
# Whether the last pop changed the newly top frame on exit
8686
self.last_pop_changed = False
@@ -89,7 +89,7 @@ def __init__(self) -> None:
8989
self.break_frames = [] # type: List[int]
9090
self.continue_frames = [] # type: List[int]
9191

92-
def _add_dependencies(self, key: Key, value: Key = None) -> None:
92+
def _add_dependencies(self, key: Key, value: Optional[Key] = None) -> None:
9393
if value is None:
9494
value = key
9595
else:
@@ -105,10 +105,10 @@ def push_frame(self) -> Frame:
105105
self.options_on_return.append([])
106106
return f
107107

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

111-
def _get(self, key: Key, index: int=-1) -> Optional[Type]:
111+
def _get(self, key: Optional[Key], index: int=-1) -> Optional[Type]:
112112
if index < 0:
113113
index += len(self.frames)
114114
for i in range(index, -1, -1):
@@ -125,7 +125,8 @@ def put(self, expr: Expression, typ: Type) -> None:
125125
if key not in self.declarations:
126126
assert isinstance(expr, BindableTypes)
127127
self.declarations[key] = get_declaration(expr)
128-
self._add_dependencies(key)
128+
if key is not None:
129+
self._add_dependencies(key)
129130
self._put(key, typ)
130131

131132
def unreachable(self) -> None:
@@ -143,7 +144,7 @@ def cleanse(self, expr: Expression) -> None:
143144
"""Remove all references to a Node from the binder."""
144145
self._cleanse_key(expr.literal_hash)
145146

146-
def _cleanse_key(self, key: Key) -> None:
147+
def _cleanse_key(self, key: Optional[Key]) -> None:
147148
"""Remove all references to a key from the binder."""
148149
for frame in self.frames:
149150
if key in frame:

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)