Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.1k
Add runtime__slots__ attribute to dataclasses#15649
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -469,9 +469,15 @@ def add_slots( | ||
| self._cls, | ||
| ) | ||
| return | ||
| info.slots = generated_slots | ||
| # Now, insert `.__slots__` attribute to class namespace: | ||
| slots_type = TupleType( | ||
| [self._api.named_type("builtins.str") for _ in generated_slots], | ||
Member 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. Is it worth creating Literal types here? So the inferred type in the test cases would be MemberAuthor 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. No, because regular classes produce the same result: classA:__slots__= ('a','b')reveal_type(A.__slots__)# N: Revealed type is "tuple[builtins.str, builtins.str]"
Member 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. Thanks, I feel like we might get an issue at some point asking that we use the literal types, but it's fine if we're consistent with how explicit | ||
| self._api.named_type("builtins.tuple"), | ||
| ) | ||
| add_attribute_to_class(self._api, self._cls, "__slots__", slots_type) | ||
| def reset_init_only_vars(self, info: TypeInfo, attributes: list[DataclassAttribute]) -> None: | ||
| """Remove init-only vars from the class and reset init var declarations.""" | ||
| for attr in attributes: | ||