-
Notifications
You must be signed in to change notification settings - Fork 263
add dataclass_transform #1054
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
add dataclass_transform #1054
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ def _check_generic(cls, parameters): | |
|
||
# One-off things. | ||
'Annotated', | ||
'dataclass_transform', | ||
'final', | ||
'IntVar', | ||
'is_typeddict', | ||
|
@@ -2341,3 +2342,82 @@ class Movie(TypedDict): | |
|
||
Required = _Required(_root=True) | ||
NotRequired = _NotRequired(_root=True) | ||
|
||
if hasattr(typing, 'dataclass_transform'): | ||
dataclass_transform = typing.dataclass_transform | ||
else: | ||
def dataclass_transform( | ||
*, | ||
eq_default: bool = True, | ||
order_default: bool = False, | ||
kw_only_default: bool = False, | ||
field_descriptors: typing.Tuple[typing.Any, ...] = (()), | ||
) -> typing.Callable[[T], T]: | ||
"""Decorator that marks a function, class, or metaclass as providing | ||
dataclass-like behavior. | ||
|
||
Example: | ||
|
||
from typing_extensions import dataclass_transform | ||
|
||
_T = TypeVar("_T") | ||
|
||
# Used on a decorator function | ||
@dataclass_transform() | ||
def create_model(cls: type[_T]) -> type[_T]: | ||
... | ||
return cls | ||
|
||
@create_model | ||
class CustomerModel: | ||
id: int | ||
name: str | ||
|
||
# Used on a base class | ||
@dataclass_transform() | ||
class ModelBase: ... | ||
|
||
class CustomerModel(ModelBase): | ||
id: int | ||
name: str | ||
|
||
# Used 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 decoorator can be used to customize this behavior: | ||
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- ``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_descriptors`` 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. | ||
|
||
See PEP 681 for details. | ||
|
||
""" | ||
def decorator(cls_or_fn): | ||
cls_or_fn.__dataclass_transform__ = { | ||
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. Do we need to use this pattern used in final? try:
f.__final__ = True
except (AttributeError, TypeError):
# Skip the attribute silently if it is not writable.
# AttributeError happens if the object has __slots__ or a
# read-only property, TypeError if it's a builtin class.
pass
return f 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. I was more careful there because we added the behavior later after the decorator was already in the wild, and I didn't want to accidentally break something. Here, we're adding a new decorator, so I think it makes sense to start with the simpler behavior and adjust it only if there's an actual need. |
||
"eq_default": eq_default, | ||
"order_default": order_default, | ||
"kw_only_default": kw_only_default, | ||
"field_descriptors": field_descriptors, | ||
} | ||
return cls_or_fn | ||
return decorator |
Uh oh!
There was an error while loading. Please reload this page.