Skip to content

Parallel type checking crashes on pydantic.create_model used as a base class #21472

@sunny-zuo

Description

@sunny-zuo

Disclaimer: this report is largely written by Claude as I had it go create a minimal reproduction after encountering this bug on an internal codebase. I did manually run mypy and confirm that the outputs/setup here are correct.

Crash Report

mypy --num-workers=N crashes with AssertionError: Cannot find component 'X@<lineno>' during fix_cross_refs when the pydantic.mypy plugin has synthesized a function-local TypeInfo for a create_model(...) call, that synthetic class is used as a base of a sibling local class, and another module imports any symbol from the producer module. Serial mode is unaffected. The crash also reproduces against current master (2.2.0+dev, commit e53693be0).

The @<lineno> suffix is added by mypy itself in semanal.basic_new_typeinfo for function-scoped synthetic TypeInfos. There is a TODO: clean this up, see #6422 next to the mangling code, so this area is already flagged.

Traceback

$ mypy --num-workers=3 --show-traceback producer.py consumer.py
consumer.py: 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: 2.2.0+dev.e53693be05a200543a5b00edd2bb82bbf7c12329
consumer.py: note: use --pdb to drop into pdb
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "mypy/build_worker/__main__.py", line 6, in <module>
    console_entry()
  File "mypy/build_worker/worker.py", line 361, in console_entry
    main(sys.argv[1:])
  File "mypy/build_worker/worker.py", line 126, in main
    serve(server, ctx)
  File "mypy/build_worker/worker.py", line 228, in serve
    scc_result = process_stale_scc_interface(...)
  File "mypy/build.py", line 4869, in process_stale_scc_interface
    maybe_load_deps(graph, ascc, manager)
  File "mypy/build.py", line 4749, in maybe_load_deps
    process_fresh_modules(graph, sorted(prev_scc.mod_ids), manager)
  File "mypy/build.py", line 4703, in process_fresh_modules
    graph[id].fix_cross_refs()
  File "mypy/build.py", line 3133, in fix_cross_refs
    modules_state.node_fixer.visit_symbol_table(self.tree.names)
  File "mypy/fixup.py", line 131, in visit_symbol_table
    self.visit_type_info(value._node)
  File "mypy/fixup.py", line 71, in visit_type_info
    base.accept(self.type_fixer)
  File "mypy/types.py", line 1673, in accept
    return visitor.visit_instance(self)
  File "mypy/fixup.py", line 237, in visit_instance
    inst.type = lookup_fully_qualified_typeinfo(...)
  File "mypy/fixup.py", line 375, in lookup_fully_qualified_typeinfo
    stnode = lookup_fully_qualified(name, modules, raise_on_missing=not allow_missing)
  File "mypy/lookup.py", line 56, in lookup_fully_qualified
    assert key in names, f"Cannot find component {key!r} for {name!r}"
AssertionError: Cannot find component 'Inner@12' for 'producer.Inner@12'

error: INTERNAL ERROR -- Please try using mypy master on GitHub:
...
Traceback (most recent call last):
  File "mypy/build.py", line 1392, in receive_worker_message
    return receive(self.workers[idx].conn)
  File "mypy/ipc.py", line 481, in receive
    raise OSError("No data received")
OSError: No data received

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File ".../bin/mypy", line 10, in <module>
    sys.exit(console_entry())
  File "mypy/__main__.py", line 16, in console_entry
    main()
  File "mypy/main.py", line 154, in main
    res, messages, blockers = run_build(...)
  File "mypy/main.py", line 244, in run_build
    res = build.build(...)
  File "mypy/build.py", line 422, in build
    result = build_inner(...)
  File "mypy/build.py", line 537, in build_inner
    graph = dispatch(sources, manager, stdout, connect_threads)
  File "mypy/build.py", line 4150, in dispatch
    process_graph(graph, manager)
  File "mypy/build.py", line 4618, in process_graph
    done, still_working, results = manager.wait_for_done(graph)
  File "mypy/build.py", line 1488, in wait_for_done
    return self.wait_for_done_workers(graph)
  File "mypy/build.py", line 1507, in wait_for_done_workers
    buf = self.receive_worker_message(idx)
  File "mypy/build.py", line 1400, in receive_worker_message
    raise OSError("Worker N disconnected before sending data (exit code 2)")
OSError: Worker 1 disconnected before sending data (exit code 2)

To Reproduce

Three files in one directory.

producer.py:

"""Defines a function-local pydantic class that is used as a base class."""

from pydantic import BaseModel, create_model


def make_outer() -> type[BaseModel]:
    Inner = create_model("Inner", __base__=BaseModel, value=(int, ...))

    class Outer(Inner):  # type: ignore[misc, valid-type]
        pass

    return Outer


# Unrelated top-level symbol so the consumer module has something to import.
# The crash does NOT require the consumer to reference `Inner` or `Outer`.
PUBLIC: int = 1

consumer.py:

from producer import PUBLIC

x: int = PUBLIC

mypy.ini:

[mypy]
plugins = pydantic.mypy
python_version = 3.12

Run:

rm -rf .mypy_cache
uvx --with pydantic mypy --num-workers=3 producer.py consumer.py

Bisection notes

All four conditions are required:

  1. A function-local create_model("Inner", __base__=BaseModel, ...) call.
  2. A function-local class that subclasses that synthetic Inner. Returning Inner directly (without using it as a base) does not trigger the crash.
  3. A second module that imports any top-level symbol from the producer — the consumer does not need to reference Inner or its outer class.
  4. Enough parallel workers that the two modules land on different workers.

Worker-count sensitivity on this minimal repro:

--num-workers result
0 (serial) clean
1 clean
2 clean (stdlib SCC dominates; both modules land on one worker)
3 crash
4 crash
8 crash

Independent of cache format: same crash with --no-fixed-format-cache and with --no-sqlite-cache. Disabling the pydantic.mypy plugin makes the crash disappear (at the cost of dynamic-class typing).

Your Environment

  • Mypy version used: 2.1.0 (released) and 2.2.0+dev.e53693be05a200543a5b00edd2bb82bbf7c12329 (current master, both crash identically)
  • Mypy command-line flags: --num-workers=3
  • Mypy configuration options from mypy.ini:
    [mypy]
    plugins = pydantic.mypy
    python_version = 3.12
  • Python version used: 3.12.13
  • Operating system and version: macOS 14 / Darwin 25.4.0 arm64
  • pydantic version: 2.13.4

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions