From a0728cdfe1d972d2c9095cd0fa09ab51ccd7b417 Mon Sep 17 00:00:00 2001 From: vixicat Date: Tue, 3 Jun 2025 15:31:03 -0700 Subject: [PATCH 1/2] added tests for pep572 w/ allow-redefinition flag --- test-data/unit/check-pep572-redefinition.test | 72 +++++++++++++++++++ test-data/unit/fixtures/float.pyi | 2 + 2 files changed, 74 insertions(+) create mode 100644 test-data/unit/check-pep572-redefinition.test diff --git a/test-data/unit/check-pep572-redefinition.test b/test-data/unit/check-pep572-redefinition.test new file mode 100644 index 000000000000..f2542be2be50 --- /dev/null +++ b/test-data/unit/check-pep572-redefinition.test @@ -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] diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index 9e2d20f04edf..43cb29f01a83 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -21,6 +21,7 @@ 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: ... @@ -28,6 +29,7 @@ class 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: ... From 07c1edb0f981e1f199ea649c061226234f390885 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Jun 2025 23:26:03 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test-data/unit/check-pep572-redefinition.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-pep572-redefinition.test b/test-data/unit/check-pep572-redefinition.test index f2542be2be50..78655f43dc93 100644 --- a/test-data/unit/check-pep572-redefinition.test +++ b/test-data/unit/check-pep572-redefinition.test @@ -1,4 +1,4 @@ --- Test cases for interaction with allow-redefinition flag +-- Test cases for interaction with allow-redefinition flag -- and PEP572 (the walrus operator, :=) -- Base cases for no walrus operator usage & without flag