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
Incorrect ForwardRef class lookup in inherited models #6742
Comments
@samuelcolvin, any news on this issue? |
@JrooTJunior we are working on this issue and trying to provide a fix as soon as possible |
This is expected since Please see #6823 which proposes a fix solution for this - basically I've added a Please let me know asap if you're not happy with #6823, otherwise I'll try to get it included in the next release. |
Child model MUST look in current namespace only when detecting types for ITS fields. NOT parent fields. I understand that you might have some limitations on how you implemented this, but for provided example the expected result is quite obvious. Moreover if you get rid of Additionally, I expect that in here # fw_parent.py
from pydantic import BaseModel
class Model(BaseModel):
a: "A"
class A(BaseModel):
pass
Model.model_rebuild()
# fw_child.py
from pydantic import BaseModel
from fw_parent import Model
class A(BaseModel):
pass
class Child(Model):
aa: A
# main.py
from fw_parent import A as ParentA
from fw_child import A as ChildA, Child
Child(a=ParentA(), aa=ChildA()) |
I'm not totally understand why inheritance was broken in V2 because it works as well on V1. Why child class can't copy already resolved fields from the parent class? |
@JrooTJunior I agree it probably should. Let me investigate a bit and see if I can improve this. |
@Tishka17 I believe your code example behaves properly with #6838 — any feedback on whether it resolves the issues for the people following this thread would be appreciated. Note that even with #6838 you may need to explicitly call I suspect there will be a solution to this that will eliminate the need for that |
Looks ok for me, I cannot find an example how to break it. |
Initial Checks
Description
Let's suppose parent class has fields with
ForwardRef
and callsmodel_rebuild
. It correctly build.Let's imagine we have child class in different module. In this case for that class pydantic tries to lookup types of parent fields again, but uses current scope instead of scope of parent class.
In all files below it is expected that
Child.a
will have the typefw_parent.A
as it is declared. But it isn'tWhen running
main.py
the error is:It looks like pydantic tries to rebuild parent fields while it has info about them in parent class.
When running
main2.py
the error is:It looks like pydantic tries to use wrong A class from wrong file.
Example Code
Python, Pydantic & OS Version
Selected Assignee: @dmontagu
The text was updated successfully, but these errors were encountered: