Skip to content

class S(tuple[*tuple[()]]) crashes mypy #19105

Open
@jorenham

Description

@jorenham
Contributor
class S(tuple[*tuple[()]]): ...

mypy 1.15 stacktrace (mypy-play):

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 119, in main
  File "mypy/main.py", line 203, in run_build
  File "mypy/build.py", line 191, in build
  File "mypy/build.py", line 267, in _build
  File "mypy/build.py", line 2937, in dispatch
  File "mypy/build.py", line 3335, in process_graph
  File "mypy/build.py", line 3430, in process_stale_scc
  File "mypy/semanal_main.py", line 97, in semantic_analysis_for_scc
  File "mypy/semanal.py", line 7621, in apply_semantic_analyzer_patches
  File "mypy/semanal.py", line 2587, in <lambda>
  File "mypy/semanal_shared.py", line 305, in calculate_tuple_fallback
NotImplementedError

and on current master (mypy-play)

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/__main__.py", line 37, in <module>
    console_entry()
  File "mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 127, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/main.py", line 211, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/build.py", line 191, in build
    result = _build(
             ^^^^^^^
  File "mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/build.py", line 2939, in dispatch
    process_graph(graph, manager)
  File "mypy/build.py", line 3337, in process_graph
    process_stale_scc(graph, scc, manager)
  File "mypy/build.py", line 3432, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "mypy/semanal_main.py", line 99, in semantic_analysis_for_scc
    apply_semantic_analyzer_patches(patches)
  File "mypy/semanal.py", line 7740, in apply_semantic_analyzer_patches
    patch_func()
  File "mypy/semanal.py", line 2630, in <lambda>
    self.schedule_patch(PRIORITY_FALLBACKS, lambda: calculate_tuple_fallback(base))
                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "mypy/semanal_shared.py", line 305, in calculate_tuple_fallback
    raise NotImplementedError

another way to trigger this crash is through a typo in the variadic type parameter name:

class MyTuple[*Ts](tuple[*_Ts]): ...  # oh no a typo

this produces an identical stacktrace (mypy-play)


some observations:

  • it doesn't matter if typing.Unpack is used instead of *
  • I checked all versions from 1.9.0-master, and all crash in the same way
  • class S(tuple[()]): ... does not cause a crash
    (although S() is revealed as tuple[(), fallback=__main__.S], which is kinda sus)
  • in case you didn't notice; the first example is valid at runtime, the second isn't
  • my ear is a bit itchy

And it's probably not what you want to hear, but it might be relevant info, so I'll carefully whisper it: Pyright does not have this problem (playground 1, playground 2), and the same goes for ty (playground 1, playground 2)


ref: #19100 (comment)

Activity

changed the title [-]`class S(tuple[*tuple[()]]): ...` crashes mypy[/-] [+]`class S(tuple[*tuple[()]])` crashes mypy[/+] on May 18, 2025
sterliakov

sterliakov commented on May 19, 2025

@sterliakov
Collaborator

Ough, note this isn't specific to empty tuples, the following crashes just as hard:

class S(tuple[*tuple[int]]): ...
sterliakov

sterliakov commented on May 19, 2025

@sterliakov
Collaborator

Quick braindump: we normalize such trivial tuples in semanal_typeargs, but TypeArgumentsAnalyzer runs after we have calculated all fallbacks. Fallback calculation logic assumes normalized inputs, which is most probably wrong. I'm not sure whether what I did is the best approach, but at least it doesn't crash anymore - let's continue this discussion in the linked PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jorenham@sterliakov

      Issue actions

        `class S(tuple[*tuple[()]])` crashes mypy · Issue #19105 · python/mypy