Skip to content
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

Merged
merged 4 commits into from
Oct 30, 2019

Conversation

msullivan
Copy link
Collaborator

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.

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.
Copy link
Collaborator

@JukkaL JukkaL left a 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).

mypyc/genops.py Outdated Show resolved Hide resolved
mypyc/genops.py Outdated Show resolved Hide resolved
mypyc/genops.py Outdated Show resolved Hide resolved
mypyc/genops.py Show resolved Hide resolved
mypyc/genops.py Show resolved Hide resolved
mypyc/lib-rt/CPy.h Outdated Show resolved Hide resolved
mypyc/lib-rt/CPy.h Outdated Show resolved Hide resolved
mypyc/lib-rt/CPy.h Show resolved Hide resolved
mypyc/ops_misc.py Show resolved Hide resolved

with assertRaises(TypeError, "missing 2 required positional arguments"):
Person1b()

Copy link
Collaborator

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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove "does not"

@msullivan msullivan merged commit 91be66f into master Oct 30, 2019
@msullivan msullivan deleted the dataclasses branch October 30, 2019 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make tuple types subtypes of Sized, Container, Reversible, etc.
3 participants