From 0f8cdee02a764663a1f5f15deb63442214eacc28 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Sat, 14 Nov 2026 16:02:03 +0500 Subject: [PATCH] Fix non-local partial None detection for non-literal None assignments python/mypy#22437 made semantic analysis mark a function to be checked together with the module top level (needed for non-local partial None types) only when the variable's first assignment has a literal `None` rvalue. However, the checker creates a partial None type for any inferred variable whose initial type is None, so a for loop index iterating over `[None]`, an assignment expression target, or `x = None if c else None` no longer got completed in functions using `global`/`nonlocal` or `self.x = ...`, and produced spurious errors such as "Incompatible types in assignment (expression has type "int", variable has type "None")". Treat binding forms without an rvalue (for loop index, assignment expression target, with/match/star targets, unmatched tuple items) as possibly None, and also look through conditional expressions and assignment expressions in the rvalue. Other rvalues are still treated as not None to keep the speedup from python/mypy#22437. In particular, `x = f()` where `f` returns None is intentionally not handled, since it is already reported as a func-returns-value error. --- mypy/nodes.py | 2 +- mypy/semanal.py | 21 +++++++++++++++++---- test-data/unit/check-inference.test | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 7262913dd212..88807102296d 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -1525,7 +1525,7 @@ 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) + # Can this variable be initialized to None? (May be not set for ad-hoc vars) self.is_init_to_none = False @property diff --git a/mypy/semanal.py b/mypy/semanal.py index 0cedce64a68b..2decce34c0a7 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4535,12 +4535,12 @@ def analyze_lvalue( is_final, escape_comprehensions, has_explicit_value=has_explicit_value, - is_init_to_none=is_none_expr(rvalue), + is_init_to_none=may_be_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, is_none_expr(rvalue) + lval, explicit_type, is_final, has_explicit_value, may_be_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) @@ -8641,8 +8641,21 @@ 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 may_be_none_expr(expr: Expression | None) -> bool: + """Can an assignment with this rvalue infer a (partial) None type? + + If rvalue is None, it is unknown (e.g. for loop index or assignment expression + target), so we assume it may be None. Otherwise we only look for syntactic None + values. In particular, calls are not considered, since assigning the result of + a function returning None is already an error. + """ + if expr is None or isinstance(expr, NameExpr) and expr.name == "None": + return True + if isinstance(expr, ConditionalExpr): + return may_be_none_expr(expr.if_expr) or may_be_none_expr(expr.else_expr) + if isinstance(expr, AssignmentExpr): + return may_be_none_expr(expr.value) + return False def mark_as_top_level(func: FuncBase) -> None: diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 08a70d1a6efe..e40b01566d33 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -2124,6 +2124,30 @@ class C: reveal_type(self.x) # N: Revealed type is "builtins.int | None" reveal_type(self.y) # N: Revealed type is "builtins.int | None" +[case testPartialNoneTypeNonLiteralNone] +x = None if bool() else None +y = (z := None) +for w in [None]: pass + +def f() -> None: + global x, y, z, w + if bool(): + x = y = z = w = 1 + reveal_type(x) # N: Revealed type is "builtins.int | None" + reveal_type(y) # N: Revealed type is "builtins.int | None" + reveal_type(z) # N: Revealed type is "builtins.int | None" + reveal_type(w) # N: Revealed type is "builtins.int | None" + +class C: + x = None if bool() else None + y = (t := 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" +[builtins fixtures/list.pyi] + [case testAttributePartiallyInitializedToNone] class A: def f(self) -> None: