Skip to content

Commit 8585a4f

Browse files
committed
Add tests for assignment expression in class scope
1 parent a68bd68 commit 8585a4f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

test-data/unit/check-python38.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,28 @@ def check_narrow(x: Optional[int], s: List[int]) -> None:
338338
if isinstance((y := x), int):
339339
reveal_type(y) # N: Revealed type is 'builtins.int'
340340

341+
class AssignmentExpressionsClass:
342+
x = (y := 1) + (z := 2)
343+
reveal_type(z) # N: Revealed type is 'builtins.int'
344+
345+
l = [x2 := 1, 2, 3]
346+
reveal_type(x2) # N: Revealed type is 'builtins.int'
347+
348+
def __init__(self) -> None:
349+
reveal_type(self.z) # N: Revealed type is 'builtins.int'
350+
351+
l = [z2 := 1, z2 + 2, z2 + 3]
352+
reveal_type(z2) # N: Revealed type is 'builtins.int'
353+
reveal_type(l) # N: Revealed type is 'builtins.list[builtins.int*]'
354+
355+
filtered_data = [z3 for x in l if (z3 := 1) is not None]
356+
reveal_type(filtered_data) # N: Revealed type is 'builtins.list[builtins.int*]'
357+
reveal_type(z3) # N: Revealed type is 'builtins.int'
358+
359+
# Assignment expressions from inside the class should not escape the class scope.
360+
reveal_type(x2) # E: Name 'x2' is not defined # N: Revealed type is 'Any'
361+
reveal_type(z2) # E: Name 'z2' is not defined # N: Revealed type is 'Any'
362+
341363
[builtins fixtures/isinstancelist.pyi]
342364

343365
[case testWalrusPartialTypes]

0 commit comments

Comments
 (0)