Registry / serialization / simplegeneric

simplegeneric

JSON →
library0.8.1pypypi✓ verified 22d ago

The `simplegeneric` module enables the creation of simple single-dispatch generic functions in Python, akin to built-in functions like `len()` or `iter()`. It achieves this through internal lookup tables rather than special method names. The library, currently at version 0.8.1, was last updated in 2012, signifying a mature but no longer actively developed project that remains functional for its intended purpose. It boasts no external runtime dependencies beyond a minimum Python version.

pip install simplegeneric
INSTALL
IMPORT
SIG · SIMPLEGENERIC
S
simplegeneric
serializationpythonv0.8.1
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.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.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

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

generic
from simplegeneric import generic

Define a generic function using `@generic` and specialize implementations based on the type or identity of the first argument using `@func.when_type` or `@func.when_object`.

from simplegeneric import generic @generic def move(item, target): """Default implementation""" return f"Default move: {item} to {target}" @move.when_type(int) def move_int(item, target): return f"Moving integer {item} to AD {target}" @move.when_type(str) def move_str(item, target): return f"Moving string '{item}' to {target}. All your {target} are belong to us." class Zig: pass zig_instance = Zig() @move.when_object(zig_instance) def move_zig(item, target): return f"Special move for Zig object: {item} to {target}. For great justice!" assert move(2101, "war") == "Moving integer 2101 to AD war" assert move("gentlemen", "base") == "Moving string 'gentlemen' to base. All your base are belong to us." assert move(zig_instance, "doing") == "Special move for Zig object: <object of type Zig> to doing. For great justice!" assert move(27.0, 56.2) == "Default move: 27.0 to 56.2"
Debug
Known issues
breakingPrior to version 0.8.1, the `setup.py` script was not fully compatible with Python 3, potentially causing issues during installation or packaging on Python 3 environments. The core module was Python 3 compatible from version 0.8.
fix
Ensure you are using `simplegeneric` version 0.8.1 or later when installing with Python 3. For versions <0.8.1, manual installation or Python 2.x might be required for `setup.py` functionality.
affects: <0.8.1
gotchaDispatching always occurs on the first argument, and this argument must be passed positionally when calling the generic function. Keyword arguments for the first parameter will not trigger dispatch.
fix
Always pass the dispatching argument as a positional argument.
affects: All
gotchaStandard documentation tools (like `help()`) will only display the signature of the default function. The specialized method signatures are not visible, requiring their documentation to be explicitly stated in the default function's docstring.
fix
Document the expected arguments and their types for all specialized methods within the docstring of the default `@generic` function.
affects: All
gotchaIf a generic function has optional arguments, these arguments (including their defaults) must be explicitly duplicated on every specialized method. Omitting them will result in a `TypeError` if they are not provided in a call.
fix
Ensure all optional arguments are included with their desired defaults in the signature of every `@func.when_type` or `@func.when_object` decorated function.
affects: All
gotchaFor new projects on Python 3.4+, consider using the built-in `functools.singledispatch` decorator. It provides similar single-dispatch functionality and is actively maintained as part of Python's standard library, offering better integration and type hinting support.
fix
Evaluate `functools.singledispatch` for modern Python development. `simplegeneric` is a mature but unmaintained library that predates this standard library feature.
affects: All (especially Python 3.4+)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'simplegeneric'
The `simplegeneric` package is not installed in the current Python environment.
fix
Install the package using pip: `pip install simplegeneric`
AttributeError: 'function' object has no attribute 'register'
The function intended to be a generic dispatch function was not correctly decorated with `@simplegeneric.generic`, so it lacks the `.register` method.
fix
Apply the `@simplegeneric.generic` decorator directly above the function definition: 
```python
from simplegeneric import generic

@generic
def my_generic_function(arg):
    pass

@my_generic_function.register(int)
def _(arg):
    return f'Integer: {arg}'
```
ModuleNotFoundError: No module named 'simplegeneric.generic'
The `generic` function is a direct export from the `simplegeneric` package's `__init__.py`, not located within a submodule named `simplegeneric.generic`.
fix
Import `generic` directly from the `simplegeneric` package: `from simplegeneric import generic`
TypeError: isinstance() arg 2 must be a type or tuple of types
The `.register()` method was called with an argument that is not a Python type (e.g., `int`, `str`) or a tuple of types.
fix
Provide a valid Python type (e.g., `int`, `str`, `list`) or a tuple of types to the `.register()` method: 
```python
from simplegeneric import generic

@generic
def process(item):
    return f'Default processing: {item}'

@process.register(str) # Correct: str is a type
def _(item):
    return f'Processing string: {item.upper()}'

@process.register((int, float)) # Correct: tuple of types
def _(item):
    return f'Processing number: {item * 2}'
```
Upgrade
Version history
0.8.1latest on PyPI · released Apr 1, 2012
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
1
Resources
simplegeneric — pip install simplegeneric · libregistry