Skip to content

type casting does not work with multiple attributes of the same custom type #7102

@hiaselhans

Description

@hiaselhans

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I tried to upgrade a custom data type from pydantic v1 to v2.

It looks like the type casting works as long as there is only one attribute of the type assigned to a model.
When there are two, a validation error is raised:

ValidationError: 1 validation error for Test2
y
  Input should be a valid dictionary or instance of Numeric [type=model_type, input_value=2, input_type=int]
    For further information visit https://errors.pydantic.dev/2.1/v/model_type

I attached a minimal example down below.
Maybe the schema generation isn't done as it should be. I am happy to add some more documentation if someone guides me to the right solution for this example

Example Code

from typing import Any, Self
import pydantic
from pydantic_core import CoreSchema, core_schema

class Numeric(pydantic.BaseModel):
    value: float
    unit: str = "m"

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source_type: Any, handler: pydantic.GetCoreSchemaHandler
    ) -> CoreSchema:
        return core_schema.no_info_before_validator_function(cls.validate, handler(source_type))

    @classmethod
    def validate(cls, v: Any) -> dict[str, Any] | Self:
        if isinstance(v, (str, float, int)):
            return cls(value=v)
        elif isinstance(v, Numeric):
            if v.unit == cls.unit:
                return v
        elif isinstance(v, dict):
            return v

        raise ValueError(f"Invalid value for {cls}: {v}")

class Test1(pydantic.BaseModel):
    x: Numeric

class Test2(pydantic.BaseModel):
    x: Numeric
    y: Numeric

print("test1:", Test1(x=2))
print("test2:", Test2(x=2, y=2))

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=true mimalloc=true
                 install path: /home/xxx/.local/lib/python3.11/site-packages/pydantic
               python version: 3.11.3 (main, Jun  5 2023, 09:32:32) [GCC 13.1.1 20230429]
                     platform: Linux-6.4.7-arch1-1-x86_64-with-glibc2.37
     optional deps. installed: ['typing-extensions']

Selected Assignee: @Kludex

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions