-
Notifications
You must be signed in to change notification settings - Fork 276
[Conformance]fix Feature Request: support __slots__ #630 #2425
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: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1729,7 +1729,6 @@ assert_type(dc2.y, str) # E: assert_type(Desc2[str], str) failed | |||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| testcase!( | ||||||||||||||||||||||||||||||||||||||||||||||
| bug = "conformance: Dataclass with slots=True should error when setting undeclared attributes", | ||||||||||||||||||||||||||||||||||||||||||||||
| test_dataclass_slots_undeclared_attr_conformance, | ||||||||||||||||||||||||||||||||||||||||||||||
| r#" | ||||||||||||||||||||||||||||||||||||||||||||||
| from dataclasses import dataclass | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1741,7 +1740,7 @@ class DC2: | |||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.x = 3 | ||||||||||||||||||||||||||||||||||||||||||||||
| # should error: y is not in slots | ||||||||||||||||||||||||||||||||||||||||||||||
| self.y = 3 | ||||||||||||||||||||||||||||||||||||||||||||||
| self.y = 3 # E: Object of class `DC2` has no attribute `y` | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @dataclass(slots=False) | ||||||||||||||||||||||||||||||||||||||||||||||
| class DC3: | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1751,6 +1750,6 @@ class DC3: | |||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.x = 3 | ||||||||||||||||||||||||||||||||||||||||||||||
| # should error: y is not in slots | ||||||||||||||||||||||||||||||||||||||||||||||
| self.y = 3 | ||||||||||||||||||||||||||||||||||||||||||||||
| self.y = 3 # E: Object of class `DC3` has no attribute `y` | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| self.y = 3 # E: Object of class `DC3` has no attribute `y` | |
| self.y = 3 # E: Object of class `DC3` has no attribute `y` | |
| class DC2Child(DC2): | |
| def set_attr_via_super(self) -> None: | |
| super().__init__() | |
| # should error: y is not in slots of DC2 | |
| super().y = 3 # E: Object of class `DC2` has no attribute `y` | |
| def del_attr_via_super(self) -> None: | |
| # should error: y is not in slots of DC2 | |
| del super().y # E: Object of class `DC2` has no attribute `y` | |
| class DC3Child(DC3): | |
| def set_attr_via_super(self) -> None: | |
| super().__init__() | |
| # should error: y is not in slots of DC3 | |
| super().y = 3 # E: Object of class `DC3` has no attribute `y` | |
| def del_attr_via_super(self) -> None: | |
| # should error: y is not in slots of DC3 | |
| del super().y # E: Object of class `DC3` has no attribute `y` |
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.
class_for_slots_restrictiontreatsAttributeBase1::SuperInstance(cls, _)ascls.clone(), butclsis the start-lookup class for thesuper()proxy, not the underlying instance’s actual class (see howget_super_attributeusesSuperObj::Instance(obj)/SuperObj::Class(obj)). This will enforce__slots__against the wrong class and can incorrectly rejectsuper().attr = .../del super().attrwhenattris a slot on the real instance type but not on the start-lookup class. Use theSuperObjto derive the instanceClassType(and likely skip slots restriction forSuperObj::Class).