8000 More comments · python/mypy@737ccb3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 737ccb3

Browse files
committed
More comments
1 parent c6f1a29 commit 737ccb3

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
@@ -69,7 +69,25 @@ class Key(AnyType):
6969

7070

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

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

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

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

0 commit comments

Comments
 (0)
0