Registry / serialization / multimethod

multimethod

JSON →
library2.1pypypi✓ verified 23d ago

Multimethod provides a decorator for adding multiple argument dispatching to functions in Python. It aims for simplicity and speed, utilizing type annotations to create a multimethod object that registers functions based on argument types. The library supports various type hints and advanced dispatching rules. It is currently at version 2.0.2 and has an active development and release cadence, with several releases per year.

pip install multimethod
INSTALL
IMPORT
SIG · MULTIMETHOD
M
multimethod
serializationpythonv2.1
Install
1.5s avg
Import
31ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.032s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.030s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

multimethod
from multimethod import multimethod
The primary decorator for defining multimethods.
multimeta
from multimethod import multimeta
Metaclass for automatically converting class methods to multimethods.
multidispatch
from multimethod import multidispatch
Provides compatibility with the functools.singledispatch style, supporting keyword arguments for dispatching in this specific variant.

This example demonstrates how to define a function `func` with multiple implementations that are dispatched based on the runtime types of its arguments. The `@multimethod` decorator is applied to each implementation.

from multimethod import multimethod @multimethod def func(x: int, y: float): return f"Int and Float: {x} + {y}" @multimethod def func(x: float, y: int): return f"Float and Int: {x} + {y}" @multimethod def func(x: str, y: str): return f"Strings: {x} {y}" print(func(1, 2.0)) print(func(3.0, 4)) print(func("Hello", "World"))
Debug
Known issues
breakingVersion 2.0 removed the `overload` decorator and the mechanism for resolving ambiguity using positional distance. Code relying on these features will break.
fix
Migrate from `overload` to `@multimethod` with `isa` checks if predicate dispatching is needed, or adjust code that relied on positional distance for ambiguity resolution.
affects: >=2.0
gotchaKeyword-only parameters are supported for type annotation, but `multimethod` (the main decorator) itself dispatches solely on positional arguments. Keyword arguments are ignored during the dispatching process. If keyword dispatch is needed, consider `multidispatch`.
fix
Ensure that the order and types of positional arguments are sufficient for dispatching. If keyword dispatch is critical, `multidispatch` might be an alternative within the library or external solutions.
affects: All versions
gotchaTyping aliases (e.g., `typing.List`, `typing.Dict`) do not consistently support the `issubclass` relation, which `multimethod` uses for type resolution. This can lead to unexpected dispatch behavior.
fix
It is recommended to use Abstract Base Classes (ABCs) from `collections.abc` (e.g., `list` instead of `typing.List`, or `collections.abc.Sequence` for more abstract types) or direct concrete types for more reliable dispatching.
affects: All versions
breakingStarting with version 2.0.0, `multimethod` requires Python 3.10 or newer. Older versions (e.g., 1.x) supported Python 3.6 or 3.7 and up.
fix
Upgrade your Python environment to 3.10 or higher, or pin your `multimethod` dependency to a version compatible with your Python interpreter (e.g., `multimethod<2.0`).
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: func() takes 1 positional argument but 2 were given
This error occurs when `multimethod` incorrectly dispatches to a single-argument function despite multiple-argument overloaded functions being defined, particularly when using `typing.Union` in argument annotations for different argument counts.
fix
Ensure that the `multimethod` library version is up-to-date, as this specific issue might have been addressed in newer releases. If the problem persists, consider defining distinct functions with different names for varying argument counts, or explicitly registering methods using `func.register` with precise type tuples instead of relying solely on decorator-based name lookup when combining `Union` types and variable argument counts.
ImportError: cannot import name 'overload' from 'multimethod'
This error typically occurs when a project relies on an older version of the `multimethod` library where `overload` was available, but a newer version (e.g., 2.0.0 or later) is installed, which has removed the `overload` utility.
fix
To resolve this, either downgrade `multimethod` to a compatible version (e.g., `pip install 'multimethod==1.9'`) or update your code to no longer use `overload` if its functionality has been removed or replaced in the newer `multimethod` versions.
multimethod.DispatchError: No matching method found for types (Type1, Type2, ...)
This `DispatchError` (a subclass of `TypeError`) is raised by the `multimethod` library when it cannot find a registered method whose signature matches the runtime types of the arguments provided during a function call, or when multiple equally specific methods are found (ambiguity).
fix
Verify that all expected type combinations have a corresponding method registered with `@multimethod` (or `multimethod.register`). Ensure type annotations accurately reflect the intended dispatch types, and if ambiguity is the issue, refine your type hints to create a clear dispatch hierarchy, or use the `strict` flag on the multimethod object if precise matching is required.
TypeError: method_name() missing 1 required positional argument: 'self'
This common Python error can occur with `multimethod` when decorating instance methods, particularly if `@classmethod` or `@staticmethod` decorators are applied in the incorrect order (i.e., before `@multimethod`), or if the `self` (or `cls`) argument's type annotation is omitted or incorrectly handled during method registration for class or instance methods.
fix
Ensure that `@classmethod` or `@staticmethod` are applied *after* the `@multimethod` decorator (i.e., wrapping the final multimethod definition). For instance methods, ensure the `self` parameter is present in the method signature and correctly annotated (or use `object` as a placeholder if not dispatching on `self`).
Upgrade
Version history
2.1latest on PyPI · released Jul 29, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
multimethod — pip install multimethod · libregistry