typing.Annotated could be used to build dataclasses. Using Annotated will allow
libraries to add functionality to a dataclass without having to change
dataclass creation or behavior. The example below shows how a dataclass could
be implemented. It continues the example of struct2 shown in pep593. From a
dataclass point of view, the Sample and AnnotatedSample would be equivalent.
```python
@dataclass
class Sample:
a: int
b: int
c: int = field(default=5)
d: int = 10
e: int = field(default=10)
@packed
@dataclass
class AnnotatedSample:
a: Annotated[int, ctype("I")]
b: int
c: Annotated[int, field(default=5), ctype("I")]
d: Annotated[int, ctype("h")] = 10
e: Annotated[int, ctype("h")] = field(default=10)
out_bytes = struct2.pack(AnnotatedSample())
```
When parsing the Annotated parameters, the dataclass decorator will only look
at the type parameter and ```field``` parameter if present. If not Annotated,
it falls back to existing behavior.
Let me know what you think.
Thanks!
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TI4ZHEJSP5NWPO4NHR2EKNZQYLZMGR2J/
Code of Conduct: http://python.org/psf/codeofconduct/