Skip to content

Tries to parse non-pydantic metadata from Annotated as model data (and fails) #7833

@chbndrhnns

Description

@chbndrhnns

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When adding metadata to Annotated that pydantic does not understand, I expect it to ignore it. It seems, in the example, pydantic tries to parse the incoming data based on the Metadata model.

Here is the error:

tests/test_.py:13 (test_)
def test_():
        data = {"state": 2}
>       assert Model.model_validate(data)
E       pydantic_core._pydantic_core.ValidationError: 1 validation error for Model
E       state
E         Input should be a valid dictionary or instance of Metadata [type=model_type, input_value=2, input_type=int]
E           For further information visit https://errors.pydantic.dev/2.4/v/model_type

test_.py:16: ValidationError

PEP 593 states:

"If a library (or tool) encounters a typehint Annotated[T, x] and has no special logic for metadata x, it should ignore it and simply treat the type as T.|

I do not see this happening here.

Example Code

from typing import Annotated

from pydantic import BaseModel


class Metadata(BaseModel):
    ...


class Model(BaseModel):
    state: Annotated[int, Metadata()]


def test_():
    data = {"state": 2}
    assert Model.model_validate(data)

Python, Pydantic & OS Version

pydantic version: 2.4.2
        pydantic-core version: 2.10.1
          pydantic-core build: profile=release pgo=false
                 install path: /Users/cleancoder/Library/Caches/pypoetry/virtualenvs/xxx-sT27uenX-py3.9/lib/python3.9/site-packages/pydantic
               python version: 3.9.15 (main, Feb 20 2023, 14:56:22)  [Clang 14.0.0 (clang-1400.0.29.202)]
                     platform: macOS-13.4.1-arm64-arm-64bit
             related packages: email-validator-2.0.0.post2 pydantic-extra-types-2.1.0 typing_extensions-4.7.1 pydantic-settings-2.0.3 fastapi-0.103.2

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions