Skip to content

crash with quoted forward reference #20073

@gdchinacat

Description

@gdchinacat

Crash Report
In trying to annotate my project I encountered a mypy INTERNAL ERROR. I'm pretty sure the types are wrong, but thought I should report the crash regardless. I reduced it to a simple reproduction (in the section below).

Traceback

lonniehutchinson@MacBookAir src % mypy --show-traceback mypy_bug.py
mypy_bug.py:4: 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: 1.19.0+dev.2c6c3959356674262d9b2c2dc43a33486e807a9c
Traceback (most recent call last):
  File "/Users/lonniehutchinson/python3.14_master_mypy/bin/mypy", line 7, in <module>
    sys.exit(console_entry())
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/main.py", line 127, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/main.py", line 211, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/build.py", line 200, in build
    result = _build(
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/build.py", line 279, in _build
    graph = dispatch(sources, manager, stdout)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/build.py", line 2940, in dispatch
    process_graph(graph, manager)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/build.py", line 3340, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/build.py", line 3435, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal_main.py", line 95, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal_main.py", line 222, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal_main.py", line 399, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 658, in refresh_partial
    self.refresh_top_level(node)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 676, in refresh_top_level
    self.accept(d)
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 7387, in accept
    node.accept(self)
    ~~~~~~~~~~~^^^^^^
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/nodes.py", line 1445, in accept
    return visitor.visit_class_def(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 1776, in visit_class_def
    self.analyze_class(defn)
    ~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 1916, in analyze_class
    bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(
                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        defn, bases, context=defn
        ^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/lonniehutchinson/python3.14_master_mypy/lib/python3.14/site-packages/mypy/semanal.py", line 2223, in clean_up_bases_and_infer_type_variables
    assert isinstance(node.node, TypeVarLikeExpr)
           ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError:
mypy_bug.py:4: : note: use --pdb to drop into pdb

To Reproduce

type Tb = object
type Ta[Tb] = 'B[Tb]'
class A[Ta]: ...
class B[Tb](A[Ta]): ...

Moving the quoted ref to after B is defined avoids the crash, either still quoted or not quoted. This works:

# this avoids the crash
type Tb = object
class A[Ta]: ...
class B[Tb](A[Ta]): ...
type Ta[Tb] = 'B[Tb]'

Your Environment

  • Mypy version used: mypy 1.19.0+dev.2c6c3959356674262d9b2c2dc43a33486e807a9c (compiled: no)
  • Mypy command-line flags: mypy --show-traceback mypy_bug.py
  • Mypy configuration options from mypy.ini (and other config files): default configuration, no mypy.ini
  • Python version used: Python 3.14.0 with fresh venv with mypy dev installed using website instructions for using master version from git.
  • Operating system and version: macOS 15.6.1 (24G90)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions