{"id":2125,"library":"multimethod","title":"Multimethod","description":"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.","status":"active","version":"2.0.2","language":"python","source_language":"en","source_url":"https://github.com/coady/multimethod","tags":["multiple dispatch","polymorphism","type hints","decorator","function overloading"],"install":[{"cmd":"pip install multimethod","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary decorator for defining multimethods.","symbol":"multimethod","correct":"from multimethod import multimethod"},{"note":"Metaclass for automatically converting class methods to multimethods.","symbol":"multimeta","correct":"from multimethod import multimeta"},{"note":"Provides compatibility with the functools.singledispatch style, supporting keyword arguments for dispatching in this specific variant.","symbol":"multidispatch","correct":"from multimethod import multidispatch"}],"quickstart":{"code":"from multimethod import multimethod\n\n@multimethod\ndef func(x: int, y: float):\n    return f\"Int and Float: {x} + {y}\"\n\n@multimethod\ndef func(x: float, y: int):\n    return f\"Float and Int: {x} + {y}\"\n\n@multimethod\ndef func(x: str, y: str):\n    return f\"Strings: {x} {y}\"\n\nprint(func(1, 2.0))\nprint(func(3.0, 4))\nprint(func(\"Hello\", \"World\"))\n","lang":"python","description":"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."},"warnings":[{"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.","message":"Version 2.0 removed the `overload` decorator and the mechanism for resolving ambiguity using positional distance. Code relying on these features will break.","severity":"breaking","affected_versions":">=2.0"},{"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.","message":"Keyword-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`.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Typing 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.","severity":"gotcha","affected_versions":"All versions"},{"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`).","message":"Starting 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.","severity":"breaking","affected_versions":">=2.0.0"}],"env_vars":null,"search_vec":"'2.0.2':51 'activ':55 'ad':7 'advanc':43 'aim':16 'annot':23 'argument':9,34 'base':32 'cadenc':59 'creat':25 'current':48 'decor':5,70 'develop':56 'dispatch':10,44,66 'function':12,31,71 'hint':41,69 'librari':37 'multimethod':1,2,27 'multipl':8,65 'object':28 'overload':72 'per':63 'polymorph':67 'provid':3 'python':14 'regist':30 'releas':58,62 'rule':45 'sever':61 'simplic':18 'speed':20 'support':38 'type':22,35,40,68 'util':21 'various':39 'version':50 'year':64","created_at":"2026-04-09T18:44:35.756861+00:00","updated_at":"2026-04-16T16:42:46.827974+00:00","problems":[{"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.","cause":"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.","error":"TypeError: func() takes 1 positional argument but 2 were given"},{"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.","cause":"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.","error":"ImportError: cannot import name 'overload' from 'multimethod'"},{"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.","cause":"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).","error":"multimethod.DispatchError: No matching method found for types (Type1, Type2, ...)"},{"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`).","cause":"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.","error":"TypeError: method_name() missing 1 required positional argument: 'self'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/coady/multimethod","docs":"https://coady.github.io/multimethod","changelog":"https://github.com/coady/multimethod/blob/main/CHANGELOG.md","pypi":"https://pypi.org/project/multimethod/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-28","install_tag":null}}