-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
dataclasses
: Synthetic __init__
method on dataclass has broken ClassVar
annotation under some conditions.
#133956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This isn't just about the annotations, the dataclass also generates the wrong parameters. The
dataclasses does some complicated dance to identify My general recommendation though is to not use |
cc @ericvsmith @carljm for dataclasses |
Not to distract from the topic at hand, but can you elaborate on this advice? Not using stringified forward references can impose a lot of additional positioning constraints on code. I already have to dance around the fact that the values, which are bound to I have seen comments about how a change in Python 3.14+ is supposed to fix this, but that is years away from being a baseline version in terms of what needs to be supported. What is the advice in the meantime? (For the use case which triggered the original report, I am just going to fall back to |
Manually add strings only for specific types that need it. |
Fair. That is cognitive overhead during development, but... if one has a type checker's language server hooked up to their development environment, then it is probably bearable. |
I'd prefer not to add workarounds in |
I've had a chance to look at the code and the old b.p.o. issue mentioned in the comments. While this does give me some sympathy for your position, it really does not change the fact that this is a sharp edge on which developers can easily slice their hands. If I'm someone who has not inspected the internals and historical discussions of
Also, this jumped out at me while reading the old discussion:
What happened to the Also, was the performance difference between a regex match and an
If multiple CPython maintainers are saying do not use |
I think the particular issue here is probably fixable, something like this:
If someone is interested in confirming this works, writing tests, and putting up a PR, I'd be happy to review it.
PEP-649, which already said that the future import would be deprecated, was accepted two years ago. PEP 749 merely made the deprecation more concrete. |
I'd like to work on this |
@dzherb thanks! You can experiment with #133956 (comment) and add tests for this case. Probably, some other corner-cases can be found :) |
Yes, looks good. This is what I had in mind when I wrote "if that name points to a module, inspect that module's
Thanks. Go for it. If you don't get to it in a few days, then I can pick it up. Not sure what the policy is on back-porting fixes, but it would be nice to see this in the next patch/micro releases of 3.9 - 3.13 too. |
We can backport fixes to 3.13 and 3.14 only :( |
…ther than 'typing_extensions.ClassVar'. (python/cpython#133956)
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
When using automatically-stringified annotations (
from __future__ import annotations
),typing_extensions.ClassVar
is not treated the same astyping.ClassVar
even though those two objects are identical. Specifically, thedataclasses.dataclass
-produced__init__
method does not excludetyping_extensions.ClassVar
-wrapped variables from its annotations, but does excludetyping.ClassVar
-wrapped variables from its annotations. Among other problems, this causestyping.get_type_hints
to choke.Reproducer below:
This results in the following output:
(Credit to @Daraan for helping me pinpoint the problem.)
This is reproducible on both Python 3.10 and 3.13.
Possibly related to #89687.
CPython versions tested on:
3.10, 3.13
Operating systems tested on:
No response
Linked PRs
ClassVar
string annotation in@dataclass
caused incorrect__init__
generation #134073The text was updated successfully, but these errors were encountered: