Skip to content

Crash with generic-of-generic when called with --prety #22424

Closed

Description

@jonathanzetier

Crash Report

I encountered a crash while initially using 2.4.1, and confirmed that it exists on the lasest master.

Traceback

jonathan:~/software/mypy$ mypy --pretty ~/sandbox/mypycrashes/crash.py --show-traceback
/home/jonathan.eskeldson/sandbox/mypycrashes/crash.py: 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.5.0+dev.b83696ea8099ef3772a5a2bc2c49f2b05340c37a
Traceback (most recent call last):
  File "/home/jonathan.eskeldson/software/mypy/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/jonathan.eskeldson/software/mypy/mypy/__main__.py", line 16, in console_entry
    main()
  File "/home/jonathan.eskeldson/software/mypy/mypy/main.py", line 167, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/jonathan.eskeldson/software/mypy/mypy/main.py", line 257, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 427, in build
    result = build_inner(
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 542, in build_inner
    graph = dispatch(sources, manager, stdout, connect_threads)
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 4152, in dispatch
    graph = load_graph(sources, manager)
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 4385, in load_graph
    manager.parse_all([state for state in new if state.needs_parse])
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 1091, in parse_all
    parallel_states[0].parse_file()
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 3292, in parse_file
    with self.wrap_context():
  File "/usr/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 3119, in wrap_context
    yield
  File "/home/jonathan.eskeldson/software/mypy/mypy/build.py", line 3318, in parse_file
    manager.errors.raise_error()
  File "/home/jonathan.eskeldson/software/mypy/mypy/errors.py", line 1020, in raise_error
    self.new_messages(), use_stdout=use_stdout, module_with_blocker=self.blocker_module()
  File "/home/jonathan.eskeldson/software/mypy/mypy/errors.py", line 1137, in new_messages
    self.format_messages(path, error_tuples, formatter=self.error_formatter)
  File "/home/jonathan.eskeldson/software/mypy/mypy/errors.py", line 1113, in format_messages
    return self.format_messages_default(error_tuples, source_lines)
  File "/home/jonathan.eskeldson/software/mypy/mypy/errors.py", line 1059, in format_messages_default
    source_line = source_lines[line - 1]
IndexError: list index out of range

/home/jonathan.eskeldson/sandbox/mypycrashes/crash.py: note: use --pdb to drop into pdb
jonathan:~/software/mypy$

To Reproduce

I was surprised at how small of a file I was able to do to reproduce this. I'm only able to reproduce this by passing --pretty to mypy, which doesn't seem to be available on the online mypy playground. These are the entire contents of the file, yes it's just one line.

typing.Optional[typing.Union[A, B[T]]

If I run mypy by itself, I get error: Unexpected EOF while parsing [syntax] (which is what I'd expect), if I run mypy --pretty, that's when it crashes. This behavior exists in more well-formed python programs as well, but this bug seems to exist whether the python program is well-formed or not.

Changing the file to simply typing.Optional[typing.Union[A, B]] seems to run without crashing.

Your Environment

  • Mypy version used: 2.4.1, version: 2.5.0+dev.b83696ea8099ef3772a5a2bc2c49f2b05340c37a
  • Mypy command-line flags: --pretty
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10, 3.11
  • Operating system and version: Ubuntu 22.04

Activity

  1. lianxiaozhi commented on Nov 10, 2026

    @lianxiaozhi
    Contributor

    I've opened #22430 with a bounds check for the source line used by --pretty. The regression test reproduces the crash before the fix and passes afterward.

  2. added a commit that references this issue on Nov 18, 2026
    22b03fe
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

      No branches or pull requests

      Issue actions