Skip to content

Commit a97f905

Browse files
committed
Define trio.Event.__bool__() to reduce bugs
Closes #3238
1 parent 77dd921 commit a97f905

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/trio/_sync.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import math
4-
from typing import TYPE_CHECKING, Protocol
4+
from typing import TYPE_CHECKING, NoReturn, Protocol
55

66
import attrs
77

@@ -112,6 +112,14 @@ def statistics(self) -> EventStatistics:
112112
"""
113113
return EventStatistics(tasks_waiting=len(self._tasks))
114114

115+
if not TYPE_CHECKING:
116+
117+
def __bool__(self) -> NoReturn:
118+
"""Raise NotImplementedError."""
119+
raise NotImplementedError(
120+
"Trio events cannot be treated as bools; consider using 'event.is_set()'"
121+
)
122+
115123

116124
class _HasAcquireRelease(Protocol):
117125
"""Only classes with acquire() and release() can use the mixin's implementations."""

0 commit comments

Comments
 (0)