From a4347876c3e955983bc23108de6893a679c5ddae Mon Sep 17 00:00:00 2001 From: jbjd Date: Sun, 30 Mar 2025 13:04:46 -0500 Subject: [PATCH] Allow an assignment expression to redefine an annotation --- pyflakes/checker.py | 8 ++++++-- pyflakes/test/test_other.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index c7d3e882..9888d7ac 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -1008,8 +1008,12 @@ def addBinding(self, node, value): for scope in reversed(self.scopeStack) if not isinstance(scope, GeneratorScope) ) - # it may be a re-assignment to an already existing name - scope.setdefault(value.name, value) + if value.name in scope and isinstance(scope[value.name], Annotation): + # re-assignment to name that was previously only an annotation + scope[value.name] = value + else: + # it may be a re-assignment to an already existing name + scope.setdefault(value.name, value) else: self.scope[value.name] = value diff --git a/pyflakes/test/test_other.py b/pyflakes/test/test_other.py index 68c38e95..23990820 100644 --- a/pyflakes/test/test_other.py +++ b/pyflakes/test/test_other.py @@ -1744,6 +1744,13 @@ def test_assign_expr(self): print(x) ''') + def test_assign_expr_after_annotation(self): + self.flakes(""" + a: int + print(a := 3) + print(a) + """) + def test_assign_expr_generator_scope(self): """Test assignment expressions in generator expressions.""" self.flakes('''