python / python/mypy

Crash on a type alias to new-style union of unresolved types

Open
#21,263 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash topic-import-cycles topic-pep-604 topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

mypy crashes when used with the latest version of Qiskit 2.4.0, released yesterday (2026-04-16).
Both the released mypy 1.20.1 and master version crash.

Note: the Qiskit package is not typed, so I use the option --follow-untyped-imports. mypy does not crash with the previous release of Qiskit, 2.3.1, but crashes with the latest 2.4.0 release.

Traceback

version: 2.0.0+dev.410f9339916d836b10a286adc1dae43d9f12d251
Traceback (most recent call last):
  File "/Users/tmartine/micromamba/envs/mypy-bug/bin/mypy", line 6, in <module>
    sys.exit(console_entry())
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/main.py", line 143, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/main.py", line 233, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 421, in build
    result = build_inner(
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 529, in build_inner
    graph = dispatch(sources, manager, stdout, connect_threads)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 4012, in dispatch
    process_graph(graph, manager)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 4476, in process_graph
    done, still_working, results = manager.wait_for_done(graph)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 1389, in wait_for_done
    process_stale_scc(graph, next_scc, self)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 4650, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/build.py", line 3271, in type_check_first_pass
    self.type_checker().check_first_pass(recurse_into_functions=recurse_into_functions)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 538, in check_first_pass
    self.accept(d)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 649, in accept
    stmt.accept(self)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/nodes.py", line 1877, in accept
    return visitor.visit_assignment_stmt(self)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 3186, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 3361, in check_assignment
    rvalue_type, lvalue_type = self.check_simple_assignment(
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 4767, in check_simple_assignment
    rvalue_type = self.expr_checker.accept(
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 6178, in accept
    typ = self.accept_maybe_cache(node, type_context=type_context)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 6213, in accept_maybe_cache
    typ = node.accept(self)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/nodes.py", line 2667, in accept
    return visitor.visit_op_expr(self)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 3557, in visit_op_expr
    return self.accept(e.analyzed)
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 6180, in accept
    typ = node.accept(self)  # r-value type, when interpreted as a value expression
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/nodes.py", line 3390, in accept
    return visitor.visit_type_alias_expr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 4962, in visit_type_alias_expr
    return self.alias_type_in_runtime_context(alias.node, ctx=alias, alias_definition=True)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checkexpr.py", line 5019, in alias_type_in_runtime_context
    return self.chk.named_generic_type("types.UnionType", item.items)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/checker.py", line 7605, in named_generic_type
    args = [remove_instance_last_known_values(arg) for arg in args]
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/erasetype.py", line 239, in remove_instance_last_known_values
    return t.accept(LastKnownValueEraser())
           ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/mypy/types.py", line 3608, in accept
    assert isinstance(visitor, SyntheticTypeVisitor)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

/Users/tmartine/micromamba/envs/mypy-bug/lib/python3.14/site-packages/qiskit/circuit/library/arithmetic/quadratic_form.py:27: note: use --pdb to drop into pdb

To Reproduce

mamba create -n mypy-bug python==3.14 mypy qiskit==2.4.0
echo "import qiskit" > mypy_bug.py
mamba run -n mypy-bug mypy --show-traceback --follow-untyped-imports mypy_bug.py

Your Environment

  • Mypy version used: 1.20.1 and master
  • Mypy command-line flags: --show-traceback --follow-untyped-imports
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.14
  • Operating system and version: macOS 26.4.1 (25E253)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided mamba reproduction with Qiskit 2.4.0 and --follow-untyped-imports. Start in mypy/checkexpr.py at alias_type_in_runtime_context and follow the call into mypy/erasetype.py and the SyntheticTypeVisitor assertion. Done means the reproduction no longer crashes and the unresolved-union alias case has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.