Skip to content

Commit f3c08f3

Browse files
committed
More comments
1 parent b80871d commit f3c08f3

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

mypy/checker.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,25 @@ class Key(AnyType):
6868

6969

7070
class ConditionalTypeBinder:
71-
"""Keep track of conditional types of variables."""
71+
"""Keep track of conditional types of variables.
72+
73+
NB: Variables are tracked by literal expression, so it is possible
74+
to confuse the binder; for example,
75+
76+
```
77+
class A:
78+
a = None # type: Union[int, str]
79+
x = A()
80+
lst = [x]
81+
reveal_type(x.a) # Union[int, str]
82+
x.a = 1
83+
reveal_type(x.a) # int
84+
reveal_type(lst[0].a) # Union[int, str]
85+
lst[0].a = 'a'
86+
reveal_type(x.a) # int
87+
reveal_type(lst[0].a) # str
88+
```
89+
"""
7290

7391
def __init__(self) -> None:
7492
# The set of frames currently used. These map
@@ -87,10 +105,13 @@ def __init__(self) -> None:
87105
# Whenever a new key (e.g. x.a.b) is added, we update this
88106
self.dependencies = {} # type: Dict[Key, Set[Key]]
89107

90-
# Set to True on return/break/raise, False on blocks that can block any of them
108+
# breaking_out is set to True on return/break/continue/raise
109+
# It is cleared on pop_frame() and placed in last_pop_breaking_out
110+
# Lines of code after breaking_out = True are unreachable and not
111+
# typechecked.
91112
self.breaking_out = False
92113

93-
# Whether the last pop changed the top frame on exit
114+
# Whether the last pop changed the newly top frame on exit
94115
self.last_pop_changed = False
95116
# Whether the last pop was necessarily breaking out, and couldn't fall through
96117
self.last_pop_breaking_out = False

0 commit comments

Comments
 (0)