Registry / serialization / singledispatch

singledispatch

JSON →
library4.1.2pypypi✓ verified 23d ago

The `singledispatch` library provides a standalone implementation of the `functools.singledispatch` decorator, which allows for generic functions to have different behaviors based on the type of their first argument. Originally a backport for older Python versions, the PyPI package currently targets Python 3.9+ and is actively maintained with regular releases, as evidenced by recent version updates.

pip install singledispatch
INSTALL
IMPORT
SIG · SINGLEDISPATCH
S
singledispatch
serializationpythonv4.1.2
Install
1.5s 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 v4.1.2 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

singledispatch
from singledispatch import singledispatch
from functools import singledispatch
While `functools.singledispatch` is built into Python 3.4+, this package provides a standalone implementation. If you install and use the `singledispatch` package, you should import from it directly to ensure you are using its version.
singledispatchmethod
from singledispatch import singledispatchmethod
from functools import singledispatchmethod
`singledispatchmethod` was added to `functools` in Python 3.8. If targeting Python versions prior to 3.8 (though the `singledispatch` package itself now requires Python 3.9+), or if using this package's specific implementation, import from `singledispatch` directly.

This example demonstrates how to create a single-dispatch generic function using the `@singledispatch` decorator. It defines a default implementation and then registers specific implementations for `int` and `list` types. When called, the appropriate function is dispatched based on the type of the first argument.

from singledispatch import singledispatch @singledispatch def process_data(arg): """Default implementation for process_data""" return f"Processing generic data: {arg}" @process_data.register(int) def _(arg): """Process integer data""" return f"Processing integer: {arg * 2}" @process_data.register(list) def _(arg): """Process list data""" return f"Processing list: {', '.join(map(str, arg))}" print(process_data("hello")) print(process_data(10)) print(process_data([1, 2, 3])) print(process_data(3.14))
Debug
Known issues
gotchaPotential confusion with `functools.singledispatch` which is part of Python's standard library since Python 3.4. If you are using Python 3.4 or newer, `functools.singledispatch` is generally preferred unless you specifically need the `singledispatch` package's independent implementation or its associated utilities.
fix
For new projects on Python 3.4+, consider using `from functools import singledispatch`. If you specifically need this package, ensure you import from `singledispatch`.
affects: All versions
breakingVersion 4.0.0 dropped support for Python 3.8, now requiring Python 3.9 or later. This is a significant breaking change for environments still using Python 3.8.
fix
Upgrade your Python environment to 3.9+ or pin `singledispatch` to a version older than 4.0.0 (e.g., `~=3.7`) if you need to support Python 3.8.
affects: 4.0.0 and later
gotchaThe `singledispatch` decorator, by its nature, dispatches solely based on the *type of the first argument*. It does not support multiple dispatch (dispatching on multiple arguments) or dispatching based on keyword arguments.
fix
If you require dispatching on multiple arguments or keyword arguments, you will need to implement a custom solution or use a third-party library designed for multiple dispatch.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'singledispatch'
The `singledispatch` package has not been installed in the current Python environment.
fix
pip install singledispatch
TypeError: 'module' object is not callable
The `singledispatch` module was imported directly, and then the module object was incorrectly used as a decorator.
fix
from singledispatch import singledispatch
AttributeError: 'function' object has no attribute 'register'
The `.register()` method was called on a regular Python function that had not been decorated with `@singledispatch`.
fix
from singledispatch import singledispatch

@singledispatch
def my_function(arg):
    # Default implementation
    pass

@my_function.register(int)
def _(arg: int):
    return arg + 1
TypeError: singledispatch expects a callable as its first argument
The `singledispatch` decorator or function was applied to, or passed, a non-callable object.
fix
from singledispatch import singledispatch

@singledispatch
def my_function(arg):
    # This is a callable function
    pass
Upgrade
Version history
4.1.2latest on PyPI · released May 14, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
8
OpenAI (training)
1
Resources
singledispatch — pip install singledispatch · libregistry