[New-bugs-announce] [issue46511] dataclasses: Allow typing.Annotated to wrap dataclasses-specific annotations

Gregory Beauregard report at bugs.python.org
Tue Jan 25 04:02:48 EST 2022


New submission from Gregory Beauregard <greg at greg.red>:

In https://bugs.python.org/issue46491 the typing runtime behavior was changed so that `Annotated[Classvar[...]]` is now valid at runtime in order to alleviate tension between typing and non-typing annotation space uses. dataclasses.py should likely follow suit in its runtime use of `ClassVar` and `InitVar`.

Reviewing the code I see two areas that would need addressed:

1) `InitVar` needs changed so `Annotated[InitVar[...]]` is no longer a runtime error. This is currently a runtime error completely by accident: typing.py expects special type forms to be `callable()`, usually by implementing a `__call__` that throws an error, but `InitVar` does not implement this. Adding an  implementation like in typing.py would fix the runtime error:
https://github.com/python/cpython/blob/b1a3446f077b7d56b89f55d98dadb8018986a3e5/Lib/typing.py#L391-L392

2) The dataclasses-specific typehint introspection implementation needs modified to accommodate being wrapped by an `Annotated` annotation. I see in the comments the code is performance sensitive so I'm not sure what you want to do; f.ex. the regex needs modified, but it's not clean.

What are your thoughts?

----------
components: Library (Lib)
messages: 411567
nosy: GBeauregard, Jelle Zijlstra, eric.smith
priority: normal
severity: normal
status: open
title: dataclasses: Allow typing.Annotated to wrap dataclasses-specific annotations
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46511>
_______________________________________


More information about the New-bugs-announce mailing list