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-91860: Add typing.dataclass_transform (PEP 681)#91861
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
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 |
|---|---|---|
| @@ -123,6 +123,7 @@ def _idfunc(_, x): | ||
| 'assert_never', | ||
| 'cast', | ||
| 'clear_overloads', | ||
| 'dataclass_transform', | ||
| 'final', | ||
| 'get_args', | ||
| 'get_origin', | ||
| @@ -3265,3 +3266,81 @@ def reveal_type(obj: T, /) -> T: | ||
| """ | ||
| print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr) | ||
| return obj | ||
| def dataclass_transform( | ||
| *, | ||
| eq_default: bool = True, | ||
| order_default: bool = False, | ||
| kw_only_default: bool = False, | ||
| field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (), | ||
| **kwargs: Any, | ||
| ) -> Callable[[T], T]: | ||
| """Decorator that marks a function, class, or metaclass as providing | ||
| dataclass-like behavior. | ||
| Example usage with a decorator function: | ||
| _T = TypeVar("_T") | ||
| @dataclass_transform() | ||
| def create_model(cls: type[_T]) -> type[_T]: | ||
| ... | ||
| return cls | ||
| @create_model | ||
| class CustomerModel: | ||
| id: int | ||
| name: str | ||
| On a base class: | ||
| @dataclass_transform() | ||
| class ModelBase: ... | ||
| class CustomerModel(ModelBase): | ||
| id: int | ||
| name: str | ||
| On a metaclass: | ||
| @dataclass_transform() | ||
| class ModelMeta(type): ... | ||
| class ModelBase(metaclass=ModelMeta): ... | ||
| class CustomerModel(ModelBase): | ||
| id: int | ||
| name: str | ||
| Each of the ``CustomerModel`` classes defined in this example will now | ||
| behave similarly to a dataclass created with the ``@dataclasses.dataclass`` | ||
| decorator. For example, the type checker will synthesize an ``__init__`` | ||
| method. | ||
| The arguments to this decorator can be used to customize this behavior: | ||
| - ``eq_default`` indicates whether the ``eq`` parameter is assumed to be | ||
| True or False if it is omitted by the caller. | ||
| - ``order_default`` indicates whether the ``order`` parameter is | ||
| assumed to be True or False if it is omitted by the caller. | ||
| - ``kw_only_default`` indicates whether the ``kw_only`` parameter is | ||
| assumed to be True or False if it is omitted by the caller. | ||
| - ``field_specifiers`` specifies a static list of supported classes | ||
| or functions that describe fields, similar to ``dataclasses.field()``. | ||
| At runtime, this decorator records its arguments in the | ||
| ``__dataclass_transform__`` attribute on the decorated object. | ||
JelleZijlstra marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| It has no other runtime effect. | ||
| See PEP 681 for more details. | ||
| """ | ||
| def decorator(cls_or_fn): | ||
| cls_or_fn.__dataclass_transform__ = { | ||
| "eq_default": eq_default, | ||
| "order_default": order_default, | ||
| "kw_only_default": kw_only_default, | ||
| "field_specifiers": field_specifiers, | ||
| "kwargs": kwargs, | ||
| } | ||
| return cls_or_fn | ||
| return decorator | ||
2 changes: 2 additions & 0 deletions
2 Misc/NEWS.d/next/Library/2022-04-23-08-06-36.gh-issue-91860.ityDjK.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,2 @@ | ||
| Add :func:`typing.dataclass_transform`, implementing :pep:`681`. Patch by | ||
| Jelle Zijlstra. |
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.