[2.2 regression] Internal mypy error related to circular imports and TypedDict
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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. https://github.com/QCoDeS/Qcodes_contrib_drivers/pull/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 67eecf07fea3fa4bbaab9d46cd28372a8ded330b
- 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with the linked mypy_22_issue repository and --show-traceback, then trace the reported path through mypy/semanal.py and mypy/semanal_main.py, especially final-iteration deferral and cyclic TypedDict resolution. Done means the four-file example no longer produces an INTERNAL ERROR while reporting the appropriate type-checking result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100