From 821478249390b47200c2c53f93da5db028daf814 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 7 May 2026 10:46:05 +0100 Subject: [PATCH] Fix crash on partial type, --allow-redefinition, and global declaration Filter out partial types when updating binder. Fixes #21423. --- mypy/checker.py | 4 +++- test-data/unit/check-inference.test | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mypy/checker.py b/mypy/checker.py index 58b7fedf55f20..7c787d9a42c91 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -8445,7 +8445,9 @@ def visit_global_decl(self, o: GlobalDecl, /) -> None: n.node = sym.node n.kind = GDEF n.fullname = sym.node.fullname - self.binder.assign_type(n, sym.node.type, sym.node.type) + typ = get_declaration(n) + if typ is not None: + self.binder.assign_type(n, typ, typ) class TypeCheckerAsSemanticAnalyzer(SemanticAnalyzerCoreInterface): diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 91c1b8558c575..a5b3ae7238a5a 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -4369,3 +4369,22 @@ def g() -> None: reveal_type(x) # N: Revealed type is "None | builtins.int" x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int | None") reveal_type(x) # N: Revealed type is "None | builtins.int" + +[case testLocalPartialTypesWithGlobalInitializedToEmptyListAndRedefine1] +# flags: --allow-redefinition +a = [] # E: Need type annotation for "a" (hint: "a: list[] = ...") + +def f() -> None: + a.append(1) + +reveal_type(a) # N: Revealed type is "builtins.list[Any]" +[builtins fixtures/list.pyi] + +[case testLocalPartialTypesWithGlobalInitializedToEmptyListAndRedefine2] +# flags: --allow-redefinition +x = [] # E: Need type annotation for "x" (hint: "x: list[] = ...") + +# This used to crash. +def f() -> None: + global x + x