Enable pydantic_model_creator to use Forward references using ReverseRelation in different files#1842
Enable pydantic_model_creator to use Forward references using ReverseRelation in different files#1842eyllanesc-JE wants to merge 2 commits intotortoise:developfrom
Conversation
CodSpeed Performance ReportMerging #1842 will not alter performanceComparing Summary
|
Pull Request Test Coverage Report for Build 12632106944Details
💛 - Coveralls |
|
@eyllanesc-JE, could you please explain why setting |
|
@markus-96 any chance you can weight in here since you contributed to the pydantic integration recently? |
|
@henadzit maybe next week, too busy right now... But what I can say is that I did not encounter this bug yet, although I am working on a project right now that makes heavy use of defining models in different packages. Also, please take the comment into account I made in the corresponding issue: #1841 (comment) |
By default if the global namespace is not set then the mro is used where the elements such as the fields indicated in the bug are defined. See the docs:
By overriding global namespace then get_type_hints does not have access to properties of the Model namespace. But only to the new models themselves. In my solution using locals namespace I am adding elements that typing.get_type_hints does not know about, such as models in other files. Another solution could be that pydantic_model_creator can be passed the locals and globals namespace giving the programmer more freedom to see which elements get_type_hints needs. |
|
ok, since from typing import TYPE_CHECKING
from typing_extensions import TypeVar
from tortoise import fields
from tortoise import Model
if TYPE_CHECKING:
from models.foo import Foo
else:
Foo = TypeVar('Foo', bound=Model)
class Bar(Model):
foo: fields.ForeignKeyRelation['Foo'] = fields.ForeignKeyField("models.Foo") |
Description
Motivation and Context
Fixed #1841
How Has This Been Tested?
Checklist: