Skip to content

Commit 3f0c355

Browse files
authored
Fix crash on partial type, --allow-redefinition, and global declaration (#21428)
Filter out partial types when updating binder. Fixes #21423.
1 parent 1730c95 commit 3f0c355

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

mypy/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8445,7 +8445,9 @@ def visit_global_decl(self, o: GlobalDecl, /) -> None:
84458445
n.node = sym.node
84468446
n.kind = GDEF
84478447
n.fullname = sym.node.fullname
8448-
self.binder.assign_type(n, sym.node.type, sym.node.type)
8448+
typ = get_declaration(n)
8449+
if typ is not None:
8450+
self.binder.assign_type(n, typ, typ)
84498451

84508452

84518453
class TypeCheckerAsSemanticAnalyzer(SemanticAnalyzerCoreInterface):

test-data/unit/check-inference.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4369,3 +4369,22 @@ def g() -> None:
43694369
reveal_type(x) # N: Revealed type is "None | builtins.int"
43704370
x = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int | None")
43714371
reveal_type(x) # N: Revealed type is "None | builtins.int"
4372+
4373+
[case testLocalPartialTypesWithGlobalInitializedToEmptyListAndRedefine1]
4374+
# flags: --allow-redefinition
4375+
a = [] # E: Need type annotation for "a" (hint: "a: list[<type>] = ...")
4376+
4377+
def f() -> None:
4378+
a.append(1)
4379+
4380+
reveal_type(a) # N: Revealed type is "builtins.list[Any]"
4381+
[builtins fixtures/list.pyi]
4382+
4383+
[case testLocalPartialTypesWithGlobalInitializedToEmptyListAndRedefine2]
4384+
# flags: --allow-redefinition
4385+
x = [] # E: Need type annotation for "x" (hint: "x: list[<type>] = ...")
4386+
4387+
# This used to crash.
4388+
def f() -> None:
4389+
global x
4390+
x

0 commit comments

Comments
 (0)