Skip to content

gh-135110: Fix misleading generator.close() documentation #135152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 26, 2025

Conversation

cdenihan
Copy link
Contributor

@cdenihan cdenihan commented Jun 4, 2025

This PR fixes the misleading documentation for generator.close() that incorrectly stated the method 'raises a GeneratorExit' to the caller.

Problem

The current documentation in both Doc/reference/expressions.rst and Doc/howto/functional.rst states that generator.close() 'raises a GeneratorExit' which is misleading because:

  • The method sends the GeneratorExit exception to the generator internally
  • The method returns None to the caller
  • The method does not raise the exception to the caller

Solution

Changed 'raises' to 'sends' in both documentation files for accuracy and consistency:

  1. Doc/reference/expressions.rst (line 628-629):

    • Before: 'Raises a GeneratorExit at the point where the generator function was paused'
    • After: 'Sends a GeneratorExit exception to the generator at the point where the generator function was paused'
  2. Doc/howto/functional.rst (line 605):

    • Before: 'raises a GeneratorExit exception inside the'
    • After: 'sends a GeneratorExit exception inside the'

Testing

The behavior described in the fixed documentation can be verified by the examples in the original issue report, which show that generator.close() always returns None.

Fixes #135110


📚 Documentation preview 📚: https://cpython-previews--135152.org.readthedocs.build/

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.

Changed 'raises' to 'sends' in both Doc/reference/expressions.rst and
Doc/howto/functional.rst for consistency and accuracy.

Fixes python#135110
@python-cla-bot
Copy link

python-cla-bot bot commented Jun 4, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Jun 4, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Jun 4, 2025
@cdenihan cdenihan changed the title Fix misleading generator.close() documentation gh-135110: Fix misleading generator.close() documentation Jun 4, 2025
Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

I'm a little worried about the use of the word "send" here, especially considering generator.send() is a thing. I think "raises" is fine, we should just make it clearer that the actual yield expressions are what raise. Maybe we could say that close() is equivalent to throw(GeneratorExit)?

cdenihan added 2 commits June 20, 2025 11:44
Replace 'sends' with 'raises' and clarify that close() is equivalent to throw(GeneratorExit). This makes the documentation clearer and avoids confusion with generator.send().
@cdenihan cdenihan requested a review from ZeroIntensity June 20, 2025 15:57
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@ZeroIntensity
Copy link
Member

It looks like the CLA got screwed up.

@cdenihan
Copy link
Contributor Author

cdenihan commented Jun 20, 2025

Just signed it again, should be all good now.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Thanks!

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 26, 2025
@ZeroIntensity ZeroIntensity merged commit 0d76dcc into python:main Jun 26, 2025
29 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Jun 26, 2025
@miss-islington-app
Copy link

Thanks @cdenihan for the PR, and @ZeroIntensity for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 26, 2025
…thonGH-135152)

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.
(cherry picked from commit 0d76dcc)

Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 26, 2025
…thonGH-135152)

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.
(cherry picked from commit 0d76dcc)

Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Jun 26, 2025

GH-135985 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jun 26, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jun 26, 2025

GH-135986 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jun 26, 2025
ZeroIntensity pushed a commit that referenced this pull request Jun 26, 2025
…H-135152) (GH-135986)

gh-135110: Fix misleading `generator.close()` documentation (GH-135152)

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.
(cherry picked from commit 0d76dcc)

Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
ZeroIntensity pushed a commit that referenced this pull request Jun 26, 2025
…H-135152) (GH-135985)

gh-135110: Fix misleading `generator.close()` documentation (GH-135152)

The documentation incorrectly stated that generator.close() 'raises' a
GeneratorExit exception. This was misleading because the method doesn't
raise the exception to the caller - it sends the exception internally
to the generator and returns None.
(cherry picked from commit 0d76dcc)

Co-authored-by: Connor Denihan <188690869+cdenihan@users.noreply.github.com>
@cdenihan cdenihan deleted the fix-generator-close-docs branch June 26, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

generator.close() never raises GeneratorExit
2 participants