Backtestspydanticrun-20261009-110034#11025
Mypy internal error when subclassing a generic model with a recursive type variable
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
The production fixes are functionally identical. Both import `has_placeholder` from `mypy.semanal_shared`. When a collected field's type still contains a placeholder, both call `self._api.defer()` (unless on the final iteration) and return False. That avoids serializing an unresolved `PlaceholderType`, which is the root cause of the assertion in `field.serialize()`, and lets mypy resolve the self-referential `Child` on a later pass. A folds the check into the existing `field.type is None` condition; B keeps it as a separate branch. That is only a cosmetic difference. The tests are where they differ. A appends a case to the existing `generics.py` module, but uses `TypeVar(..., bound='Element')` with a string forward reference. That differs from the issue, which binds to the already-defined class `Element`. It also checks only `Child(children=[Child()])` and the `children` type. B adds a dedicated module and registers it in the test case list. It reproduces the issue's exact setup (`bound=Element`) and also covers `model_construct` and a further subclass (`GrandChild`) that inherits the placeholder-mapped field and adds its own field. Both tests would crash before the fix and pass after it, but B's case is a more faithful reproduction and exercises more of the plugin's paths (`__init__`, `model_construct`, multi-level inheritance). Scope is minimal and equivalent in both. B wins on test quality.
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
5m 30s
Wall-clock time in the container
Full test suite
No new failures
0 passed · 54 skipped · 54 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
46vs 27 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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
TypeVarbound to aBaseModelsubclass, - has a self-referential type hint (e.g.,
Childreferencing itself throughGeneric[_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: unknownThe two patches
pydantic/mypy.pyindex 40aee062..c37a5b61 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,13 @@ class PydanticModelTransformer: for field in fields: if field.type is None: return False+ if has_placeholder(field.type):+ # The type still references a class that is not fully defined yet (e.g. an inherited+ # field mapped through a self-referential generic base, such as `class Child(Base['Child'])`).+ # Such types can't be serialized, so we need another pass.+ 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/self_referencing_generic.pynew file mode 100644index 00000000..f2c76f70@@ -0,0 +1,37 @@+"""+Subclassing a generic model with the subclass itself as the type argument+(https://github.com/pydantic/pydantic/issues/11036).+"""++from typing import Generic, TypeVar++from typing_extensions import assert_type++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+++class GrandChild(Child):+ name: str = ''+++child = Child(children=[Child()])+assert_type(child.children, list[Child])+assert_type(Child.model_construct(children=[child]).children, list[Child])++grand_child = GrandChild(children=[child], name='a')+assert_type(grand_child.children, list[Child])tests/mypy/test_mypy.pyindex c66f1964..36501de5 100644@@ -110,6 +110,7 @@ cases: list[ParameterSet | tuple[str, str]] = [ ('mypy-plugin.ini', 'plugin_strict_fields.py'), ('mypy-plugin.ini', 'final_with_default.py'), ('mypy-plugin.ini', 'create_model_var.py'),+ ('mypy-plugin.ini', 'self_referencing_generic.py'), ('mypy-plugin-strict-no-any.ini', 'dataclass_no_any.py'), ('mypy-plugin-strict-no-any.ini', 'plugin_no_any_dynamic_alias.py'), ('mypy-plugin-very-strict.ini', 'metaclass_args.py'),
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])