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

TaggedDict multiple inheritance is failing Pydantic validation #1869

Open
ketozhang opened this issue Nov 18, 2024 · 1 comment
Open

TaggedDict multiple inheritance is failing Pydantic validation #1869

ketozhang opened this issue Nov 18, 2024 · 1 comment

Comments

@ketozhang
Copy link

Description of the problem

Using pydantic for deserialization, the ASDF serialized node of the YAML tree comes in as a asdf.tagged.TaggedDict. Pydantic is having trouble validating this as if it's a regular dictionary. I identified this being due to TaggedDict having multiple-inheritance to both UserDict and dict. I have not seen examples where this is the intended implementation for custom dicts.

Example of the problem

from collections import UserDict

from asdf.tagged import TaggedDict
from pydantic import BaseModel


class Bar(BaseModel):
    a: int


class Foo(BaseModel):
    bar: Bar


Foo.model_validate({"bar": {"a": 1}})
Foo.model_validate({"bar": TaggedDict({"a": 1})})
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
bar.a
  Field required [type=missing, input_value={'a': 1}, input_type=TaggedDict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing

Success on simplest single-inheritance.

class SimpleDict(UserDict): ...

Foo.model_validate({"bar": SimpleDict({"a": 1})})

Fails on simplest multi-inheritance

class MultiInheritanceDict(UserDict, dict): ...

Foo.model_validate({"bar": MultiInheritanceDict({"a": 1})})
pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
bar.a
  Field required [type=missing, input_value={'a': 1}, input_type=MultiInheritanceDict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing

System information

asdf version: 3.5.0
python version: 3.12.7
operating system: OSX 14.6.1

@ketozhang ketozhang changed the title TaggedDict multiple inheritance is causing issues TaggedDict multiple inheritance is causing issues with Pydantic Nov 18, 2024
@ketozhang ketozhang changed the title TaggedDict multiple inheritance is causing issues with Pydantic TaggedDict multiple inheritance is failing Pydantic validation Nov 18, 2024
@ketozhang
Copy link
Author

ketozhang commented Nov 18, 2024

I don't use TaggedDict directly with pydantic, so I don't suggest this needs to be fixed. This is likely relevant for pydantic to solve.

However, I do question the multiple inheritance here. A cursory glance, it's not typical to see UserDict inheriting dict.

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

1 participant