Skip to content

Commit dfdf510

Browse files
authored
PEP 681: kw_only field descriptor parameter is supported (#2381)
* kw_only field descriptor parameter is supported * Sync features with dataclass
1 parent 6f9c247 commit dfdf510

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

pep-0681.rst

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ certain features or make small adjustments to the way they use them.
6464
That's already true for the Mypy custom plugins, which
6565
don't support every feature of every dataclass-like library.
6666

67+
As new features are added to dataclass in the future, we intend, when
68+
appropriate, to add support for those features on
69+
``dataclass_transform`` as well. Keeping these two feature sets in
70+
sync will make it easier for dataclass users to understand and use
71+
``dataclass_transform`` and will simplify the maintenance of dataclass
72+
support in type checkers.
6773

6874
Specification
6975
=============
@@ -185,8 +191,8 @@ the bool value (``True`` or ``False``) to be statically evaluated.
185191
* ``eq``. ``order``, ``frozen``, ``init`` and ``unsafe_hash`` are parameters
186192
supported in the stdlib dataclass, with meanings defined in :pep:`557 <557#id7>`.
187193
* ``hash`` is an alias for the ``unsafe_hash`` parameter.
188-
* ``kw_only`` and ``slots`` are parameters supported in the stdlib dataclass,
189-
first introduced in Python 3.10.
194+
* ``kw_only``, ``match_args`` and ``slots`` are parameters supported
195+
in the stdlib dataclass, first introduced in Python 3.10.
190196

191197
``dataclass_transform`` parameters
192198
----------------------------------
@@ -366,6 +372,11 @@ descriptor classes typically use common parameter names to construct
366372
these field descriptors. This specification formalizes the names and
367373
meanings of the parameters that must be understood for static type
368374
checkers. These standardized parameters must be keyword-only.
375+
376+
These parameters are a superset of those supported by
377+
``dataclasses.field``, excluding those that do not have an impact on
378+
type checking such as ``compare`` and ``hash``.
379+
369380
Field descriptor classes are allowed to use other
370381
parameters in their constructors, and those parameters can be
371382
positional and may use other names.
@@ -386,6 +397,12 @@ positional and may use other names.
386397
* ``factory`` is an alias for ``default_factory``. Stdlib dataclasses
387398
use the name ``default_factory``, but attrs uses the name ``factory``
388399
in many scenarios, so this alias is necessary for supporting attrs.
400+
* ``kw_only`` is an optional bool parameter that indicates whether the
401+
field should be marked as keyword-only. If true, the field will be
402+
keyword-only. If false, it will not be keyword-only. If unspecified,
403+
the value of the ``kw_only`` parameter on the object decorated with
404+
``dataclass_transform`` will be used, or if that is unspecified, the
405+
value of ``kw_only_default`` on ``dataclass_transform`` will be used.
389406
* ``alias`` is an optional str parameter that provides an alternative
390407
name for the field. This alternative name is used in the synthesized
391408
``__init__`` method.
@@ -612,13 +629,6 @@ a ``cmp`` parameter, since it only applies to attrs. Attrs users
612629
should use the dataclass-standard ``eq`` and ``order`` parameter names
613630
instead.
614631

615-
``kw_only`` field descriptor parameter
616-
--------------------------------------
617-
618-
The attrs library supports a ``kw_only`` parameter for individual
619-
fields. We chose not to support a ``kw_only`` parameter, since it is
620-
specific to attrs.
621-
622632
Automatic field name aliasing
623633
-----------------------------
624634

@@ -629,7 +639,6 @@ underscore from the name of the corresponding ``__init__`` parameter.
629639
This proposal omits that behavior since it is specific to attrs. Users
630640
can manually alias these fields using the ``alias`` parameter.
631641

632-
633642
Alternate field ordering algorithms
634643
-----------------------------------
635644

0 commit comments

Comments
 (0)