Skip to content

Commit 8c1b98b

Browse files
committed
more tests
1 parent 1bbce9d commit 8c1b98b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

mypy/newsemanal/semanal_pass1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def visit_import(self, node: Import) -> None:
9090

9191
def visit_if_stmt(self, s: IfStmt) -> None:
9292
infer_reachability_of_if_statement(s, self.options)
93+
for expr in s.expr:
94+
expr.accept(self)
9395
for node in s.body:
9496
node.accept(self)
9597
if s.else_body:

mypy/semanal_pass1.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ def visit_func_def(self, func: FuncDef, decorated: bool = False) -> None:
174174
if sem.type is not None:
175175
# Don't process methods during pass 1.
176176
return
177+
for arg in func.arguments:
178+
if arg.initializer:
179+
arg.initializer.accept(self)
177180
func.is_conditional = sem.block_depth[-1] > 0
178181
func._fullname = sem.qualified_name(func.name())
179182
at_module = sem.is_module_scope() and not decorated
@@ -309,6 +312,7 @@ def visit_import_all(self, node: ImportAll) -> None:
309312

310313
def visit_while_stmt(self, s: WhileStmt) -> None:
311314
if self.sem.is_module_scope():
315+
s.expr.accept(self)
312316
s.body.accept(self)
313317
if s.else_body:
314318
s.else_body.accept(self)

test-data/unit/check-38.test

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,48 @@ def g(x: int): ...
110110
[case testWalrus]
111111
if (a := 2):
112112
reveal_type(a) # E: Revealed type is 'builtins.int'
113+
114+
while (b := "x"):
115+
reveal_type(b) # E: Revealed type is 'builtins.str'
116+
117+
def f(x: int = (c := 4)) -> None:
118+
if (a := 2):
119+
reveal_type(a) # E: Revealed type is 'builtins.int'
120+
121+
while (b := "x"):
122+
reveal_type(b) # E: Revealed type is 'builtins.str'
123+
124+
x = (y := 1) + (z := 2)
125+
reveal_type(x) # E: Revealed type is 'builtins.int'
126+
reveal_type(y) # E: Revealed type is 'builtins.int'
127+
reveal_type(z) # E: Revealed type is 'builtins.int'
128+
129+
l = [y2 := 1, y2 + 2, y2 + 3]
130+
reveal_type(y2) # E: Revealed type is 'builtins.int'
131+
reveal_type(l) # E: Revealed type is 'builtins.list[builtins.int*]'
132+
133+
filtered_data = [y3 for x in l if (y3 := a) is not None]
134+
reveal_type(filtered_data) # E: Revealed type is 'builtins.list[builtins.int*]'
135+
# TODO this should be valid: https://www.python.org/dev/peps/pep-0572/#scope-of-the-target
136+
y3 # E: Name 'y3' is not defined
137+
138+
# https://www.python.org/dev/peps/pep-0572/#exceptional-cases
139+
(y4 := 3)
140+
reveal_type(y4) # E: Revealed type is 'builtins.int'
141+
142+
y5 = (y6 := 3)
143+
reveal_type(y5) # E: Revealed type is 'builtins.int'
144+
reveal_type(y6) # E: Revealed type is 'builtins.int'
145+
146+
f(x=(y7 := 3))
147+
reveal_type(y7) # E: Revealed type is 'builtins.int'
148+
149+
reveal_type((lambda: (y8 := 3) and y8)()) # E: Revealed type is 'builtins.int'
150+
151+
y7 = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
152+
if y7 := "x": # E: Incompatible types in assignment (expression has type "str", variable has type "int")
153+
pass
154+
155+
reveal_type(c) # E: Revealed type is 'builtins.int'
156+
157+
[builtins fixtures/f_string.pyi]

0 commit comments

Comments
 (0)