Skip to content
Merged
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
3 changes: 3 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,7 @@ class Var(SymbolNode):
"invalid_partial_type",
"is_argument",
"is_sentinel",
"is_init_to_none",
)

__match_args__ = ("name", "type", "final_value")
Expand Down Expand Up @@ -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:
Expand Down
42 changes: 35 additions & 7 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand All @@ -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"
Expand All @@ -4532,18 +4535,21 @@ 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):
if explicit_type:
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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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."""
Expand All @@ -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(
Expand Down Expand Up @@ -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)]
Expand All @@ -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.

Expand Down Expand Up @@ -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?
Expand All @@ -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)
Expand Down Expand Up @@ -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.

Expand Down
18 changes: 18 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading