A replacement function for single dispatch that allows dispatching based on type hints for all arguments.
Project description
multiarg-dispatch
multiarg-dispatch is a Python package that extends the functionality of functools.singledispatch to allow dispatching based on type hints for all arguments, not just the first one. It provides a simple decorator to define generic functions with multiple implementations depending on argument types.
Features
- Dispatch functions based on the types of all arguments, including keyword arguments.
- Supports union types in type hints.
- Raises a warning if arguments have default values (since defaults are not considered during dispatch).
- Type checking enforced at registration: all parameters must have type hints.
- Fully compatible with Python >3.11.
- Note that the registry uses strong references, so for garbage collection do not forget to delete the function that uses multidispatch.
- Note that local classes cannot be used as type hints, since these are accessible globally to retrieve as type hint.
Installation
Install from PyPI:
pip install multiarg-dispatch
Or with uv:
uv add multiarg-dispatch
Usage
from multiarg_dispatch import multidispatch, DispatchWarning
@multidispatch
def test_func(a, b=None):
return "default"
@test_func.register
def _(a: int, b: str = "default") -> str:
return f"int:{a},str:{b}"
@test_func.register
def _(a: str, b: list = []) -> str:
if b is None:
b = []
return f"str:{a},list:{b}"
@test_func.register
def _(a: float, b: str | list = "default") -> str:
return f"float:{a},union:{b}"
print(test_func(10)) # int:10,str:default
print(test_func("hello", [])) # str:hello,list:[]
print(test_func(3.14, "extra")) # float:3.14,union:extra
API
multidispatch(func)
Decorator to make a function multi-dispatch capable.
register(func): Register a new implementation based on type hints.dispatch(cls): Retrieve the implementation for given types.registry: Read-only view of all registered implementations.
DispatchWarning
Warning raised when dispatching might be affected by defaults.
Contributing
Contributions are welcome! Please follow these guidelines:
- Fork the repository.
- Create a feature branch (
git checkout -b feature/my-feature). - Make your changes.
- Ensure all tests pass (
pytestrecommended). - Open a Pull Request with a clear description of your changes.
Please follow PEP 8 style guidelines and include type hints where appropriate.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file multiarg_dispatch-0.1.0.tar.gz.
File metadata
- Download URL: multiarg_dispatch-0.1.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c8238cb4130bab1c0689e94c6e5a6f43d6e1f5c907c942bdb082effe67a78c6
|
|
| MD5 |
5652a586f145b8359493ee27ceb49e19
|
|
| BLAKE2b-256 |
c220057c7447093ebc0498bebecd75b2071a979ff721b6bc053242783c784847
|
File details
Details for the file multiarg_dispatch-0.1.0-py3-none-any.whl.
File metadata
- Download URL: multiarg_dispatch-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25e6da74df58d5c95eaf403a726345327b1fa2a7d41e11a4be57db4685e9a2ae
|
|
| MD5 |
2fd9f81d1ab119876a8b9485370f49e4
|
|
| BLAKE2b-256 |
e4fb64c6ad3291db5e1e12536f33cd545ca9ccb73fe82993a0bac8414c041423
|