Skip to content

Commit a0728cd

Browse files
committed
added tests for pep572 w/ allow-redefinition flag
1 parent 5a0fa55 commit a0728cd

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- Test cases for interaction with allow-redefinition flag
2+
-- and PEP572 (the walrus operator, :=)
3+
4+
-- Base cases for no walrus operator usage & without flag
5+
[case testRedefine_noUsage]
6+
# flags: --allow-redefinition
7+
a: int
8+
a = 1
9+
a = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
10+
if (a == 1.0):
11+
reveal_type(a) # N: Revealed type is "builtins.int"
12+
reveal_type(a) # N: Revealed type is "builtins.int"
13+
14+
[builtins fixtures/float.pyi]
15+
16+
[case testRedefine_withUsage]
17+
# flags: --allow-redefinition
18+
a: int
19+
b: int
20+
a = 1
21+
b = 2
22+
b = b + a
23+
reveal_type(a) # N: Revealed type is "builtins.int"
24+
a = 1.0
25+
if (a == 1.0):
26+
reveal_type(a) # N: Revealed type is "builtins.float"
27+
reveal_type(a) # N: Revealed type is "builtins.float"
28+
29+
[builtins fixtures/float.pyi]
30+
31+
[case test_withUsage]
32+
# flags:
33+
a: int
34+
b: int
35+
a = 1
36+
b = 2
37+
b = b + a
38+
reveal_type(a) # N: Revealed type is "builtins.int"
39+
a = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
40+
if (a == 1.0):
41+
reveal_type(a) # N: Revealed type is "builtins.int"
42+
reveal_type(a) # N: Revealed type is "builtins.int"
43+
44+
[builtins fixtures/float.pyi]
45+
46+
-- This should error due to no usage before attempted redefinition
47+
[case testRedefine_PEP572_noUsage]
48+
# flags: --allow-redefinition
49+
a: int
50+
a = 1
51+
52+
if (a := 1.0): # E: Incompatible types in assignment (expression has type "float", variable has type "int")
53+
reveal_type(a) # N: Revealed type is "builtins.int"
54+
reveal_type(a) # N: Revealed type is "builtins.int"
55+
56+
[builtins fixtures/float.pyi]
57+
58+
-- This should pass as 'a' is used before it is attempted to be redefined
59+
[case testRedefine_PEP572_withUsage]
60+
# flags: --allow-redefinition
61+
a: int
62+
b: int
63+
a = 1
64+
b = 2
65+
b = b + a
66+
reveal_type(a) # N: Revealed type is "builtins.int"
67+
68+
if (a := 1.0):
69+
reveal_type(a) # N: Revealed type is "builtins.float"
70+
reveal_type(a) # N: Revealed type is "builtins.float"
71+
72+
[builtins fixtures/float.pyi]

test-data/unit/fixtures/float.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class ellipsis: pass
2121

2222
class int:
2323
def __abs__(self) -> int: ...
24+
def __add__(self, other: int) -> int: ...
2425
def __float__(self) -> float: ...
2526
def __int__(self) -> int: ...
2627
def __mul__(self, x: int) -> int: ...
2728
def __neg__(self) -> int: ...
2829
def __rmul__(self, x: int) -> int: ...
2930

3031
class float:
32+
def __eq__(self, other: float) -> bool: ...
3133
def __float__(self) -> float: ...
3234
def __int__(self) -> int: ...
3335
def __mul__(self, x: float) -> float: ...

0 commit comments

Comments
 (0)