forked from python/peps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -203,6 +203,7 @@ customization of default behaviors: | |
eq_default: bool = True, | ||
order_default: bool = False, | ||
kw_only_default: bool = False, | ||
transform_descriptor_types: bool = False, | ||
field_descriptors: tuple[type | Callable[..., Any], ...] = (), | ||
) -> Callable[[_T], _T]: ... | ||
|
@@ -218,6 +219,10 @@ customization of default behaviors: | |
assumed to be True or False if it is omitted by the caller. If not | ||
specified, ``kw_only_default`` will default to False (the default | ||
assumption for dataclass). | ||
* ``transform_descriptor_types`` indicates whether, for fields whose | ||
annotated types are descriptors, the corresponding parameter in the | ||
synthesized ``__init__`` method should be the descriptor's setter | ||
parameter type. If False, the descriptor type will be used. | ||
This comment has been minimized.
Sorry, something went wrong. |
||
* ``field_descriptors`` specifies a static list of supported classes | ||
that describe fields. Some libraries also supply functions to | ||
allocate instances of field descriptors, and those functions may | ||
|
@@ -326,6 +331,31 @@ Metaclass example | |
id: int | ||
name: str | ||
``transform_descriptor_types`` example | ||
`````````````````````````````````````` | ||
|
||
Because ``transform_descriptor_types`` is set to ``True``, the | ||
``target`` parameter on the synthesized ``__init__`` method will be of | ||
type ``float`` instead of ``Descriptor``. | ||
|
||
.. code-block:: python | ||
class Descriptor: | ||
This comment has been minimized.
Sorry, something went wrong.
erictraut
|
||
def __get__(self, instance: object, owner: Any) -> int: | ||
... | ||
# The setter and getter can have different types (asymmetric). | ||
# The setter's value type is used for the __init__ parameter. | ||
# The getter's return type is ignored. | ||
def __set__(self, instance: object, value: float): | ||
... | ||
class CustomerModel( | ||
ModelBase, | ||
transform_descriptor_types=True | ||
This comment has been minimized.
Sorry, something went wrong.
erictraut
|
||
): | ||
target: Descriptor | ||
Field descriptors | ||
----------------- | ||
|
@@ -449,6 +479,7 @@ For example: | |
"eq_default": True, | ||
"order_default": False, | ||
"kw_only_default": False, | ||
"transform_descriptor_types": False, | ||
"field_descriptors": () | ||
} | ||
|
@@ -556,6 +587,7 @@ following declaration within their type stubs or source files: | |
eq_default: bool = True, | ||
order_default: bool = False, | ||
kw_only_default: bool = False, | ||
transform_descriptor_types: bool = False, | ||
field_descriptors: tuple[type | Callable[..., Any], ...] = (), | ||
) -> Callable[[_T], _T]: | ||
# If used within a stub file, the following implementation can | ||
|
@@ -676,6 +708,18 @@ users of Django would need to explicitly declare the ``id`` field. | |
This limitation may make it impractical to use the | ||
``dataclass_transform`` mechanism with Django. | ||
|
||
Class-wide default values | ||
------------------------- | ||
|
||
SQLAlchemy requested that we expose a way to specify that the default | ||
value of all fields in the transformed class is None. It is typical | ||
that all of their fields are optional and None indicates that the | ||
field is not set. | ||
|
||
We chose not to support this feature, since it is specific to | ||
SQLAlchemy. Users can manually set ``default=None`` on these fields | ||
instead. | ||
|
||
Open Issues | ||
=========== | ||
|
||
|
This is a good description and a reasonable name. It should also mention the default value, which should be False, since that corresponds to the behavior of
dataclass
.