KEMBAR78
mypy fails to typecheck inherited model with a field alias · Issue #11009 · pydantic/pydantic · GitHub
Skip to content

mypy fails to typecheck inherited model with a field alias #11009

@koterpillar

Description

@koterpillar

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When a model:

  • Inherits another model
  • Overrides a field
  • Sets an alias for it

mypy fails to typecheck the result complaining about __replace__ taking the aliased field name but not the original field name.

I have found this issue updating a project for Python 3.13, there might be other issues with replace functionality but I'll open them separately.

Example Code

from pydantic import BaseModel, Field


class One(BaseModel):
    foo: str


class Two(One):
    foo: str = Field(..., alias="bar")

# Signature of "__replace__" incompatible with supertype "One"  [override]
#     Superclass:
#         def __replace__(*, <SNIP>, foo: str = ...) -> One
#     Subclass:
#         def __replace__(*, <SNIP>, bar: str = ...) -> Two

Python, Pydantic & OS Version

pydantic version: 2.10.2
        pydantic-core version: 2.27.1
          pydantic-core build: profile=release pgo=false
                 install path: /home/alex/.cache/pypoetry/virtualenvs/mybox-YUnsYrZx-py3.13/lib/python3.13/site-packages/pydantic
               python version: 3.13.0 (main, Oct  8 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
                     platform: Linux-6.11.8-300.fc41.x86_64-x86_64-with-glibc2.40
             related packages: mypy-1.13.0 typing_extensions-4.12.2
                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2pendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions