Metadata-Version: 2.4
Name: anyio-ext
Version: 0.1.0
Summary: A collection of helpful utilities for anyio.
Project-URL: Homepage, https://github.com/tastyware/anyio-ext
Project-URL: Funding, https://github.com/sponsors/tastyware
Project-URL: Source, https://github.com/tastyware/anyio-ext
Project-URL: Changelog, https://github.com/tastyware/anyio-ext/releases
Author-email: Graeme Holliday <graeme@tastyware.dev>
License: MIT License
        
        Copyright (c) 2026 tastyware
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Framework :: AnyIO
Classifier: Framework :: AsyncIO
Classifier: Framework :: Django
Classifier: Framework :: FastAPI
Classifier: Framework :: Flask
Classifier: Framework :: Trio
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: Unix
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Systems Administration
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: anyio>=4.13.0
Description-Content-Type: text/markdown

[![PyPI](https://img.shields.io/pypi/v/anyio-ext)](https://pypi.org/project/anyio-ext)
[![Downloads](https://static.pepy.tech/badge/anyio-ext)](https://pepy.tech/project/anyio-ext)
[![Release](https://img.shields.io/github/v/release/tastyware/anyio-ext?label=release%20notes)](https://github.com/tastyware/anyio-ext/releases)

# anyio-ext

A collection of helpful utilities for [anyio](https://anyio.readthedocs.io/en/latest/).

## Features

- Powerful, easy-to-use caching decorator
- Typed `asyncio.gather` implementation
- Generic `Queue` and `Stack` primitives

## Installation

```console
$ pip install anyio-ext
```

## Getting started

`gather()` is similar to `asyncio.gather()`:

```python
from anyio import sleep
from anyio_ext import gather

results = await gather(*[sleep(i) for i in range(3)])
print(results)  # (None, None, None)
```

Caching is implemented with a decorator:

```python
from anyio_ext import cached

@cached(ttl=60)
async def my_task() -> int: ...
```

## Queues

Queue and stack implementations work similarly:

```python
from anyio_ext import Queue, Stack

queue = Queue[int](max_size=32)
stack = Stack[int](max_size=32)

await queue.push(1)
await queue.push(2)
print(await queue.pop())  # 1
await stack.push(1)
await stack.push(2)
print(await stack.pop())  # 2
```

## Advanced caching

Cache keys are generated by hashing arguments. You can exclude non-serializable arguments from cache key construction:

```python
from sqlalchemy.ext.asyncio import AsyncSession

@cached(ttl=60, exclude={"session"})
async def my_task(session: AsyncSession) -> int: ...
```

You can also customize which parts of arguments get hashed:

```python
@cached(
    ttl=60,
    key_fns={
        # hash just the ID, not the entire model
        "user": lambda u: u.id,
        # hash a couple relevant fields
        "message": lambda m: (m.type, m.timestamp),
    },
)
async def my_task(user: User, message: Message) -> int: ...
```

You can easily invalidate keys by passing the same arguments:

```python
@cached(ttl=60)
async def my_task(time: int) -> int: ...

await my_task.invalidate(3)
```

Simple cache statistics are available:

```python
print(my_task.hits, my_task.misses, len(my_task))  # 999 1 1
```

Methods can also be cached:

```python
class MyClass:
    @cached(ttl=60)
    async def my_task(self, time: int) -> int: ...

instance = MyClass()
await instance.my_task(3)
instance.my_task.invalidate(3)
print(MyClass.my_task.hits, MyClass.my_task.misses, len(MyClass.my_task))  # 0 1 0
```

Note that statistics are on a per-class basis, but calls to `my_task()` are on a per-instance basis (even though behind the scenes, the cache is shared across all instances). `self` is one of the parameters used for caching by default, but it won't cause memory leaks as keys don't store references to arguments.
