Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading