Backtestspydanticrun-20261007-130130#11025

Issue #11025 Merged fix PR #13442 Run 7 Oct 2026 · run-20261007-130130 pydantic/pydantic

Mypy internal error when subclassing a generic model with a recursive type variable

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

Both patches fix the crash the same way: they check `has_placeholder(field.type)` in the transform path, call `self._api.defer()` when this is not the final iteration, and return False. This stops `serialize()` from being called on unresolved placeholder types, which is the root cause in the traceback. Correctness on the reported scenario is therefore equivalent.

The differences are in scope and tests. Patch B folds the check into the existing `field.type is None` branch, so it now also defers in the None case, which previously returned False without deferring. That is a behavior change beyond the issue. It is probably harmless, but it adds risk. Patch A applies the defer only to the placeholder case, which is narrower and clearer.

On tests, Patch A reproduces the issue (`Child(ElementWithChildren['Child'])`). It also checks an `Optional[ElementT]` field and a forward reference to a class defined later (`EarlyElement` / `LateElement`), which exercises the deferral more broadly. Patch B tests only the basic self-reference case, and its test contains an odd `"""<link>"""` docstring that looks like leftover junk. Patch A wins on test coverage, tighter scope and craft.

The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.

Solve

11m 8s

Wall-clock time in the container

Full test suite

No new failures

12,253 passed · 501 skipped · 12,754 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

40vs 27 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using mypy with the pydantic.mypy plugin and a pydantic model that:

  • inherits from a generic model with a TypeVar bound to a BaseModel subclass,
  • has a self-referential type hint (e.g., Child referencing itself through Generic[_T]).

mypy crashes with the following output:

/home/reznak/Documents/Projects/svglab/svglab/bug.py:17: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.13.0
Traceback (most recent call last):
  File "mypy/semanal.py", line 7113, in accept
  File "mypy/nodes.py", line 1183, in accept
  File "mypy/semanal.py", line 1705, in visit_class_def
  File "mypy/semanal.py", line 1898, in analyze_class
  File "mypy/semanal.py", line 1945, in analyze_class_body_common
  File "mypy/semanal.py", line 2030, in apply_class_plugin_hooks
  File "/home/reznak/.cache/pypoetry/virtualenvs/svglab-Gw7jsKfD-py3.12/lib/python3.12/site-packages/pydantic/mypy.py", line 159, in _pydantic_model_class_maker_callback
    transformer.transform()
  File "/home/reznak/.cache/pypoetry/virtualenvs/svglab-Gw7jsKfD-py3.12/lib/python3.12/site-packages/pydantic/mypy.py", line 462, in transform
    'fields': {field.name: field.serialize() for field in fields},
                           ^^^^^^^^^^^^^^^^^
  File "/home/reznak/.cache/pypoetry/virtualenvs/svglab-Gw7jsKfD-py3.12/lib/python3.12/site-packages/pydantic/mypy.py", line 360, in serialize
    'type': self.type.serialize(),
            ^^^^^^^^^^^^^^^^^^^^^
  File "mypy/types.py", line 1498, in serialize
  File "mypy/types.py", line 3160, in serialize
AssertionError: Internal error: unresolved placeholder type svglab.bug.Child
/home/reznak/Documents/Projects/svglab/svglab/bug.py:17: : note: use --pdb to drop into pdb

The validation seems to work correctly at run-time. Removing BaseModel from the hierarchy fixes the crash, as does disabling the pydantic.mypy plugin or instantiating the type variable with any other (hypothetical) subclass of Element other than Child.

Example Code

from typing import Generic, TypeVar

from pydantic import BaseModel


class Element(BaseModel):
    pass


_T = TypeVar("_T", bound=Element)


class ElementWithChildren(Element, Generic[_T]):
    children: list[_T] = []


class Child(ElementWithChildren["Child"]):
    pass


Child()

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/reznak/.cache/pypoetry/virtualenvs/svglab-Gw7jsKfD-py3.12/lib/python3.12/site-packages/pydantic
python version: 3.12.7 (main, Oct  3 2024, 14:05:50) [GCC 13.2.0]
platform: Linux-6.6.63_1-x86_64-with-glibc2.39
related packages: pydantic-extra-types-2.10.0 mypy-1.13.0 typing_extensions-4.12.2
commit: unknown

The two patches

Our patch+40 −0
pydantic/mypy.pyindex 40aee062..57efc3c2 100644@@ -58,6 +58,7 @@ from mypy.plugins.common import (     deserialize_and_fixup_type, ) from mypy.semanal import set_callable_name+from mypy.semanal_shared import has_placeholder from mypy.server.trigger import make_wildcard_trigger from mypy.state import state from mypy.type_visitor import TypeTranslator@@ -534,6 +535,12 @@ class PydanticModelTransformer:         for field in fields:             if field.type is None:                 return False+            if has_placeholder(field.type):+                # The field type refers to a class that is not fully analyzed yet (e.g. the current class itself,+                # as in `class Child(Parent['Child'])` with a generic field in `Parent`), and can't be serialized.+                if not self._api.final_iteration:+                    self._api.defer()+                return False          is_settings = info.has_base(BASESETTINGS_FULLNAME)         self.add_initializer(fields, config, is_settings, is_a_root_model)tests/mypy/modules/generics.pyindex ecf5dea2..e96c271a 100644@@ -53,3 +53,36 @@ class DomainType(HistoryField[int]):  thing = DomainType(value=None) assert_type(thing.value, Optional[int])+++class Element(BaseModel):+    pass+++ElementT = TypeVar('ElementT', bound=Element)+++class ElementWithChildren(Element, Generic[ElementT]):+    children: list[ElementT] = []+    first_child: Optional[ElementT] = None+++class Child(ElementWithChildren['Child']):+    pass+++child = Child(children=[Child()])+assert_type(child.children, list[Child])+assert_type(child.first_child, Optional[Child])+++class EarlyElement(ElementWithChildren['LateElement']):+    pass+++class LateElement(Element):+    value: int+++early = EarlyElement(children=[LateElement(value=1)])+assert_type(early.children, list[LateElement]) 
Merged fix · PR #13442+26 −1
pydantic/mypy.pyindex 40aee0627..475aa3e55 100644@@ -58,6 +58,7 @@ from mypy.plugins.common import (     deserialize_and_fixup_type, ) from mypy.semanal import set_callable_name+from mypy.semanal_shared import has_placeholder from mypy.server.trigger import make_wildcard_trigger from mypy.state import state from mypy.type_visitor import TypeTranslator@@ -532,7 +533,12 @@ class PydanticModelTransformer:             # Some definitions are not ready. We need another pass.             return False         for field in fields:-            if field.type is None:+            if field.type is None or has_placeholder(field.type):+                # The field type may contain placeholders, e.g. when inheriting from a generic+                # model parametrized with a forward reference to the class being defined.+                # Request another pass so that they can be resolved:+                if not self._api.final_iteration:+                    self._api.defer()                 return False          is_settings = info.has_base(BASESETTINGS_FULLNAME)tests/mypy/modules/generics.pyindex ecf5dea2c..aec77397d 100644@@ -53,3 +53,22 @@ class DomainType(HistoryField[int]):  thing = DomainType(value=None) assert_type(thing.value, Optional[int])+++TElement = TypeVar('TElement', bound='Element')+++class Element(BaseModel):+    pass+++class ElementWithChildren(Element, Generic[TElement]):+    children: list[TElement] = []+++class Child(ElementWithChildren['Child']):+    """https://github.com/pydantic/pydantic/issues/11025"""+++child = Child(children=[Child()])+assert_type(child.children, list[Child])