Skip to content

Commit 2f4c529

Browse files
Allow CapacityLimiter to have zero total_tokens
1 parent 77dd921 commit 2f4c529

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/trio/_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ def total_tokens(self) -> int | float:
251251
def total_tokens(self, new_total_tokens: int | float) -> None: # noqa: PYI041
252252
if not isinstance(new_total_tokens, int) and new_total_tokens != math.inf:
253253
raise TypeError("total_tokens must be an int or math.inf")
254-
if new_total_tokens < 1:
255-
raise ValueError("total_tokens must be >= 1")
254+
if new_total_tokens < 0:
255+
raise ValueError("total_tokens must be >= 0")
256256
self._total_tokens = new_total_tokens
257257
self._wake_waiters()
258258

src/trio/_tests/test_sync.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ async def child() -> None:
4949

5050

5151
async def test_CapacityLimiter() -> None:
52+
assert CapacityLimiter(0).total_tokens == 0
5253
with pytest.raises(TypeError):
5354
CapacityLimiter(1.0)
54-
with pytest.raises(ValueError, match=r"^total_tokens must be >= 1$"):
55+
with pytest.raises(ValueError, match=r"^total_tokens must be >= 0$"):
5556
CapacityLimiter(-1)
5657
c = CapacityLimiter(2)
5758
repr(c) # smoke test
@@ -139,10 +140,10 @@ async def test_CapacityLimiter_change_total_tokens() -> None:
139140
with pytest.raises(TypeError):
140141
c.total_tokens = 1.0
141142

142-
with pytest.raises(ValueError, match=r"^total_tokens must be >= 1$"):
143-
c.total_tokens = 0
143+
with pytest.raises(ValueError, match=r"^total_tokens must be >= 0$"):
144+
c.total_tokens = -1
144145

145-
with pytest.raises(ValueError, match=r"^total_tokens must be >= 1$"):
146+
with pytest.raises(ValueError, match=r"^total_tokens must be >= 0$"):
146147
c.total_tokens = -10
147148

148149
assert c.total_tokens == 2

0 commit comments

Comments
 (0)