-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Prohibit assignment to __class__ (Fixes #7724) #19180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
59450e8
523a168
fe381b2
111a3c4
cd6d90b
78a6c99
3a6b580
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[case test_assign_to_dunder_class] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some historical reason, all our testcases use |
||
class A: | ||
def foo(self): | ||
self.__class__ = B # E: Assignment to '__class__' is unsafe and not allowed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add testcases with non- I'd also love to see tests where this is assigned to various other destinations:
|
||
|
||
class B: | ||
pass | ||
|
||
[case test_assign_to_other_dunder_attributes] | ||
class C: | ||
def bar(self): | ||
self.__name__ = "NewName" # OK | ||
self.__doc__ = "Test doc" # OK | ||
|
||
class D: | ||
pass | ||
|
||
[case test_assign_to_regular_attribute] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We certainly already have tests checking that. |
||
class E: | ||
x = 1 | ||
def baz(self): | ||
self.x = 2 # OK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wording is too strong - assignments are fine themselves, there's nothing inherently unsafe with doing this, it's not a security vuln. Probably something along the lines of
(probably accompanied by a
self.note
explaining it further) would be less intrusive.