New GitHub issue #92989 from stevendaprano:<br>

<hr>

<pre>
Using nonlocal inside a non-nested function to refer to a global gives the syntax error "no binding for nonlocal 'name' found" even if the global 'name' exists. This is confusing. Improve the error message by making it clear that the problem is not that variable 'name' doesn't exist, but that the function is not nested inside another function containing a variable 'name'.

Suggestion: if `nonlocal` is used in a non-nested function where there is no surrounding function (e.g. either a global level function or one defined inside a class), rather than reporting that there is no binding found, make the error message more specific:

"nonlocal can only be used in function nested inside another function; perhaps try global instead?"

As evidence that this error message is not clear, see #92983 where the submitter @snoopyjc was mislead into thinking that the problem was the loop variable rather than the lack of nested function.

</pre>

<hr>

<a href="https://github.com/python/cpython/issues/92989">View on GitHub</a>
<p>Labels: type-feature</p>
<p>Assignee: </p>