Install & Compatibility
Where this runs
tested against v1.0.0 · 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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.028s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.028s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
dispatch
✓ from multipledispatch import dispatch
Defines a function `add` that behaves differently based on the types of its arguments, demonstrating basic multiple dispatch.
from multipledispatch import dispatch
@dispatch(int, int)
def add(x, y):
return x + y
@dispatch(object, object)
def add(x, y):
return f"{x} + {y}"
print(add(1, 2)) # Expected: 3
print(add(1, 'hello')) # Expected: '1 + hello'
Debug
Known issues
gotchaThe library may not always select the *most specific* implementation when multiple inheritance is involved or when method definitions create unexpected ambiguities. Some community reports suggest it might resolve based on definition order rather than strict specificity, which deviates from expected multiple dispatch behavior.fixThoroughly test dispatch resolution with complex type hierarchies and overlapping signatures. Consider alternatives like `plum-dispatch` if strict specificity with inheritance is critical. Explicitly define more specific signatures to avoid relying on implicit resolution order.
affects: All versions up to 1.0.0
gotchaAmbiguityWarning: If multiple equally specific implementations exist for a given call signature, `multipledispatch` raises an `AmbiguityWarning` at function definition time. If not resolved by adding a more specific signature, one of the competing functions will be selected "pseudo-randomly" at runtime.fixAlways address `AmbiguityWarning` by defining a more specific method for the ambiguous signature. For example, if `(object, float)` and `(float, object)` are ambiguous, define `@dispatch(float, float)`.
affects: All versions up to 1.0.0
gotchaPerformance with many signatures: Adding a new signature requires a full re-resolution of the entire function's dispatch table. This process can become slow and troublesome if a single dispatched function accumulates hundreds of type signatures.fixDesign dispatched functions with a manageable number of distinct type signatures. Consider refactoring complex dispatch logic into smaller, more focused functions or using alternative architectural patterns if the number of signatures becomes excessively large.
affects: All versions up to 1.0.0
gotchaGlobal namespace conflicts: By default, `dispatch` uses a global namespace to register functions. In large applications or when integrating multiple libraries that both use `multipledispatch` without explicit namespaces, function name collisions can lead to difficult-to-track-down bugs or unexpected dispatch behavior.fixTo avoid conflicts, explicitly define and pass a custom dictionary as a namespace for your dispatchers, especially in library code. Use `from functools import partial; dispatch = partial(dispatch, namespace=my_namespace)` to bind a local namespace to the decorator.
affects: All versions up to 1.0.0
gotcha`multipledispatch`'s support for object-oriented programming paradigms, especially with class inheritance and complex Method Resolution Order (MRO), is limited. It may not behave as intuitively as other multiple dispatch implementations when dealing with subclasses and inherited methods.fixWhen working with class inheritance, carefully evaluate the dispatch behavior. If advanced inheritance-aware dispatch is needed, consider more feature-rich alternatives like `plum-dispatch`.
affects: All versions up to 1.0.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'multipledispatch'
The `multipledispatch` package is not installed in the Python environment being used, or the environment (e.g., virtual environment in an IDE like Jupyter or PyCharm) is not correctly activated or configured.
fixInstall the package using pip: `pip install multipledispatch`. If using a virtual environment or an IDE, ensure the correct interpreter or kernel where `multipledispatch` is installed is active.
TypeError: No matching function for types (...)
When a dispatched function is called with arguments of certain types, `multipledispatch` cannot find any registered implementation that matches the combination of those argument types, including considering inheritance.
fixDefine a new function with the `@dispatch` decorator for the specific types causing the error, or for a more general parent type that covers the missing combination. Alternatively, ensure the arguments passed to the function match one of the already registered dispatch signatures.
AmbiguityWarning: Ambiguities exist in dispatched function f The following signatures may result in ambiguous behavior: [object, float], [float, object] Consider making the following additions: @dispatch(float, float) def f(...)
`multipledispatch` has detected that two or more registered function signatures are equally specific for a given set of input types, making it ambiguous which implementation should be called.
fixAdd a more specific dispatch function to resolve the ambiguity. For example, if `(object, float)` and `(float, object)` are ambiguous for a call with `(float, float)`, explicitly define `@dispatch(float, float)` to provide a clear resolution.
TypeError: some_func requires at least 1 positional argument
This error can occur when defining a dispatched function without any positional arguments, or with only keyword-only arguments, which can conflict with `multipledispatch`'s internal handling of argument signatures.
fixEnsure that the dispatched function has at least one positional argument defined in its signature. If the intent is to use keyword arguments, they should be handled carefully within a function that still accepts positional arguments, or consider if `multipledispatch` is the best tool for functions primarily driven by keyword arguments.
error: Name "function_name" already defined
This is typically a static analysis error from tools like MyPy, which interprets multiple `@dispatch` decorated functions with the same name as redefinitions, not as overloaded functions.
fixWhile `multipledispatch` handles these at runtime, static type checkers like MyPy often need configuration to understand this pattern. As a workaround, some users might rename functions (e.g., `_function_name_int_str`) or disable specific MyPy checks, but a more robust solution often involves using `typing.overload` in conjunction with `multipledispatch` (though this might not be directly supported by `multipledispatch` itself, but rather a pattern to satisfy type checkers) or awaiting MyPy improvements.
Upgrade
Version history
1.0.0latest on PyPI · released Jun 27, 2023
Audit
Dependencies
No dependency data recorded yet.