-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
[mypyc] Support generating extension classes for dataclasses #7817
Conversation
The approach here is to generate an extension class while also populating a dictionary with the appropriate fields for the dataclass constructor. Once the extension class is created, we swap out the contents of its `__dict__` with the `__dict__` that `dataclass` expects, invoke `dataclass`, and then put back all the original stuff. Fixes #671.
934c0e2
to
100d868
Compare
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.
Great, better dataclass support is important for Python 3.7+ programs, where dataclasses can be expected to be ubiquitous. Most of my comments are about more documentation, generally looks good (though it's kind of hacky).
|
||
with assertRaises(TypeError, "missing 2 required positional arguments"): | ||
Person1b() | ||
|
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.
What about testing that the class behaves like an extension class? Maybe try to monkeypatch and try to assign a value with an invalid type to an attribute (through attribute assignmend and constructor).
mypyc/genops.py
Outdated
@@ -1459,6 +1460,12 @@ def dataclass_finalize( | |||
(which will be descriptors) with whatever they would be in a | |||
non-extension class, calling dataclass, then switching them back. | |||
|
|||
The resulting class is an extension class and instances of it do not | |||
does not have a __dict__ (unless something else requires it). |
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.
Remove "does not"
The approach here is to generate an extension class while also
populating a dictionary with the appropriate fields for the dataclass
constructor. Once the extension class is created, we swap out the
contents of its
__dict__
with the__dict__
thatdataclass
expects, invoke
dataclass
, and then put back all the original stuff.Fixes #671.