Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
when an async test raise an outcome exception (e.g., ``pytest.skip()``, ``pytest.xfail()``,
or ``pytest.fail()``)
(`#1179 <https://github.com/agronholm/anyio/issues/1179>`_; PR by @EmmanuelNiyonshuti)
- Fixed ``CapacityLimiter.total_tokens`` rejecting a value of ``0`` when the limiter was
instantiated outside of an event loop, contradicting the documented behavior of
allowing 0 total tokens
(`#1183 <https://github.com/agronholm/anyio/pull/1183>`_; PR by @nyxst4ck)

**4.14.0**

Expand Down
4 changes: 2 additions & 2 deletions src/anyio/_core/_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ def total_tokens(self) -> float:
def total_tokens(self, value: float) -> None:
if not isinstance(value, int) and value is not math.inf:
raise TypeError("total_tokens must be an int or math.inf")
elif value < 1:
raise ValueError("total_tokens must be >= 1")
elif value < 0:
raise ValueError("total_tokens must be >= 0")

if self._internal_limiter is None:
self._total_tokens = value
Expand Down
8 changes: 8 additions & 0 deletions tests/test_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,14 @@ async def use_limiter() -> None:
backend_options=anyio_backend_options,
)

def test_zero_tokens_outside_event_loop(self) -> None:
# Regression test for the CapacityLimiterAdapter setter rejecting 0,
# which contradicted the 4.12 behavior of allowing 0 total tokens
limiter = CapacityLimiter(1)
limiter.total_tokens = 0
assert limiter.total_tokens == 0
assert CapacityLimiter(0).total_tokens == 0

async def test_total_tokens_as_kwarg(self) -> None:
# Regression test for #515
limiter = CapacityLimiter(total_tokens=1)
Expand Down
Loading