Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-79413: Add module and qualname arguments to dataclasses.make_dataclass()#11371
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
Closed
remilapeyre
wants to merge
1
commit into
python:main
from
remilapeyre:make_dataclass-instances-should-be-pickable
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -8,6 +8,8 @@ | ||
| import functools | ||
| import _thread | ||
| # Used by the functionnal API when the calling module is not known | ||
| from enum import _make_class_unpicklable | ||
| __all__ = ['dataclass', | ||
| 'field', | ||
| @@ -1138,7 +1140,7 @@ def _astuple_inner(obj, tuple_factory): | ||
| def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, | ||
| repr=True, eq=True, order=False, unsafe_hash=False, | ||
| frozen=False): | ||
| frozen=False, module=None, qualname=None): | ||
| """Return a new dynamically created dataclass. | ||
| The dataclass name will be 'cls_name'. 'fields' is an iterable | ||
| @@ -1158,6 +1160,14 @@ class C(Base): | ||
| For the bases and namespace parameters, see the builtin type() function. | ||
| 'module' should be set to the module this class is being created in; if it | ||
| is not set, an attempt to find that module will be made, but if it fails the | ||
| class will not be picklable. | ||
| 'qualname' should be set to the actual location this call can be found in | ||
| its module; by default it is set to the global scope. If this is not correct, | ||
remilapeyre marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| pickle will fail in some circumstances. | ||
| The parameters init, repr, eq, order, unsafe_hash, and frozen are passed to | ||
| dataclass(). | ||
| """ | ||
| @@ -1198,6 +1208,22 @@ class C(Base): | ||
| # We use `types.new_class()` instead of simply `type()` to allow dynamic creation | ||
| # of generic dataclassses. | ||
| cls = types.new_class(cls_name, bases, {}, lambda ns: ns.update(namespace)) | ||
| # TODO: this hack is the same that can be found in enum.py and should be | ||
| # removed if there ever is a way to get the caller module. | ||
| if module is None: | ||
| try: | ||
| module = sys._getframe(1).f_globals['__name__'] | ||
| except (AttributeError, ValueError): | ||
| pass | ||
| if module is None: | ||
| _make_class_unpicklable(cls) | ||
| else: | ||
| cls.__module__ = module | ||
remilapeyre marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if qualname is not None: | ||
| cls.__qualname__ = qualname | ||
| return dataclass(cls, init=init, repr=repr, eq=eq, order=order, | ||
| unsafe_hash=unsafe_hash, frozen=frozen) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3 Misc/NEWS.d/next/Library/2018-12-30-21-00-56.bpo-35232.yMfv98.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| `dataclasses.make_dataclass` accepts two new keyword arguments `module` and | ||
| `qualname` in order to make created classes picklable. Patch contributed by | ||
| Rémi Lapeyre. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.