8000 Doc: Clarify the return type of Event.wait when timeout is used. by pelson · Pull Request #104168 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Doc: Clarify the return type of Event.wait when timeout is used. #104168

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 5 commits into from
Feb 26, 2024

Conversation

pelson
Copy link
Contributor
@pelson pelson commented May 4, 2023

Explicitly clarify the return type of Event.wait(timeout) in the documentation.

Without this, it takes some logic to figure out what will occur (or whether perhaps a TimeoutError might be raised, for example). It would be easy to miss this, and not check the return type.


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

@bedevere-bot bedevere-bot added docs Documentation in the Doc dir skip news awaiting review labels May 4, 2023
@ghost
Copy link
ghost commented May 4, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@@ -989,7 +989,7 @@ method. The :meth:`~Event.wait` method blocks until the flag is true.

When the timeout argument is present and not ``None``, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof).
(or fractions thereof). When the timeout occurs, ``False`` will be returned.
Copy link
Member

Choose a reason for hiding this comment

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

It is already implicitly documented in the following paragraph. If it is not clear, it would be better to rewrite that paragraph.

Copy link
Contributor Author
@pelson pelson Feb 2, 2024

Choose a reason for hiding this comment

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

The paragraph you refer to currently states:

This method returns True if and only if the internal flag has been set to
true, either before the wait call or after the wait starts, so it will
always return True except if a timeout is given and the operation
times out.

It is certainly not clear enough IMO that False is the return of a timeout (rather than raising, for example). My rationale for putting it where I did was to keep the documentation of the timeout behaviour in a single paragraph, whereas the quoted paragraph, IMO, is less about timeout, and more about a general return value.

If you prefer, I can propose:

This method returns True if and only if the internal flag has been set to
true, either before the wait call or after the wait starts, so it will
always return True except if a timeout is given and the operation
times out. If a timeout occurs, False will be returned.

Copy link
Member

Choose a reason for hiding this comment

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

I think that this paragraph is more about the valid values and meaning of the timeout parameter (None or the time period in seconds, not necessary integer) than about the behavior.

I like your second idea more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added 2 commits. The first is as discussed. The second strips down some unnecessary and confusing description (removing the except clause from the sentence)

@@ -996,9 +996,8 @@ method. The :meth:`~Event.wait` method blocks until the flag is true.
(or fractions thereof).

This method returns ``True`` if and only if the internal flag has been set to
Copy link
Member

Choose a reason for hiding this comment

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

"if the internal flag has been set" is vague. In practice, it means that the set() method was called, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that is is vague. I would also like to use that style of language, rather than talking about internal flags tbh.

I've re-written the docstring entirely based on the reviews, but haven't yet used the simpler language you suggested - the only hesitation is that we have to also mention clear() and the fact that set() may have been called before wait() if we want to be 100% precise (I'm not sure it helps with clarity to be so precise though).

Copy link
Member

Choose a reason for hiding this comment

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

The wait entry immediately follows the set and clear entries, so I think it should be clear enough that those are the functions for manipulating the 'internal flag'.

always return ``True`` except if a timeout is given and the operation
8000 times out.
true, either before the wait call or after the wait starts.
If a timeout is given and the operation times out, ``False`` will be returned.
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand what is the expected return value if no timeout is given and set() was not called: the function just doesn't return and block until set() is called?

Copy link
Member

Choose a reason for hiding this comment

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

Several paragraphs above:

      Block until the internal flag is true.  If the internal flag is true on
      entry, return immediately.  Otherwise, block until another thread calls
      :meth:`.set` to set the flag to true, or until the optional timeout occurs.

Copy link
Member

Choose a reason for hiding this comment

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

My rewrite combines paragraphs is a way that I think is clearer in less words. I think this ready to merge now.

Copy link
Member
@terryjreedy terryjreedy left a comment

Choose a reason for hiding this comment

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

I think the first as well as the third paragraph should be rewritten together and condensed because together they currently jumble together blocking and returning in a confusing and overly wordy manner and because the first sentence is incorrect. (Correcting the incorrectness is part of the reason for needing too many words.) I suggest the following which first gives the two reasons for blocking and then defines the return value as indicating which block reason became false, triggering the return.

Block as long as the internal flag controlled by set and clear is false and the timeout, if any, has not expired. Return the reason for returning: True if returning because the internal flag was initially True or set True thereafter; False if a timeout expires.

Leave the 2nd sentence alone if correct, except that the final parentheses are not needed.

@bedevere-app
Copy link
bedevere-app bot commented Feb 13, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pelson pelson force-pushed the doc/threading-event branch 2 times, most recently from 65c9ed2 to f7f6623 Compare February 13, 2024 08:14
@terryjreedy terryjreedy self-assigned this Feb 23, 2024
@terryjreedy terryjreedy added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Feb 23, 2024
@terryjreedy
Copy link
Member

I think this ready to merge now and will do it unless one of you two object or prefer to do so.

@pelson
Copy link
Contributor Author
pelson commented Feb 26, 2024

LGTM. Thanks for your input 👍

@serhiy-storchaka serhiy-storchaka merged commit 37f5d06 into python:main Feb 26, 2024
@miss-islington-app
Copy link

Thanks @pelson for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 26, 2024
…onGH-104168)

(cherry picked from commit 37f5d06)

Co-authored-by: Phil Elson <pelson.pub@gmail.com>
@bedevere-app
Copy link
bedevere-app bot commented Feb 26, 2024

GH-115938 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Feb 26, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 26, 2024
…onGH-104168)

(cherry picked from commit 37f5d06)

Co-authored-by: Phil Elson <pelson.pub@gmail.com>
@bedevere-app
Copy link
bedevere-app bot commented Feb 26, 2024

GH-115939 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Feb 26, 2024
@serhiy-storchaka
Copy link
Member

Thank you for your contribution @pelson and for your review @terryjreedy.

serhiy-storchaka pushed a commit that referenced this pull request Feb 26, 2024
GH-104168) (GH-115938)

(cherry picked from commit 37f5d06)

Co-authored-by: Phil Elson <pelson.pub@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Feb 26, 2024
GH-104168) (GH-115939)

(cherry picked from commit 37f5d06)

Co-authored-by: Phil Elson <pelson.pub@gmail.com>
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this pull request Mar 4, 2024
@pelson pelson deleted the doc/threading-event branch August 6, 2024 14:37
LukasWoodtli pushed a commit to LukasWoodtli/cpython that referenced this pull request Jan 22, 2025
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 issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0