Skip to content

Commit

Permalink
Fixed inability to pass total_tokens as a keyword argument on the tri…
Browse files Browse the repository at this point in the history
…o backend (#666)

Fixes #515.
  • Loading branch information
agronholm committed Jan 13, 2024
1 parent 6c70118 commit 7878248
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

- Added support for the Python 3.12 ``walk_up`` keyword argument in
``anyio.Path.relative_to()`` (PR by Colin Taylor)
- Fixed passing ``total_tokens`` to ``anyio.CapacityLimiter()`` as a keyword argument
not working on the ``trio`` backend
(`#515 <https://github.com/agronholm/anyio/issues/515>`_)

**4.2.0**

Expand Down
16 changes: 13 additions & 3 deletions src/anyio/_backends/_trio.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,14 +631,24 @@ def set(self) -> None:

class CapacityLimiter(BaseCapacityLimiter):
def __new__(
cls, *args: Any, original: trio.CapacityLimiter | None = None
cls,
total_tokens: float | None = None,
*,
original: trio.CapacityLimiter | None = None,
) -> CapacityLimiter:
return object.__new__(cls)

def __init__(
self, *args: Any, original: trio.CapacityLimiter | None = None
self,
total_tokens: float | None = None,
*,
original: trio.CapacityLimiter | None = None,
) -> None:
self.__original = original or trio.CapacityLimiter(*args)
if original is not None:
self.__original = original
else:
assert total_tokens is not None
self.__original = trio.CapacityLimiter(total_tokens)

async def __aenter__(self) -> None:
return await self.__original.__aenter__()
Expand Down
5 changes: 5 additions & 0 deletions tests/test_synchronization.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,8 @@ async def use_limiter() -> None:
backend=anyio_backend_name,
backend_options=anyio_backend_options,
)

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

0 comments on commit 7878248

Please sign in to comment.