From 293004e22081b54a103b73d19a09d88b7d803970 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 11 Nov 2026 01:44:23 +0000 Subject: [PATCH] Tighten detection of non-local partial None types --- mypy/nodes.py | 3 +++ mypy/semanal.py | 42 ++++++++++++++++++++++++----- test-data/unit/check-inference.test | 18 +++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 1dfd70267b06a..7262913dd2127 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -1460,6 +1460,7 @@ class Var(SymbolNode): "invalid_partial_type", "is_argument", "is_sentinel", + "is_init_to_none", ) __match_args__ = ("name", "type", "final_value") @@ -1524,6 +1525,8 @@ def __init__(self, name: str, type: mypy.types.Type | None = None) -> None: self.is_argument = False # Was this variable created by PEP 661 sentinel()/Sentinel() syntax? self.is_sentinel = False + # Is this variable initialized to None? (May be not set for ad-hoc vars) + self.is_init_to_none = False @property def name(self) -> str: diff --git a/mypy/semanal.py b/mypy/semanal.py index c079a865af1de..0cedce64a68b9 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -3796,6 +3796,7 @@ def analyze_lvalues(self, s: AssignmentStmt) -> None: explicit_type=explicit, is_final=s.is_final_def, has_explicit_value=has_explicit_value, + rvalue=s.rvalue, ) def analyze_rvalue_as_type_form(self, s: AssignmentStmt) -> None: @@ -4511,6 +4512,7 @@ def analyze_lvalue( escape_comprehensions: bool = False, has_explicit_value: bool = False, is_index_var: bool = False, + rvalue: Expression | None = None, ) -> None: """Analyze an lvalue or assignment target. @@ -4522,6 +4524,7 @@ def analyze_lvalue( in the enclosing scope instead. This implements https://www.python.org/dev/peps/pep-0572/#scope-of-the-target is_index_var: If lval is the index variable in a for loop + rvalue: Assignment rvalue (passed only in situations when it affects semantics). """ if escape_comprehensions: assert isinstance(lval, NameExpr), "assignment expression target must be NameExpr" @@ -4532,10 +4535,13 @@ def analyze_lvalue( is_final, escape_comprehensions, has_explicit_value=has_explicit_value, + is_init_to_none=is_none_expr(rvalue), is_index_var=is_index_var, ) elif isinstance(lval, MemberExpr): - self.analyze_member_lvalue(lval, explicit_type, is_final, has_explicit_value) + self.analyze_member_lvalue( + lval, explicit_type, is_final, has_explicit_value, is_none_expr(rvalue) + ) if explicit_type and not self.is_self_member_ref(lval): self.fail("Type cannot be declared in assignment to non-self attribute", lval) elif isinstance(lval, IndexExpr): @@ -4543,7 +4549,7 @@ def analyze_lvalue( self.fail("Unexpected type declaration", lval) lval.accept(self) elif isinstance(lval, TupleExpr): - self.analyze_tuple_or_list_lvalue(lval, explicit_type) + self.analyze_tuple_or_list_lvalue(lval, explicit_type, rvalue) elif isinstance(lval, StarExpr): if nested: self.analyze_lvalue(lval.expr, nested, explicit_type) @@ -4559,6 +4565,7 @@ def analyze_name_lvalue( is_final: bool, escape_comprehensions: bool, has_explicit_value: bool, + is_init_to_none: bool, is_index_var: bool, ) -> None: """Analyze an lvalue that targets a name expression. @@ -4606,7 +4613,7 @@ def analyze_name_lvalue( if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer: # Define new variable. var = self.make_name_lvalue_var( - lvalue, kind, not explicit_type, has_explicit_value, is_index_var + lvalue, kind, not explicit_type, has_explicit_value, is_init_to_none, is_index_var ) added = self.add_symbol(name, var, lvalue, escape_comprehensions=escape_comprehensions) # Only bind expression if we successfully added name to symbol table. @@ -4666,6 +4673,7 @@ def make_name_lvalue_var( kind: int, inferred: bool, has_explicit_value: bool, + is_init_to_none: bool, is_index_var: bool, ) -> Var: """Return a Var node for an lvalue that is a name expression.""" @@ -4686,6 +4694,7 @@ def make_name_lvalue_var( v.is_ready = False # Type not inferred yet v.has_explicit_value = has_explicit_value v.is_index_var = is_index_var + v.is_init_to_none = is_init_to_none return v def make_name_lvalue_point_to_existing_def( @@ -4722,11 +4731,14 @@ def make_name_lvalue_point_to_existing_def( original_def and isinstance(original_def.node, Var) and original_def.node.is_inferred + and original_def.node.is_init_to_none ): for func in self.scope.functions: mark_as_top_level(func) - def analyze_tuple_or_list_lvalue(self, lval: TupleExpr, explicit_type: bool = False) -> None: + def analyze_tuple_or_list_lvalue( + self, lval: TupleExpr, explicit_type: bool, rvalue: Expression | None + ) -> None: """Analyze an lvalue or assignment target that is a list or tuple.""" items = lval.items star_exprs = [item for item in items if isinstance(item, StarExpr)] @@ -4736,18 +4748,28 @@ def analyze_tuple_or_list_lvalue(self, lval: TupleExpr, explicit_type: bool = Fa else: if len(star_exprs) == 1: star_exprs[0].valid = True - for i in items: + for i, it in enumerate(items): + if isinstance(rvalue, TupleExpr) and len(rvalue.items) == len(items): + item_rvalue = rvalue.items[i] + else: + item_rvalue = None self.analyze_lvalue( - lval=i, + lval=it, nested=True, explicit_type=explicit_type, # Lists and tuples always have explicit values defined: # `a, b, c = value` has_explicit_value=True, + rvalue=item_rvalue, ) def analyze_member_lvalue( - self, lval: MemberExpr, explicit_type: bool, is_final: bool, has_explicit_value: bool + self, + lval: MemberExpr, + explicit_type: bool, + is_final: bool, + has_explicit_value: bool, + is_init_to_none: bool, ) -> None: """Analyze lvalue that is a member expression. @@ -4804,6 +4826,7 @@ def analyze_member_lvalue( v.info = self.type v.is_ready = False v.explicit_self_type = explicit_type or is_final + v.is_init_to_none = is_init_to_none lval.def_var = v lval.node = v # TODO: should we also set lval.kind = MDEF? @@ -4819,6 +4842,7 @@ def analyze_member_lvalue( and isinstance(cur_node.node, Var) and cur_node.node.is_inferred and cur_node.node.is_initialized_in_class + and cur_node.node.is_init_to_none ): for func in self.scope.functions: mark_as_top_level(func) @@ -8617,6 +8641,10 @@ def erase_func_annotations(func: FuncDef) -> None: func.unanalyzed_type = None +def is_none_expr(expr: Expression | None) -> bool: + return isinstance(expr, NameExpr) and expr.name == "None" + + def mark_as_top_level(func: FuncBase) -> None: """Mark a function to be processed as part of module top level. diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index e13b986eb778d..08a70d1a6efef 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -2106,6 +2106,24 @@ def f() -> None: x = 1 x() # E: "None" not callable +[case testPartialNoneTypeInTuple] +x, y = None, None + +def f() -> None: + global x, y + if bool(): + x = y = 1 + reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(y) # N: Revealed type is "builtins.int | None" + +class C: + x, y = None, None + def f(self) -> None: + if bool(): + self.x = self.y = 1 + reveal_type(self.x) # N: Revealed type is "builtins.int | None" + reveal_type(self.y) # N: Revealed type is "builtins.int | None" + [case testAttributePartiallyInitializedToNone] class A: def f(self) -> None: