Skip to content

Internal mypy error related to circular imports and TypedDict #21741

Description

@jenshnielsen

Crash Report

I have observed a regression in mypy 2.2.0 and up where code that used to type check now causes an internal mypy error. An example of this can be seen here. QCoDeS/Qcodes_contrib_drivers#576 https://github.com/QCoDeS/Qcodes_contrib_drivers/actions/runs/29298819262/job/86978061271?pr=576 as well as a more minimal example linked below.

Traceback

❯ uv run mypy mypy_repro\ --show-traceback
mypy_repro\m_nd.py: error: Cannot resolve TypedDict item (possible cyclic definition)  [misc]
mypy_repro\m_param.py:28: 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.4.0+dev.67eecf07fea3fa4bbaab9d46cd28372a8ded330b
Traceback (most recent call last):
  File "<frozen runpy>", line 203, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\source\repos\mypy_22_issue\.venv\Scripts\mypy.exe\__main__.py", line 10, in <module>
    sys.exit(console_entry())
  File "D:\source\opensource\mypy\mypy\__main__.py", line 16, in console_entry
    main()
  File "D:\source\opensource\mypy\mypy\main.py", line 154, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "D:\source\opensource\mypy\mypy\main.py", line 244, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "D:\source\opensource\mypy\mypy\build.py", line 422, in build
    result = build_inner(
  File "D:\source\opensource\mypy\mypy\build.py", line 537, in build_inner
    graph = dispatch(sources, manager, stdout, connect_threads)
  File "D:\source\opensource\mypy\mypy\build.py", line 4146, in dispatch
    process_graph(graph, manager)
  File "D:\source\opensource\mypy\mypy\build.py", line 4614, in process_graph
    done, still_working, results = manager.wait_for_done(graph)
  File "D:\source\opensource\mypy\mypy\build.py", line 1484, in wait_for_done
    process_stale_scc(graph, next_scc, self)
  File "D:\source\opensource\mypy\mypy\build.py", line 4782, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "D:\source\opensource\mypy\mypy\semanal_main.py", line 92, in semantic_analysis_for_scc
    process_functions(graph, scc, patches)
  File "D:\source\opensource\mypy\mypy\semanal_main.py", line 275, in process_functions
    process_top_level_function(
  File "D:\source\opensource\mypy\mypy\semanal_main.py", line 314, in process_top_level_function
    deferred, incomplete, progress = semantic_analyze_target(
  File "D:\source\opensource\mypy\mypy\semanal_main.py", line 380, in semantic_analyze_target
    analyzer.refresh_partial(
  File "D:\source\opensource\mypy\mypy\semanal.py", line 709, in refresh_partial
    self.accept(node)
  File "D:\source\opensource\mypy\mypy\semanal.py", line 7732, in accept
    node.accept(self)
    ~~~~~~~~~~~^^^^^^
  File "D:\source\opensource\mypy\mypy\nodes.py", line 1165, in accept
    return visitor.visit_func_def(self)
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "D:\source\opensource\mypy\mypy\semanal.py", line 991, in visit_func_def
    self.analyze_func_def(defn)
    ~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "D:\source\opensource\mypy\mypy\semanal.py", line 1036, in analyze_func_def
    self.defer(defn)
    ~~~~~~~~~~^^^^^^
  File "D:\source\opensource\mypy\mypy\semanal.py", line 7333, in defer
    assert not self.final_iteration, "Must not defer during final iteration"
           ^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Must not defer during final iteration

mypy_repro\m_param.py:28: note: use --pdb to drop into pdb

To Reproduce

Run mypy on the code in the following repo https://github.com/jenshnielsen/mypy_22_issue
Note that since this is related to circular imports, I was not able to minimize this below 4 files

Your Environment

  • Mypy version used: 2.2.0, 2.3.0 as well as master as of 67eecf0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): Empty config
  • Python version used: 3.14.6
  • Operating system and version: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions