From 8a9935bb4cfe41a2b09092474a87a86e86c45411 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sun, 12 Oct 2025 16:10:03 -0700 Subject: [PATCH 1/2] Prevent assignment to None for non-Optional class variables Python 3.5 has been dead for a long time --- mypy/checker.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index 3bee7b633339..c34055112063 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3175,7 +3175,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) @@ -3227,7 +3227,6 @@ def check_assignment( lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True, - new_syntax: bool = False, ) -> None: """Type check a single assignment: lvalue = rvalue.""" if isinstance(lvalue, (TupleExpr, ListExpr)): @@ -3295,15 +3294,6 @@ def check_assignment( # Try to infer a partial type. No need to check the return value, as # an error will be reported elsewhere. self.infer_partial_type(lvalue_type.var, lvalue, rvalue_type) - 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 @@ -5057,7 +5047,7 @@ def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None: 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 + lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True ) self.check_final(s) From 02a5c1b0b12b0f87c5d985afded85f0349300286 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 12 Oct 2025 23:13:11 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mypy/checker.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index c34055112063..3f63490f894f 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -3223,10 +3223,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, + self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type: bool = True ) -> None: """Type check a single assignment: lvalue = rvalue.""" if isinstance(lvalue, (TupleExpr, ListExpr)): @@ -5046,9 +5043,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 - ) + self.check_assignment(lvalue=s.lvalue, rvalue=expr, infer_lvalue_type=True) self.check_final(s) def visit_assert_stmt(self, s: AssertStmt) -> None: