-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
Closed as not planned
Closed as not planned
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-dataclassestype-featureA feature request or enhancementA feature request or enhancement
Description
Dataclasses - Support use of Annotated including field as metainfo
dataclasses are great and I make extensive use of them but there is something which I always find non-matching. Example:
from dataclasses import dataclass, field
@dataclass
class Dummy:
a: int = field(init=False, default=5)Having declared a to be of type int the assignment is the result of a callable field which (as an end-user) I am not sure it is returning an int
Given that Annotated can convey metainformation, would it not be really appropriate to use it for dataclasses, as in:
from dataclasses import dataclass, field
from typing import Annotated
@dataclass
class Dummy
a: Annotated[int, field(init=False)] = 5This should be clearer for anyone:
- the type for
aisint - the information that it must not be part of
__init__is in the metainformation insideAnnotatedwith the use offield. - And the default value is assigned to
aas expected.
Pitch
It modernizes the use of field, allows assigned a default value which actually matches the defined type, rather than relying on type-checkers having workarounds to cover the case.
It does not remove the old syntax.
Previous discussion
https://discuss.python.org/t/dataclasses-make-use-of-annotated/30028
Linked PRs
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-dataclassestype-featureA feature request or enhancementA feature request or enhancement