-
-
Notifications
You must be signed in to change notification settings - Fork 29.4k
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
Cross-module dataclass inheritance breaks get_type_hints #89687
Comments
|
[I believe this is fundamentally a dataclass version of https://bugs.python.org/issue41249] When using This is best shown by example, if you have foo.py: and then in bar.py: the final line will raise "NameError: name 'collections' is not defined". This code will not error if you do either of the following:
I am not confident enough on the internals of dataclass to suggest a fix, but potentially a similar approach to that which solved the TypedDict equivalent https://bugs.python.org/issue41249 would work? |
|
I can't reproduce this on Python 3.8.3, 3.9.6, 3.10.0 or 3.11.0a1+. Which versions of Python have you tried this on? (I'm able to reproduce the |
|
I think the example has a minor typo. The crash is reproducible on 3.9 if the last line in bar.py is typing.get_type_hints(B.__init__)instead of typing.get_type_hints(B) |
|
Thanks @sergei. With that alteration, I have reproduced this on Python 3.9, 3.10 and 3.11. |
|
Ah yes, I completely apologize about that typo, the __init__ is definitely needed (serves me right for trying to clean up a repo before posting it). One other comment to make, perhaps obvious; manually passing a namespace to get_type_hints' |
|
I had some time to debug this. It happens because we treat classes and functions differently. The difference between
Funny thing, this problem goes away if we remove and I am going to try to solve this with something really simple (if no one else is working on it). |
|
@nikita, I had a go at writing some more rigorous tests regarding this issue. I found the same thing you did -- the issue seems:
My tests are in these two files on my cpython fork:
(I'm not proposing adding two new files to the cpython test suite -- just put the tests in new files so that I could isolate the new tests from the rest of the test suite and understand the problem better.) |
|
If you try running my test script with the |
|
Is it worth also addressing the case where a @dataclass/typing.TypeDict class is defined within a function? |
|
@sergei, I believe that's a much larger issue, and one that's quite difficult to solve. It's one of the principal reasons why https://mail.python.org/archives/list/python-dev@python.org/thread/CLVXXPQ2T2LQ5MP2Y53VVQFCXYWQJHKZ/ |
|
I completelly agree that the exable above is out of scope. Because it requires |
|
Is it worth filing a separate issue for locals()? In my experience local classes are less common than cross-module inheritance, so I suspect that the chances of someone accidentally hitting lack of locals() forwarding are quite low. However, given how confusing the error message is, it might be worth having an issue for that. Wdyt? |
|
In my opinion, it is never a bad thing to create a new issue :) сб, 23 окт. 2021 г. в 22:46, Sergei Lebedev <report@bugs.python.org>:
|
get_type_hintswith dataclasses__init__generation #29158Note: 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: