-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
typing: get_type_hints on stringified lone ClassVar raises TypeError #90711
Comments
A stringified lone ClassVar raises at runtime, but this pattern is tested for in dataclasses unit tests and used in the wild. The PEP is not clear that it should or should not be used with arguments, and it works fine when not stringified. The fix for this is trivial and I can submit a patch if there's agreement. |
I think this is needed for moving dataclasses to using typing.py introspection tools to be viable. |
dataclasses is no doubt too lenient. But it's just trying to accept valid strings that look like ClassVar. Way back when this was initially implemented, we decided that calling get_type_hints would be too expensive for every dataclass, and would also neccesitate importing typing, which we didn't want to require. I think someone needs to do an analysis of how expensive it would be for dataclasses to import typing and to call get_type_hints. Perhaps we'd make a different decision today. |
I'm drafting an implementation for the purpose of investigating performance right now; I will share when ready. |
It seems acceptable to mypy. I'm not sure I like the agenda of "moving dataclasses to using typing.py introspection tools". |
It's acceptable to mypy, and pyright added support a few months ago when I made an issue and Eric Traut discovered this pattern in the wild too. Some of the other type checkers (pyre) still error I believe. My feeling is that since this has apparently become used in practice we should fix the runtime error for when it's stringified, but we don't necessarily need to prescribe that it's a legal type annotation for type checkers (?). If I was prescribing how to use ClassVar in a vacuum, I don't see any good typing reason we should prohibit this. re: moving dataclasses: I'm not sure either! I'm trying to look into the issues it brings up in practice to weigh against the difficulties of maintaining the existing bespoke introspection implementation and the problems it has (dealing with stringified annotations, supporting renaming the Annotated symbol). I made an implementation that fully moves dataclasses over to using
There's also nontechnical issues, like the potential politics of making dataclasses always import typing. I'll be updating bpo-46511 later with thoughts. |
Please just don't go there. I beg you. It's not worth it. |
I think I miscommunicated my intent with sentence placement. I already posted the thoughts I referred to; they're just my concluding opinion on the technical merit of using get_type_hints in dataclasses to solve the Annotated problem: https://bugs.python.org/msg411945 In any case, I apologize for the faux pas and I'll be careful in the future. I dug up the issue where pyright was changed (to allow bare ClassVar) that has an argument for not allowing it to be bare: microsoft/pyright#2377 |
I
|
Is there any reason to keep this issue open? The PR was merged. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: