Skip to content

'break' in nested function causes an internal error  #15378

Closed

Description

@xiaxinmeng

The following example presents a nest function definition. In the inner function, this code uses the 'break' keywords, Mypy reports an internal error.

example.py

def foo():
    for x in range(10):
        def inner():
            break

The expected output:

It does not report an internal error.

The actual output:

/home/xxm/Desktop/example.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.4.0+dev.66bd2c441277b29b1454e26b2a3454a0dae5fb18
Traceback (most recent call last):
  File "/home/xxm/.local/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 267, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 2926, in dispatch
    process_graph(graph, manager)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 3324, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 3425, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/build.py", line 2311, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 472, in check_first_pass
    self.accept(d)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 786, in accept
    return visitor.visit_func_def(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 962, in visit_func_def
    self._visit_func_def(defn)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 966, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 1038, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 1234, in check_func_def
    self.accept(item.body)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1220, in accept
    return visitor.visit_block(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 2684, in visit_block
    self.accept(s)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1401, in accept
    return visitor.visit_for_stmt(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 4564, in visit_for_stmt
    self.accept_loop(s.body, s.else_body)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 602, in accept_loop
    self.accept(body)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1220, in accept
    return visitor.visit_block(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 2684, in visit_block
    self.accept(s)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 786, in accept
    return visitor.visit_func_def(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 962, in visit_func_def
    self._visit_func_def(defn)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 966, in _visit_func_def
    self.check_func_item(defn, name=defn.name)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 1038, in check_func_item
    self.check_func_def(defn, typ, name, allow_empty)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 1234, in check_func_def
    self.accept(item.body)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1220, in accept
    return visitor.visit_block(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 2684, in visit_block
    self.accept(s)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 582, in accept
    stmt.accept(self)
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/nodes.py", line 1455, in accept
    return visitor.visit_break_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/checker.py", line 4844, in visit_break_stmt
    self.binder.handle_break()
  File "/home/xxm/.local/lib/python3.11/site-packages/mypy/binder.py", line 379, in handle_break
    self.allow_jump(self.break_frames[-1])
                    ~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range
/home/xxm/Desktop/exmaple.py:4: : note: use --pdb to drop into pdb

Test environment:

Mypy version: 1.4.0+ (master)
Python version: 3.11.3
Operating System: Ubuntu 18.04
How to install MyPy: "python3 -m pip install -U git+https://github.com/python/mypy.git"

Activity

  1. added a commit that references this issue on Jun 9, 2023
    2c66cba
  2. added a commit that references this issue on Jun 12, 2023
    9caf095
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