Skip to content

Define trio.Event.__bool__() to reduce bugs #3238

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

Open
Zac-HD opened this issue Apr 2, 2025 · 3 comments
Open

Define trio.Event.__bool__() to reduce bugs #3238

Zac-HD opened this issue Apr 2, 2025 · 3 comments

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Apr 2, 2025

I've now hit enough bugs where someone wrote if event: rather than if event.is_set(): that I think Event.__bool__() should raise an error of some kind, likely NotImplementedError("Trio events cannot be treated as bools; consider using 'event.is_set()'").

I could be argued into def __bool__(self): return self.is_set() but disprefer this - "there should be one obvious way to do it". Thoughts?

I don't think there's as urgent a need on other classes, but we could consider e.g. Condition and Semaphore too...

@Zac-HD Zac-HD added the polish label Apr 2, 2025
@A5rocks
Copy link
Contributor

A5rocks commented Apr 2, 2025

Since trio.Event is marked as a final class, mypy (at least) supports --enable-error-code=truthy-bool that will warn on if event. (actually I'm not even sure if that requires being marked as final)

I'm not sure how useful something at runtime is compared to that though.

@Zac-HD
Copy link
Member Author

Zac-HD commented Apr 2, 2025

Seems very useful for everyone who doesn't use mypy, including those on other typecheckers! (like me, for example 😅)

@jakkdl
Copy link
Member

jakkdl commented Apr 2, 2025

seems good, and should be a quick fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants