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 multimethodVerified import paths — ran on the pinned version, not inferred.
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.
Migrate from `overload` to `@multimethod` with `isa` checks if predicate dispatching is needed, or adjust code that relied on positional distance for ambiguity resolution.
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.
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.
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`).
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.
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.
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.
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`).
No dependency data recorded yet.