Skip to content

Commit cd6d90b

Browse files
committed
Disallow assignment to '__class__' in TypeChecker.visit_assignment_stmt
Added a check inside TypeChecker.visit_assignment_stmt to disallow assignments to '__class__', as such assignments are unsafe and may lead to unpredictable behavior. The new check raises an error using self.fail when '__class__' is assigned, matching the style used in other TypeChecker checks. This change addresses issue #7724.
1 parent 111a3c4 commit cd6d90b

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

mypy/checker.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8882,11 +8882,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
88828882
lv.accept(self)
88838883
if isinstance(lv, MemberExpr):
88848884
if lv.name == '__class__':
8885-
self.errors.report(
8886-
lv.line,
8887-
lv.column,
8888-
"Assignment to '__class__' is unsafe and not allowed"
8889-
)
8885+
self.fail("Assignment to '__class__' is unsafe and not allowed", lv)
88908886
self.lvalue = False
88918887

88928888
def visit_name_expr(self, e: NameExpr) -> None:

0 commit comments

Comments
 (0)