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: