Skip to content

Add test interactions for PP572 with --allow-redefinition #19222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test-data/unit/check-pep572-redefinition.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- Test cases for interaction with allow-redefinition flag
-- and PEP572 (the walrus operator, :=)

-- Base cases for no walrus operator usage & without flag
[case testRedefine_noUsage]
# flags: --allow-redefinition
a: int
a = 1
a = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
if (a == 1.0):
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(a) # N: Revealed type is "builtins.int"

[builtins fixtures/float.pyi]

[case testRedefine_withUsage]
# flags: --allow-redefinition
a: int
b: int
a = 1
b = 2
b = b + a
reveal_type(a) # N: Revealed type is "builtins.int"
a = 1.0
if (a == 1.0):
reveal_type(a) # N: Revealed type is "builtins.float"
reveal_type(a) # N: Revealed type is "builtins.float"

[builtins fixtures/float.pyi]

[case test_withUsage]
# flags:
a: int
b: int
a = 1
b = 2
b = b + a
reveal_type(a) # N: Revealed type is "builtins.int"
a = 1.0 # E: Incompatible types in assignment (expression has type "float", variable has type "int")
if (a == 1.0):
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(a) # N: Revealed type is "builtins.int"

[builtins fixtures/float.pyi]

-- This should error due to no usage before attempted redefinition
[case testRedefine_PEP572_noUsage]
# flags: --allow-redefinition
a: int
a = 1

if (a := 1.0): # E: Incompatible types in assignment (expression has type "float", variable has type "int")
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(a) # N: Revealed type is "builtins.int"

[builtins fixtures/float.pyi]

-- This should pass as 'a' is used before it is attempted to be redefined
[case testRedefine_PEP572_withUsage]
# flags: --allow-redefinition
a: int
b: int
a = 1
b = 2
b = b + a
reveal_type(a) # N: Revealed type is "builtins.int"

if (a := 1.0):
reveal_type(a) # N: Revealed type is "builtins.float"
reveal_type(a) # N: Revealed type is "builtins.float"

[builtins fixtures/float.pyi]
2 changes: 2 additions & 0 deletions test-data/unit/fixtures/float.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ class ellipsis: pass

class int:
def __abs__(self) -> int: ...
def __add__(self, other: int) -> int: ...
def __float__(self) -> float: ...
def __int__(self) -> int: ...
def __mul__(self, x: int) -> int: ...
def __neg__(self) -> int: ...
def __rmul__(self, x: int) -> int: ...

class float:
def __eq__(self, other: float) -> bool: ...
def __float__(self) -> float: ...
def __int__(self) -> int: ...
def __mul__(self, x: float) -> float: ...
Expand Down
Loading