Skip to main content

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 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.
  • Exact[C] matches a value only when its type is exactly C, for the cases where a subclass should not quietly take over another overload's place; bool is otherwise also an int. See Exact types.
  • Hint[X] dispatches on a type hint passed as a value, matching the hints that are sub-hints of X, so a function handed a hint can branch on what the hint is. See Dispatching on hints.
  • Super[C] accepts a value whose class is C or a class above it, and inside Type[...] or Hint[...] it accepts the class or hint C and everything above it, so Type[Super[Dog]] accepts the class Dog and each class it derives from. Between[L, U] bounds from both sides at once, so Between[Dog, Animal] accepts a value whose class lies between Dog and Animal, both included, and Type[Between[Dog, Animal]] accepts those classes themselves. As the type argument of an invariant generic, a bound names a range of arguments, so List[Super[int]] accepts a list declared to hold int, numbers.Integral or object. See Exact types and Variance.
  • A call that matches no overload, or that matches two equally specific overloads, is always a clear error rather than a silent guess. NoMethodError is a TypeError carrying the closest candidates, and AmbiguousMethodError names the overloads that tie. A function's ambiguities() 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 and When nothing matches.
  • 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.
  • Registries are explicit. @dispatch keeps functions separate per module; build a Dispatcher 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.

Install

pip install git+https://github.com/bagofseeds/bagof-dispatchers.git

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.

bagof.dispatchers is the low-level root of the bagof family. It owns the hint subtype relation and the introspection helpers the other bags build on; see Part of bagof and the project overview. For how it compares with plum, multipledispatch, functools.singledispatch, and Julia's own multiple dispatch, see the comparison page.

Metadata

Release files for bagof-dispatchers 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bagof-dispatchers 0.3.1
File Size Uploaded
bagof_dispatchers-0.3.1.tar.gz 327.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bagof-dispatchers 0.3.1
File Interpreter ABI Platform
bagof_dispatchers-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 502.5 kB

Release files / bagof_dispatchers-0.3.1.tar.gz

Download URL bagof_dispatchers-0.3.1.tar.gz
Size 327.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6e58b648a6a53aa599360a5c8dc0e1f19a336e63d919ece347f58752c844fd5c
BLAKE2b-256 checksum
How to use checksums
25533f057b9c6ca61b1829b5f878e45acc16e08b7c9f55833a6c3c6c8ebda2dc
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.1-py3-none-any.whl

Download URL bagof_dispatchers-0.3.1-py3-none-any.whl
Size 175.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ea010ec3f80f722973ef5a88daa61dcc8c8a1d45af97604ad946a3063cc0849
BLAKE2b-256 checksum
How to use checksums
6533d69313cca73892b62df2ec5984a8ea15241feff0d86230e0fa8256e35f8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.3.2

2 release files

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page