Skip to content

Pydantic 2.14.0a1 bug for empty subclass #417

@Czaki

Description

@Czaki

Such test is failing with pydantic==2.14.0a1

def test_empty_subclass():
    class MyModel(EventedModel):
        a: int = 1
        b: str = "default"

    class MyModel2(MyModel):
        pass


    m1 = MyModel()
    assert m1.a == 1
    assert m1.b == "default"

    m2 = MyModel2()
    assert m2.a == 1
    assert m2.b == "default"

with:


    def __init__(_model_self_, **data: Any) -> None:
>       super().__init__(**data)
E       pydantic_core._pydantic_core.ValidationError: 2 validation errors for MyModel2
E       a
E         Field required [type=missing, input_value={}, input_type=dict]
E           For further information visit https://errors.pydantic.dev/2.14/v/missing
E       b
E         Field required [type=missing, input_value={}, input_type=dict]
E           For further information visit https://errors.pydantic.dev/2.14/v/missing

Further investigation shows that pedantic lost default values for both fields.

But this code pass:

def test_pydantic_empty_subclass_plain():
    class MyModel(BaseModel):
        a: int = 1
        b: str = "default"

    class MyModel2(MyModel):
        pass

    m2 = MyModel2()
    assert m2.a == 1
    assert m2.b == "default"

I tried to reproduce it with pure pydantic code but failed.

I found that the workaround is to add namespace.setdefault("__annotations__", {}) before cls = super().__new__(mcs, name, bases, namespace, **kwargs) in EventedMetaclass.__new__

also this code works:

def test_empty_subclass():
    class MyModel(EventedModel):
        a: int = 1
        b: str = "default"

    class MyModel2(MyModel):
        c: int = 1


    m1 = MyModel()
    assert m1.a == 1
    assert m1.b == "default"

    m2 = MyModel2()
    assert m2.a == 1
    assert m2.b == "default"

It was detected in napari pre tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions