{"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":"en","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,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}