Skip to content
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

[BUG] PatchDict errors with inherited schemas #1324

Open
filippomc opened this issue Oct 22, 2024 · 2 comments
Open

[BUG] PatchDict errors with inherited schemas #1324

filippomc opened this issue Oct 22, 2024 · 2 comments

Comments

@filippomc
Copy link

filippomc commented Oct 22, 2024

Describe the bug

I have a schema hierarchy such as:

class ViewableContent(Schema):
    name: str
    description: str = None

class MySchema(ViewableContent):
   other: str # If I don't add a new field the problem does not arise

Then add a router like the following:

@router.patch('/{uuid}', response={200: MySchema})
@transaction.atomic
def my_update(request: HttpRequest, uuid: str, payload: PatchDict[MySchema]):
   ...

When I run my application the following error is raised:

  File "/home/user/mnp/applications/neuroglass-research/backend/neuroglass_research/api/__init__.py", line 4, in <module>
    from .studies import router as studies_router
  File "/home/user/mnp/applications/neuroglass-research/backend/neuroglass_research/api/studies.py", line 69, in <module>
    def update_study(request: HttpRequest, study_id: int, payload: PatchDict[UpdateStudyPayload]):
                                                                   ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/mnp/lib/python3.12/site-packages/ninja/patch_dict.py", line 45, in __getitem__
    new_cls = create_patch_schema(schema_cls)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/user/miniconda3/envs/mnp/lib/python3.12/site-packages/ninja/patch_dict.py", line 29, in create_patch_schema
    t = schema_cls.__annotations__[f]
        ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
KeyError: 'name'

Versions:

  • Python version: 3.12
  • Django version: 5.1.2
  • Django-Ninja version: 1.3.0
  • Pydantic version: 2.9.2
@filippomc
Copy link
Author

filippomc commented Oct 22, 2024

Possibly related discussion: pydantic/pydantic#4242

@vitalik
Copy link
Owner

vitalik commented Oct 22, 2024

If I don't add a new field the problem does not arise

so problem only appears when you ADD some field ? or always ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants