Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

CoreSchema for stdlib dataclasses not always respected #12531

Open
Labels
awaiting author responseawaiting response from issue openerbug V2Bug related to Pydantic V2pendingIs unconfirmedtopic-types supportRelated to (custom/arbitrary) types support
@ottermata

Description

@ottermata

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

The core schema defined with__get_pydantic_core_schema__ on a stdlib dataclass does not always work, when the class is used a field in other types. Switching toBaseModel or even pydantics dataclasses fixes this, but is unfortunately not an option for me.
The issue also occurs if the second usage of the type is slightly different, likeFoo &list[Foo].

I encountered this when updating from version 2.10.6 to 2.11, but it also happens with 2.12.

Example Code

importdataclassesfrompprintimportpprintfrompydanticimportGetCoreSchemaHandler,TypeAdapterfrompydantic_coreimportcore_schema,CoreSchema@dataclasses.dataclassclassFoo:field:str@classmethoddef__get_pydantic_core_schema__(cls,source_type:CoreSchema,handler:GetCoreSchemaHandler    )->CoreSchema:returncore_schema.no_info_before_validator_function(lambda_: {"field":"overridden"},schema=handler(source_type),serialization=core_schema.plain_serializer_function_ser_schema(lambda_: {"field":"overridden"},info_arg=False,            ),        )@dataclasses.dataclassclassBar:a:Foob:Fooif__name__=='__main__':adapter=TypeAdapter(Bar)pprint(adapter.core_schema)print(adapter.dump_python(Bar(a=Foo(field="a"),b=Foo(field="b"),            )        )    )# {"a": {"field": "overridden"}, "b": {"field": "b"}}# -> should be both {"field": "overridden"}print(adapter.validate_python(            {"a": {"field":"a"},"b": {"field":"b"},            }        )    )# Bar(a=Foo(field='overridden'), b=Foo(field='b'))# -> should be both Foo(field='overridden')

Python, Pydantic & OS Version

pydantic version: 2.12.4        pydantic-core version: 2.41.5          pydantic-core build: profile=release pgo=false               python version: 3.13.9 (main, Oct 14 2025, 21:29:44) [Clang 20.1.4 ]                     platform: Linux-6.8.0-87-generic-x86_64-with-glibc2.39             related packages: typing_extensions-4.15.0 mypy-1.18.2 fastapi-0.121.2                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting author responseawaiting response from issue openerbug V2Bug related to Pydantic V2pendingIs unconfirmedtopic-types supportRelated to (custom/arbitrary) types support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp