Backtestspydanticrun-20261027-165153#11454
AssertionError for the mypy plugin if type variables are defined before their bound
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 root cause the same way: before generating methods, check whether the class's type variables still contain placeholder types (the forward-referenced bound) and defer so the generated `self`/return types do not carry placeholders into the final iteration. Both pass all four independent tests. Differences: (1) Control flow. Patch A mirrors the existing field-placeholder check exactly: it defers when not in the final iteration and returns False either way, so methods are never generated with placeholder types. Patch B only returns early when it can defer; on the final iteration it falls through and generates methods with placeholders still present, which is the very state that produced the internal error. That is an edge case, but it is an inconsistency with the surrounding pattern and a residual risk. (2) Coverage. Patch A explicitly checks bound, default and constraint values; Patch B relies on mypy's `has_placeholder` type visitor, which also recurses into those for TypeVarType, so B is not less correct there, just more implicit. (3) Patch A also fixes a latent bug in `add_method`: plugin-generated classmethods are stored as a `Decorator` symbol while only the `FuncDef` lives in the class body, so the existing cleanup never removed stale generated classmethods on re-runs. This is adjacent but directly relevant once the transformer can now be re-run after deferring, and it is small and low risk. (4) Tests. Patch A adds a dedicated regression module exercising bound, constraints and default TypeVars plus assert_type checks on instantiation and model_construct; Patch B adds a minimal bound-only case to an existing module (with double-quoted strings inconsistent with the file's style). A's tests are broader and would fail before the fix. Overall A is the more careful, consistent and better-tested fix.
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
7m 21s
Wall-clock time in the container
Full test suite
No new failures
0 passed · 55 skipped · 55 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
61vs 14 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
Running mypy with the pydantic.mypy plugin enabled fails with an error if it encounters a BaseModel with a bound TypeVar, where the type variable is defined _before_ its bound. (See the minimal example below for an illustration) With the pydantic.mypy plugin disabled, type checking is successful.
Error details:
cat.py:16: 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.15.0
Traceback (most recent call last):
File "mypy/semanal.py", line 7240, in accept
File "mypy/nodes.py", line 811, in accept
File "mypy/semanal.py", line 907, in visit_func_def
File "mypy/semanal.py", line 952, in analyze_func_def
File "mypy/semanal.py", line 6894, in defer
AssertionError: Must not defer during final iteration
In my experience, it is quite common to define type variables at the top of the module, see for example the mypy docs on Generics.
Issue #11025 describes a different problem, but may be related.
Thank you for your work on Pydantic! :sparkling_heart:
Example Code
from typing import Generic, TypeVar
from pydantic import BaseModel
# Defined before Mouse: Causes internal error below
M = TypeVar('M', bound='Mouse')
class Mouse:
pass
# Defined after Mouse: Works without problem
# M = TypeVar('M', bound='Mouse')
class Cat(BaseModel, Generic[M]): # INTERNAL ERROR
pass
Python, Pydantic & OS Version
pydantic version: 2.10.6
pydantic-core version: 2.27.2
pydantic-core build: profile=release pgo=false
install path: .venv/lib/python3.12/site-packages/pydantic
python version: 3.12.3 (main, Jan 17 2025, 18:03:48) [GCC 13.3.0]
platform: Linux-6.8.0-52-generic-x86_64-with-glibc2.39
related packages: mypy-1.15.0 typing_extensions-4.12.2
commit: unknown
(Additionally I tried to reproduce it with current Pydantic main (aa59d53), with the same results)The two patches
pydantic/mypy.pyindex 548c2a8e..04d4e94e 100644@@ -549,6 +549,18 @@ class PydanticModelTransformer: if not self._api.final_iteration: self._api.defer() return False+ for type_var in info.defn.type_vars:+ type_var_types = [type_var.upper_bound, type_var.default]+ if isinstance(type_var, TypeVarType):+ type_var_types.extend(type_var.values)+ if any(has_placeholder(t) for t in type_var_types):+ # The bound, constraints or default of a type variable of the class may not be ready yet,+ # e.g. when the type variable is defined before the class used as its bound.+ # The generated methods would include these placeholders (e.g. in the `self` type), so+ # request another pass instead:+ 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)@@ -1373,6 +1385,9 @@ def add_method( sym = info.names[name] if sym.plugin_generated and isinstance(sym.node, FuncDef): cls.defs.body.remove(sym.node) # pragma: no cover+ elif sym.plugin_generated and isinstance(sym.node, Decorator) and sym.node.func in cls.defs.body:+ # Generated classmethods are stored as a `Decorator` symbol, but only the `FuncDef` is in the body:+ cls.defs.body.remove(sym.node.func) # pragma: no cover if isinstance(api, SemanticAnalyzerPluginInterface): function_type = api.named_type('builtins.function')tests/mypy/modules/generics_forward_ref_typevar.pynew file mode 100644index 00000000..05d3d36a@@ -0,0 +1,45 @@+"""Regression test for a mypy internal error ("Must not defer during final iteration").++Type variables defined before the class referenced in their bound, constraints or default.+"""++from typing import Generic, TypeVar++from typing_extensions import TypeVar as TypeVarExt+from typing_extensions import assert_type++from pydantic import BaseModel++M = TypeVar('M', bound='Mouse')+C = TypeVar('C', 'Mouse', int)+D = TypeVarExt('D', default='Mouse')+++class Mouse:+ pass+++class Cat(BaseModel, Generic[M]):+ pass+++class Owner(BaseModel, Generic[M]):+ pet: M+++class Constrained(BaseModel, Generic[C]):+ value: C+++class WithDefault(BaseModel, Generic[D]):+ value: D+++cat = Cat[Mouse]()+owner = Owner(pet=Mouse())+assert_type(owner.pet, Mouse)+assert_type(Owner.model_construct(pet=Mouse()), Owner[Mouse])+constrained = Constrained(value=1)+assert_type(constrained.value, int)+with_default = WithDefault(value=Mouse())+assert_type(with_default.value, Mouse)tests/mypy/test_mypy.pyindex 2ac32f6f..79ac66ac 100644@@ -106,6 +106,7 @@ cases: list[ParameterSet | tuple[str, str]] = [ ('mypy-plugin.ini', 'frozen_field.py'), ('mypy-plugin.ini', 'plugin_optional_inheritance.py'), ('mypy-plugin.ini', 'generics.py'),+ ('mypy-plugin.ini', 'generics_forward_ref_typevar.py'), ('mypy-plugin.ini', 'root_models.py'), ('mypy-plugin.ini', 'plugin_strict_fields.py'), ('mypy-plugin.ini', 'final_with_default.py'),
pydantic/mypy.pyindex 548c2a8ef..35e080643 100644@@ -549,6 +549,10 @@ class PydanticModelTransformer: if not self._api.final_iteration: self._api.defer() return False+ for tv in info.defn.type_vars:+ if has_placeholder(tv) and 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 aec77397d..843488818 100644@@ -72,3 +72,13 @@ class Child(ElementWithChildren['Child']): child = Child(children=[Child()]) assert_type(child.children, list[Child])++AnyBaseFoo = TypeVar("AnyBaseFoo", bound="BaseFoo")++class BaseFoo:+ pass++class Foo(BaseModel, Generic[AnyBaseFoo]):+ pass # intentionally empty++assert_type(Foo[BaseFoo](), Foo[BaseFoo])