diff --git a/mypy/checker.py b/mypy/checker.py index 6d70dcb90e94..9ba9d8ee41e4 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3208,7 +3208,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None: # as X | Y. if not (s.is_alias_def and self.is_stub): with self.enter_final_context(s.is_final_def): - self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax) + self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None) if s.is_alias_def: self.check_type_alias_rvalue(s) @@ -3256,11 +3256,7 @@ def check_type_alias_rvalue(self, s: AssignmentStmt) -> None: self.store_type(s.lvalues[-1], alias_type) def check_assignment( - self, - lvalue: Lvalue, - rvalue: Expression, - infer_lvalue_type: bool = True, - new_syntax: bool = False, + self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True ) -> None: """Type check a single assignment: lvalue = rvalue.""" if isinstance(lvalue, (TupleExpr, ListExpr)): @@ -3338,15 +3334,6 @@ def check_assignment( # We are replacing partial now, so the variable type # should remain optional. self.set_inferred_type(var, lvalue, make_optional_type(fallback)) - elif ( - is_literal_none(rvalue) - and isinstance(lvalue, NameExpr) - and isinstance(lvalue.node, Var) - and lvalue.node.is_initialized_in_class - and not new_syntax - ): - # Allow None's to be assigned to class variables with non-Optional types. - rvalue_type = lvalue_type elif ( isinstance(lvalue, MemberExpr) and lvalue.kind is None ): # Ignore member access to modules @@ -5107,9 +5094,7 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None: # There is no __ifoo__, treat as x = x y expr = OpExpr(s.op, s.lvalue, s.rvalue) expr.set_line(s) - self.check_assignment( - lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True, new_syntax=False - ) + self.check_assignment(lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True) self.check_final(s) def visit_assert_stmt(self, s: AssertStmt) -> None: diff --git a/mypyc/test-data/irbuild-classes.test b/mypyc/test-data/irbuild-classes.test index 7954535d5dea..5ad9e6501bad 100644 --- a/mypyc/test-data/irbuild-classes.test +++ b/mypyc/test-data/irbuild-classes.test @@ -1051,7 +1051,7 @@ class B(A): y = LOL z: Optional[str] = None b = True - bogus = None # type: int + bogus = None # type: int # type: ignore [out] def A.lol(self): self :: __main__.A diff --git a/mypyc/test-data/run-classes.test b/mypyc/test-data/run-classes.test index 5535034faa50..04a3b1313a4d 100644 --- a/mypyc/test-data/run-classes.test +++ b/mypyc/test-data/run-classes.test @@ -1126,7 +1126,7 @@ class B(A): y = LOL z = None # type: Optional[str] b = True - bogus = None # type: int + bogus = None # type: int # type: ignore def g() -> None: a = A() diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 6db60275944f..79b76f19e09f 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -236,7 +236,8 @@ class C: [case testMultipleAssignmentNoneClassVariableInInit] from typing import Optional class C: - x, y = None, None # type: int, str + x, y = None, None # type: int, str # E: Incompatible types in assignment (expression has type "None", variable has type "int") \ + # E: Incompatible types in assignment (expression has type "None", variable has type "str") def __init__(self) -> None: self.x = None # E: Incompatible types in assignment (expression has type "None", variable has type "int") self.y = None # E: Incompatible types in assignment (expression has type "None", variable has type "str") diff --git a/test-data/unit/deps.test b/test-data/unit/deps.test index 2c231c9afff6..f1009d430c2c 100644 --- a/test-data/unit/deps.test +++ b/test-data/unit/deps.test @@ -516,7 +516,7 @@ class A: from n import A class B: - x = None # type: A + x = None # type: A # type: ignore [file n.py] class A: pass [out]