-
-
Notifications
You must be signed in to change notification settings - Fork 481
[BUG] PatchDict errors with inherited schemas #1324
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
Comments
Possibly related discussion: pydantic/pydantic#4242 |
so problem only appears when you ADD some field ? or always ? |
If I don't add a field it's like Pydantic is considering the two classes equivalent and what I see is that the annotations are the same from the base class. When adding a field, that field is the only one within the annotations dict. |
well I still do not understand you.. can you show two examples working and non working or something... |
The not working example is the one I originally posted, and the application breaks on any path with the error above. Working -- no inheritance: class MySchema(Schema):
name: str
description: str = None
other: str Working -- duplicate all base class fields class ViewableContent(Schema):
name: str
description: str = None
class MySchema(ViewableContent):
name: str = None
description: str = None
other: str Working -- subclassing but no new fields class ViewableContent(Schema):
name: str
description: str = None
class MySchema(ViewableContent):
pass Created an example project with all the examples |
Hi I have encountered the same bug with inherited Schemas The exception originates from the following method, where it uses the django-ninja/ninja/patch_dict.py Lines 26 to 40 in 49f284a
In line 29 sometimes it cannot find the root Schema fields, I think is related with how python lazy-create and resolves If no field is added to leaf Schema/Class no If a field or more are added to leaf Schema/Class then the created In my project, I temporarily fix the issue using the suggest approach to get fields types in this pydantic discussion: from typing import get_type_hints
//(...)
def fix_create_patch_schema(schema_cls: Type[Any]) -> Type[ModelToDict]:
values, annotations = {}, {}
schema_cls_type_hints = get_type_hints(schema_cls)
for f in schema_cls.__fields__.keys():
t = schema_cls_type_hints[f]
if not is_optional_type(t):
values[f] = getattr(schema_cls, f, None)
annotations[f] = Optional[t]
values["__annotations__"] = annotations
OptionalSchema = type(f"{schema_cls.__name__}Patch", (schema_cls,), values)
class OptionalDictSchema(ModelToDict):
_wrapped_model = OptionalSchema
_wrapped_model_dump_params = {"exclude_unset": True}
return Body[OptionalDictSchema] Given my lack of knowledge about the Django-ninja structure. Do you think is a good solution ? Should I make a PR ? |
Any update on this? |
Describe the bug
I have a schema hierarchy such as:
Then add a router like the following:
When I run my application the following error is raised:
Versions:
The text was updated successfully, but these errors were encountered: