Skip to content

Commit 8a9935b

Browse files
committed
Prevent assignment to None for non-Optional class variables
Python 3.5 has been dead for a long time
1 parent 6dc4698 commit 8a9935b

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

mypy/checker.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,7 +3175,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
31753175
# as X | Y.
31763176
if not (s.is_alias_def and self.is_stub):
31773177
with self.enter_final_context(s.is_final_def):
3178-
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
3178+
self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
31793179

31803180
if s.is_alias_def:
31813181
self.check_type_alias_rvalue(s)
@@ -3227,7 +3227,6 @@ def check_assignment(
32273227
lvalue: Lvalue,
32283228
rvalue: Expression,
32293229
infer_lvalue_type: bool = True,
3230-
new_syntax: bool = False,
32313230
) -> None:
32323231
"""Type check a single assignment: lvalue = rvalue."""
32333232
if isinstance(lvalue, (TupleExpr, ListExpr)):
@@ -3295,15 +3294,6 @@ def check_assignment(
32953294
# Try to infer a partial type. No need to check the return value, as
32963295
# an error will be reported elsewhere.
32973296
self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type)
3298-
elif (
3299-
is_literal_none(rvalue)
3300-
and isinstance(lvalue, NameExpr)
3301-
and isinstance(lvalue.node, Var)
3302-
and lvalue.node.is_initialized_in_class
3303-
and not new_syntax
3304-
):
3305-
# Allow None's to be assigned to class variables with non-Optional types.
3306-
rvalue_type = lvalue_type
33073297
elif (
33083298
isinstance(lvalue, MemberExpr) and lvalue.kind is None
33093299
): # Ignore member access to modules
@@ -5057,7 +5047,7 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
50575047
expr = OpExpr(s.op, s.lvalue, s.rvalue)
50585048
expr.set_line(s)
50595049
self.check_assignment(
5060-
lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True, new_syntax=False
5050+
lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True
50615051
)
50625052
self.check_final(s)
50635053

0 commit comments

Comments
 (0)