Skip to content

Crash when checking slice expression with 0 used for the step #18062

@AlexWaygood

Description

@AlexWaygood

Crash Report

Originally discovered by @sharkdp: mypy crashes when type checking a module containing just the following:

()[::0]

Playground link

Traceback

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 "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 109, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/main.py", line 193, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 269, in _build
    graph = dispatch(sources, manager, stdout)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2936, in dispatch
    process_graph(graph, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3334, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 3435, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/build.py", line 2305, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 483, in check_first_pass
    self.accept(d)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 592, in accept
    stmt.accept(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 1297, in accept
    return visitor.visit_expression_stmt(self)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checker.py", line 4547, in visit_expression_stmt
    expr_type = self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 5885, in accept
    typ = node.accept(self)
          ^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/nodes.py", line 2043, in accept
    return visitor.visit_index_expr(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4318, in visit_index_expr
    result = self.visit_index_expr_helper(e)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4334, in visit_index_expr_helper
    return self.visit_index_with_type(left_type, e)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4369, in visit_index_with_type
    return self.visit_tuple_slice_helper(left_type, index)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/checkexpr.py", line 4495, in visit_tuple_slice_helper
    item = left_type.slice(b, e, s, fallback=self.named_type("builtins.tuple"))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/layers/google.python.pip/pip/lib/python3.12/site-packages/mypy/types.py", line 2549, in slice
    slice_items = self.items[begin:end:stride]
                  ~~~~~~~~~~^^^^^^^^^^^^^^^^^^
ValueError: slice step cannot be zero

main.py:1: 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.14.0+dev.2d785dffee4440b9e5fd4c4f7dbf8787a829f5fa
main.py:1: : note: use --pdb to drop into pdb

Your Environment

  • Mypy version used: 1.14.0+dev.2d785dffee4440b9e5fd4c4f7dbf8787a829f5fa
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions