#12408·ipython

'nonlocal' misclassified as incomplete code

Author: RJ722Created Jun 28, 2020Updated Aug 16, 2026
Labelsbugcore

If I enter, line-by-line, a nonlocal statement in an inner function, ipython throws a syntax error, e.g.:

In [1]: def a():
   ...:     def b():
   ...:         nonlocal c
  File "<ipython-input-1-dbf568c371f7>", line 3
    nonlocal c
    ^
SyntaxError: no binding for nonlocal 'c' found

The following code runs fine on python interpreter (and if execured as a file):

python
def a():
    def b():
        nonlocal c
        return c + 1
    c = 1
    b()
    return c

print(a())

This was originally reported by Chris Angelico as a bug in IDLE -- https://bugs.python.org/issue19335

I can successfully reproduce it on IPython 7.16.1. Here is the output of python -c "import IPython; print(IPython.sys_info())":

python
{'commit_hash': '2486838d9',
 'commit_source': 'installation',
 'default_encoding': 'UTF-8',
 'ipython_path': '/Users/rahuljha/.virtualenvs/vulture/lib/python3.7/site-packages/IPython',
 'ipython_version': '7.16.1',
 'os_name': 'posix',
 'platform': 'Darwin-16.7.0-x86_64-i386-64bit',
 'sys_executable': '/Users/rahuljha/.virtualenvs/vulture/bin/python',
 'sys_platform': 'darwin',
 'sys_version': '3.7.0b5 (v3.7.0b5:abb8802389, May 30 2018, 20:03:19) \n'
                '[Clang 6.0 (clang-600.0.57)]'}