|
| 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] |
0 commit comments