-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
NamedTuple doesn't resolve postponed annotations #2760
Comments
Hi @jameysharp |
Oh, that's interesting. It doesn't work, but now I know things I didn't before. 😁 If I call If I call it after declaring the Pydantic model, then I get a different error (and a similar result in the real application I stripped this down from): So it doesn't seem to be resolving the names using the correct global scope. My current hypothesis is that the same thing is happening at model definition time, but the error is hidden then because it's indistinguishable from a forward reference, even though this isn't actually a forward reference. By contrast, declaring the So I think there are multiple bugs here:
|
Yes it is added of course by pydantic once used in a BaseModel. A plain named tuple does not have this attribute so you need to call it after declaring your model. |
But |
Yep I know I meant "we can forward more than just the module" sorry if I was not clear. |
Thanks to @PrettyWood for pointing me to the right place to fix this! Since I was told that both NamedTuple and TypedDict use the same __pydantic_model__ machinery that dataclasses do, I checked and found that TypedDict had the same bug, and fixed that too. Tests for both issues are included, which fail without the associated fixes being applied.
Thanks to @PrettyWood for pointing me to the right place to fix this! Since I was told that both NamedTuple and TypedDict use the same __pydantic_model__ machinery that dataclasses do, I checked and found that TypedDict had the same bug, and fixed that too. Tests for both issues are included, which fail without the associated fixes being applied.
Thanks for reporting and for the PR, I'll review it now. On the general issue of postponed annotations, #2678 is relevant. It looks like |
Just ran into this as well. The PR has been sitting for a couple months now. Any chance we can get that merged? |
Thanks to @PrettyWood for pointing me to the right place to fix this! Since I was told that both NamedTuple and TypedDict use the same __pydantic_model__ machinery that dataclasses do, I checked and found that TypedDict had the same bug, and fixed that too. Tests for both issues are included, which fail without the associated fixes being applied.
Thanks to @PrettyWood for pointing me to the right place to fix this! Since I was told that both NamedTuple and TypedDict use the same __pydantic_model__ machinery that dataclasses do, I checked and found that TypedDict had the same bug, and fixed that too. Tests for both issues are included, which fail without the associated fixes being applied.
Checks
Bug
Output of
python -c "import pydantic.utils; print(pydantic.utils.version_info())"
:Here's the smallest program I could come up with that fails under
from __future__ import annotations
but works without it:The call to
parse_obj
raises this exception:pydantic.errors.ConfigError: field "v" not yet prepared so type is still a ForwardRef, you might need to call Tup.update_forward_refs().
But there is noupdate_forward_refs
onNamedTuple
instances.Just using a base
int
in theNamedTuple
doesn't exhibit the same problem, but of course it also doesn't check the constraints I want.My current workaround is to just not use
from __future__ import annotations
in the module where I declare and use theNamedTuple
, but I was hoping to use the new style consistently throughout the project.The text was updated successfully, but these errors were encountered: