Skip to content

Commit 2bc2a37

Browse files
committed
Prevent assignment to None for non-Optional class variables
Python 3.5 has been dead for a long time
1 parent da4796a commit 2bc2a37

File tree

1 file changed

+3
-18
lines changed

1 file changed

+3
-18
lines changed

mypy/checker.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,7 +3209,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
32093209
# as X | Y.
32103210
if not (s.is_alias_def and self.is_stub):
32113211
with self.enter_final_context(s.is_final_def):
3212-
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
3212+
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
32133213

32143214
if s.is_alias_def:
32153215
self.check_type_alias_rvalue(s)
@@ -3257,11 +3257,7 @@ def check_type_alias_rvalue(self, s: AssignmentStmt) -> None:
32573257
self.store_type(s.lvalues[-1], alias_type)
32583258

32593259
def check_assignment(
3260-
self,
3261-
lvalue: Lvalue,
3262-
rvalue: Expression,
3263-
infer_lvalue_type: bool = True,
3264-
new_syntax: bool = False,
3260+
self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True
32653261
) -> None:
32663262
"""Type check a single assignment: lvalue = rvalue."""
32673263
if isinstance(lvalue, (TupleExpr, ListExpr)):
@@ -3339,15 +3335,6 @@ def check_assignment(
33393335
# We are replacing partial<None> now, so the variable type
33403336
# should remain optional.
33413337
self.set_inferred_type(var, lvalue, make_optional_type(fallback))
3342-
elif (
3343-
is_literal_none(rvalue)
3344-
and isinstance(lvalue, NameExpr)
3345-
and isinstance(lvalue.node, Var)
3346-
and lvalue.node.is_initialized_in_class
3347-
and not new_syntax
3348-
):
3349-
# Allow None's to be assigned to class variables with non-Optional types.
3350-
rvalue_type = lvalue_type
33513338
elif (
33523339
isinstance(lvalue, MemberExpr) and lvalue.kind is None
33533340
): # Ignore member access to modules
@@ -5108,9 +5095,7 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
51085095
# There is no __ifoo__, treat as x = x <foo> y
51095096
expr = OpExpr(s.op, s.lvalue, s.rvalue)
51105097
expr.set_line(s)
5111-
self.check_assignment(
5112-
lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True, new_syntax=False
5113-
)
5098+
self.check_assignment(lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True)
51145099
self.check_final(s)
51155100

51165101
def visit_assert_stmt(self, s: AssertStmt) -> None:

0 commit comments

Comments
 (0)