Registry / http-networking / methoddispatch

methoddispatch

JSON →
library5.0.1pypypi✓ verified 83d ago

methoddispatch provides a decorator similar to `functools.singledispatch` but specifically designed for dispatching class methods based on the type of their arguments. It extends Python's built-in single dispatch functionality to work seamlessly within classes, supporting different implementations for various argument types. The current version is 5.0.1, with an irregular release cadence driven by feature enhancements and Python version compatibility.

pip install methoddispatch
INSTALL
IMPORT
SIG · METHODDISPATCH
M
methoddispatch
http-networkingpythonv5.0.1
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.0.1 · 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
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

SingleDispatch
from methoddispatch import SingleDispatch
from methoddispatch import methoddispatch
register
from methoddispatch import register
from methoddispatch import methoddispatch
singledispatch
from methoddispatch import singledispatch
from methoddispatch import methoddispatch

This quickstart demonstrates how to define a class method with the `@methoddispatch` decorator and register different implementations for specific argument types. The generic `process` method serves as the fallback, while `int`, `str`, and `list` types have specialized implementations.

from methoddispatch import methoddispatch class MyClass: @methoddispatch def process(self, arg): return f"Processing generic {type(arg).__name__}: {arg}" @process.register(int) def _(self, arg: int): return f"Processing int: {arg * 2}" @process.register(str) def _(self, arg: str): return f"Processing str: {arg.upper()}" @process.register(list) # Will match list[int] as per v5.0.0 behavior def _(self, arg: list): return f"Processing list of length: {len(arg)}" # Example usage instance = MyClass() print(instance.process(10)) print(instance.process("hello")) print(instance.process([1, 2, 3])) print(instance.process(3.14)) print(instance.process([4, 5])) # Demonstrates list[int] matching list handler
Debug
Known issues
breakingVersion 3.0 dropped support for decorating plain functions. `methoddispatch` must now be applied to methods within a class.
fix
Refactor any plain functions using `methoddispatch` into class methods, or switch to `functools.singledispatch` for non-method functions.
affects: <3.0 to 3.0+
breakingStarting from version 3.0, the module-level `register` function was deprecated in favor of accessing the `register` method directly from the decorated method (e.g., `MyClass.method.register()`).
fix
Update your registration calls from `register(MyClass.method, type)` to `MyClass.method.register(type)`.
affects: <3.0 to 3.0+
gotchaAs of v5.0.0, while simple type annotations like `list[int]` are supported, dispatching only occurs on the base type (e.g., `list`). This means `list[int]` will match a handler registered for `list`, not `list[int]` specifically.
fix
Be aware that generic type hints are treated as their base type for dispatch. If you need to differentiate between, say, `list[int]` and `list[str]`, you'll need to implement additional runtime checks within your `list` handler.
affects: 5.0.0+
Upgrade
Version history
5.0.1latest on PyPI · released Nov 21, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
methoddispatch — pip install methoddispatch · libregistry