{"id":3742,"library":"plum-dispatch","title":"Multiple dispatch in Python","description":"Plum-dispatch is a Python library that provides a powerful and Pythonic implementation of multiple dispatch, allowing functions to behave differently based on the types of multiple arguments. Its design philosophy is inspired by Julia's approach to multiple dispatch, and version 2.x is powered by the `beartype` library for enhanced performance. The library is actively maintained, with its current version being 2.8.0, and has seen consistent updates.","status":"active","version":"2.8.0","language":"python","source_language":"en","source_url":"https://github.com/beartype/plum","tags":["multiple-dispatch","type-annotations","polymorphism","decorator"],"install":[{"cmd":"pip install plum-dispatch","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Plum 2.x is powered by Beartype for instance-check-based dispatch and performance.","package":"beartype","optional":false},{"reason":"Used for rich text and terminal output.","package":"rich","optional":false},{"reason":"Provides backports of features for the `typing` module.","package":"typing-extensions","optional":false}],"imports":[{"note":"As of Plum 2.6.1, all imports should go through the top-level `plum` package directly.","wrong":"from plum.dispatch import dispatch","symbol":"dispatch","correct":"from plum import dispatch"}],"quickstart":{"code":"from numbers import Number\nfrom plum import dispatch\n\n@dispatch\ndef process(x: str):\n    return f\"Processing string: {x}\"\n\n@dispatch\ndef process(x: int):\n    return f\"Processing integer: {x}\"\n\n@dispatch\ndef process(x: Number):\n    return f\"Processing a generic number: {x}\"\n\n\nassert process(\"hello\") == \"Processing string: hello\"\nassert process(123) == \"Processing integer: 123\"\nassert process(1.0) == \"Processing a generic number: 1.0\"\n\ntry:\n    process(x=123) # This will fail due to keyword argument dispatch rule\nexcept Exception as e:\n    print(f\"Caught expected error: {type(e).__name__}: {e}\")","lang":"python","description":"Demonstrates defining multiple versions of a function using the `@dispatch` decorator, where the appropriate implementation is chosen based on the runtime type of the arguments. It also highlights the restriction on using positional arguments for dispatch."},"warnings":[{"fix":"Always pass arguments that determine dispatch as positional arguments. Avoid passing them as keyword arguments where dispatch is expected.","message":"Plum (version 2.x) relies on positional arguments for dispatch. Keyword arguments are explicitly NOT used in the decision-making for which method to call. Positional arguments without a default value must always be given positionally.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Update all import statements to use `from plum import <symbol>` instead of `from plum.submodule import <symbol>`.","message":"Import paths have changed. All imports for Plum's public API should now go directly through the `plum` package (e.g., `from plum import dispatch`). Sub-module imports are no longer recommended or supported.","severity":"breaking","affected_versions":"2.6.1+"},{"fix":"Ensure your project runs on Python 3.10 or higher to use Plum 2.x.","message":"Plum 2.x dropped support for Python 3.9.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Use parametric types judiciously and only where absolutely necessary. Profile your code if performance becomes a concern.","message":"Using parametric types (e.g., `List[int]`, `Tuple[str, int]`) for dispatch can incur a significant performance hit due to the need to check every element's type.","severity":"gotcha","affected_versions":"All"},{"fix":"If migrating from `multipledispatch`, be aware of potential behavior differences and leverage Plum's advanced features.","message":"Plum is often recommended over `multipledispatch` due to being more featureful, having better support for class inheritance, and correctly handling method precedence (choosing the most specific method).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"search_vec":"'2':48 '2.8.0':69 'activ':62 'allow':22 'annot':80 'approach':42 'argument':33 'base':27 'beartyp':54 'behav':25 'consist':73 'current':66 'decor':82 'design':35 'differ':26 'dispatch':2,7,21,45,77 'enhanc':57 'function':23 'implement':18 'inspir':38 'julia':40 'librari':11,55,60 'maintain':63 'multipl':1,20,32,44,76 'multiple-dispatch':75 'perform':58 'philosophi':36 'plum':6 'plum-dispatch':5 'polymorph':81 'power':15,51 'provid':13 'python':4,10,17 'seen':72 'type':30,79 'type-annot':78 'updat':74 'version':47,67 'x':49","created_at":"2026-04-11T17:42:46.812590+00:00","updated_at":"2026-04-16T18:04:34.792398+00:00","problems":[{"fix":"Ensure that a `@dispatch` method is defined for the argument types being passed (or a suitable superclass). For arguments that `plum-dispatch` needs to dispatch on, ensure they are passed positionally, not as keyword arguments.","cause":"This error occurs when `plum-dispatch` cannot find a suitable method for the types of the arguments provided, either because no dispatch has been defined for those specific types or a more general fallback, or because arguments intended for dispatch were passed as keyword arguments.","error":"NotFoundLookupError: For function \"f\", signature Signature(builtins.float) could not be resolved."},{"fix":"Use string forward references for type hints (e.g., `other: \"Real\"`) to defer the evaluation of the type hint until the class is fully defined.","cause":"This is a standard Python issue where a type hint refers to a class that is not yet fully defined (e.g., a class using itself in a type hint within its own body).","error":"NameError: name 'Real' is not defined"},{"fix":"Define a type promotion rule for the involved types using `plum.add_promotion_rule(type_from_a, type_from_b, type_to_promote_to)`.","cause":"This error occurs when `plum-dispatch` attempts to perform an operation (like addition) between two different numeric types (e.g., `int` and `float`) for which a specific type promotion rule has not been explicitly defined.","error":"TypeError: No promotion rule for \"builtins.int\" and \"builtins.float\"."},{"fix":"Review the class method definition and how `plum-dispatch` is applied. Ensure arguments are correctly passed according to the dispatched method's signature and that any custom `__call__` implementations align with `plum-dispatch`'s argument handling.","cause":"This error can arise when using `plum-dispatch` with class methods, especially in scenarios involving decorators or how arguments are handled within the class's `__call__` method, potentially exacerbated by the integration with `beartype` in Plum 2.x.","error":"TypeError: Test.__call__() got an unexpected keyword argument 'a'."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.9.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/beartype/plum","docs":"https://beartype.github.io/plum","changelog":null,"pypi":"https://pypi.org/project/plum-dispatch/","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-29","next_check":"2026-07-28","install_tag":null}}