Skip to content

AssertionError: Must not defer during final iteration, when subclassing pydantic BaseModel and using a forward referenced type variable at the same class definition #19898

@ofarukdogancay

Description

@ofarukdogancay

Crash Report

A forward reference of a generic type variable crashes mypy if the forward reference belongs to a Pydantic BaseModel. I tried replicating the issue with simpler classes by examining Pydantic’s quirks but couldn’t narrow it down further.

This works:

import typing
from pydantic import BaseModel

class Foo: ...

R = typing.TypeVar("R", bound="Foo")

class FooBar(BaseModel, typing.Generic[R]): ...

This does too:

import typing


R = typing.TypeVar("R", bound="Foo")

class Foo: ...
class Bar: ...

class FooBar(Bar, typing.Generic[R]): ...

But the following does not. Mypy outputs mypy_example/test.py:9: error: INTERNAL ERROR specifically on the line:

class FooBar(Bar, typing.Generic[R]): ...

import typing
from pydantic import BaseModel

R = typing.TypeVar("R", bound="Foo")

class Foo: ...

class FooBar(BaseModel, typing.Generic[R]): ...

Traceback

mypy 1.19.0+dev.feeb3f00a63d31cf5c7369885c4dd4a294133f59

Traceback (most recent call last):
  File "mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 127, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "mypy/main.py", line 211, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "mypy/build.py", line 196, in build
    result = _build(...)
  File "mypy/build.py", line 272, in _build
    graph = dispatch(sources, manager, stdout)
  File "mypy/build.py", line 2946, in dispatch
    process_graph(graph, manager)
  File "mypy/build.py", line 3346, in process_graph
    process_stale_scc(graph, scc, manager)
  File "mypy/build.py", line 3441, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "mypy/semanal_main.py", line 96, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "mypy/semanal_main.py", line 294, in process_functions
    process_top_level_function(...)
  File "mypy/semanal_main.py", line 333, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(...)
  File "mypy/semanal_main.py", line 399, in semantic_analyze_target
    analyzer.refresh_partial(...)
  File "mypy/semanal.py", line 661, in refresh_partial
    self.accept(node)
  File "mypy/semanal.py", line 7371, in accept
    node.accept(self)
  File "mypy/nodes.py", line 971, in accept
    return visitor.visit_func_def(self)
  File "mypy/semanal.py", line 927, in visit_func_def
    self.analyze_func_def(defn)
  File "mypy/semanal.py", line 972, in analyze_func_def
    self.defer(defn)
  File "mypy/semanal.py", line 7017, in defer
    assert not self.final_iteration, "Must not defer during final iteration"
AssertionError: Must not defer during final iteration

To Reproduce

import typing
from pydantic import BaseModel

R = typing.TypeVar("R", bound="Foo")

class Foo: ...

class FooBar(BaseModel, typing.Generic[R]): ...

Your Environment

UV Managed python virtual environment with only local build of mypy from master and pydantic

  • Pydantic version used: 2.11.7
  • Mypy version used: 1.19.0+dev.feeb3f00a63d31cf5c7369885c4dd4a294133f59
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13.5
  • Operating system and version: macOS Sequoia 15.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    crashsemantic-analyzerProblems that happen during semantic analysis

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions