Skip to content

gh-58749: Add test_nonlocal.py to ensure correct behavior of nonlocal statements#126738

Open
bombs-kim wants to merge 6 commits into
python:mainfrom
bombs-kim:simple-stmts-nonlocal
Open

gh-58749: Add test_nonlocal.py to ensure correct behavior of nonlocal statements#126738
bombs-kim wants to merge 6 commits into
python:mainfrom
bombs-kim:simple-stmts-nonlocal

Conversation

@bombs-kim

@bombs-kim bombs-kim commented Nov 12, 2024

Copy link
Copy Markdown
Contributor

As suggested by @ncoghlan:

  • Added test_nonlocal.py
  • Added a Documentation NEWS entry
  • Added a Tests NEWS entry

In tests, I've decided to use simply the variable names, rather than using f.__closure__[0].cell_contents. It seems to me that both approaches can ensure the correct behavior of nonlocal statements and simply using the variable names would make the test code easier to understand. I will change it back to f.__closure__[0].cell_contents if this is not desirable.

@bombs-kim

Copy link
Copy Markdown
Contributor Author

@ncoghlan Could you give this PR a review?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests don't need their own news entry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback. I've removed the Tests NEWS entry file.

@bombs-kim bombs-kim changed the title gh-58749: Add test_nonlocal.py to ensure correct behavior of nonlocal statements and add NEWS entries gh-58749: Add test_nonlocal.py to ensure correct behavior of nonlocal statements Dec 23, 2025
@ncoghlan

ncoghlan commented Jan 1, 2026

Copy link
Copy Markdown
Contributor

D'oh, this fell off my radar after #126523 was merged, so the NEWS entry will need some tweaks now :(

cc @hugovk since the docs NEWS entry was actually supposed to be there for 3.14.0, so I'm wondering if we should be editing it into the change log for that release rather than whichever maintenance release it ends up landing in (I'll make a wording suggestion based on in going into the 3.15.0 and 3.14.x notes)

@picnixz

picnixz commented Jan 1, 2026

Copy link
Copy Markdown
Member

I thiunk we can just remove the NEWS entry. It's only increasing the test coverage. If you're worried about where to put the NEWS entry, I'd suggest that we have a separate PR that adds the NEWS entry and then is backported.

Comment on lines +1 to +5
Corrected the language specification to match CPython's behavior: names in a
`global` statement are only restricted from being used or assigned prior to
their declaration in scope. Additional limitations previously suggested by
the spec do not apply in CPython.
Change by Beomsoo Kim

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Corrected the language specification to match CPython's behavior: names in a
`global` statement are only restricted from being used or assigned prior to
their declaration in scope. Additional limitations previously suggested by
the spec do not apply in CPython.
Change by Beomsoo Kim
3.14.0 corrected the language specification for the `global` statement to match
CPython's behavior: names in a `global` statement are restricted from being used
or assigned prior to the declaration statement, but can otherwise be bound using
any valid name binding syntax. The previous text erroneously stated that some
name binding syntax (such as loop iteration variables) could not assign to globals.
Change by Beomsoo Kim

@ncoghlan

ncoghlan commented Jan 1, 2026

Copy link
Copy Markdown
Contributor

We did conclude in the original issue that the docs edit was significant enough that it should have had a NEWS entry.

It was sufficiently borderline that I'm not surprised nobody complained about it (it was one of those cases where what everyone assumed the spec said was not, in fact, what the text actually contained), so I won't argue for it if we decide that the inadvertent omissions makes it no longer worth worrying about.

@picnixz

picnixz commented Jan 1, 2026

Copy link
Copy Markdown
Member

Maybe I phrased it poorly, but this specific PR does not need a NEWS entry. I think it's easier if we have another PR that would just add the NEWS entry wherever needed, whether it's directly in the 3.14.0 section or in the 3.14.2 section so that it's at the "top" of the page (though, when it's 3.14.3, it won't be at the top anymore so... it doesn't really matter that we put it manually in 3.14.0)

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time. tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants