bagof-dispatchers
bagof.dispatchers provides multiple dispatch for Python. Register several
implementations of a function, and each call runs the implementation whose
declared parameter types most specifically match the runtime types of the
arguments, the way Julia's multiple dispatch works.
>>> from bagof.dispatchers import dispatch
>>> @dispatch
... def area(width: int, height: int) -> int:
... return width * height
>>> @dispatch
... def area(radius: float) -> float:
... return 3 * radius * radius
>>> area(3, 4)
12
>>> area(2.0)
12.0
The second def named area does not replace the first. It adds an
overload, and the name area stays bound to a single dispatched function
that chooses between the two on every call. [Getting started][getting-started]
walks through registering overloads in more detail.
What sets it apart
- Dispatch reads the full vocabulary of type hints, not only plain classes.
Unions, literals, generics,
TypedDict, and type variables ordered by their declared variance all narrow which overload applies. See [Variance][variance]. Exact[C]matches a value only when its type is exactlyC, for the cases where a subclass should not quietly take over another overload's place;boolis otherwise also anint. See [Exact types][exact-types].Hint[X]dispatches on a type hint passed as a value, matching the hints that are sub-hints ofX, so a function handed a hint can branch on what the hint is. See [Dispatching on hints][hints-as-values].Super[C]accepts a value whose class isCor a class above it, and insideType[...]orHint[...]it accepts the class or hintCand everything above it, soType[Super[Dog]]accepts the classDogand each class it derives from.Between[L, U]bounds from both sides at once, soBetween[Dog, Animal]accepts a value whose class lies betweenDogandAnimal, both included, andType[Between[Dog, Animal]]accepts those classes themselves. As the type argument of an invariant generic, a bound names a range of arguments, soList[Super[int]]accepts a list declared to holdint,numbers.Integralorobject. See [Exact types][exact-types] and [Variance][variance].- A call that matches no overload, or that matches two equally specific
overloads, is always a clear error rather than a silent guess.
NoMethodErroris aTypeErrorcarrying the closest candidates, andAmbiguousMethodErrornames the overloads that tie. A function'sambiguities()finds such ties before any call is made, so a test suite can assert that there are none. See [When two overloads are equally specific][ambiguous] and [When nothing matches][no-match]. - Dispatch is aware of argument names as well as positions, so a call binds by position or by keyword, either way, against the same overloads. See [Registering overloads][registering].
- Registries are explicit.
@dispatchkeeps functions separate per module; build a [Dispatcher][registries] when several modules should extend one shared function instead. - The library supports Python 3.8 through the current release, and depends
on nothing beyond [
typing_extensions][typing_extensions].
Install
pip install bagof-dispatchers
Status
The project is at an early stage: the API is still settling, and some of it may still move. Issues and ideas are welcome at [bagofseeds/bagof-dispatchers][issues].
Metadata
Release files for bagof-dispatchers 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bagof_dispatchers-0.3.2.tar.gz | 330.0 kB | Without provenance | Details for bagof_dispatchers-0.3.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bagof_dispatchers-0.3.2-py3-none-any.whl | Python 3 | none | any | Details for bagof_dispatchers-0.3.2-py3-none-any.whl |
Total release size: 507.4 kB
Release files / bagof_dispatchers-0.3.2.tar.gz
| Download URL | bagof_dispatchers-0.3.2.tar.gz |
|---|---|
| Size | 330.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e556c1d38b9db8d74ba93e2a808d0e99fd02650638282ad777381d2c199ca4f
|
|
BLAKE2b-256 checksum How to use checksums |
13849e3e4bec66c7cdef2213ea526d9dec51ad3c9632bc914739d69376744b62
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / bagof_dispatchers-0.3.2-py3-none-any.whl
| Download URL | bagof_dispatchers-0.3.2-py3-none-any.whl |
|---|---|
| Size | 177.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5483cd1091a46c0092c90df386927f7de4ec98305664148b647a39a9a2ecc552
|
|
BLAKE2b-256 checksum How to use checksums |
f6243c85eb691c61e774a4acd23e7975c3fc26e49584eb697372e8ef3882696a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for bagof-dispatchers 0.3.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.