A singledispatch() for arguments that are classes annotated as specific types.
Inspired by https://github.com/python/cpython/issues/100623.
A Simple Example
While functools.singledispatch examines the class of the first user argument,
class_singledispatch uses the first argument as the class itself and performs
the same task with it as functools.singledispatch.
from class_singledispatch import class_singledispatch
class T:
pass
class OtherT:
pass
@class_singledispatch
def on_class(cls: type[T], /) -> None:
print("T!")
@on_class.register
def on_other_class(cls: type[OtherT], /) -> None:
print("OtherT!")
# Useful for <3.10 without eval_type_backport -- pass the class to the decorator
# not to use the annotation from the function for the targeted class resolution
@on_class.register(SomeOtherT)
def on_some_other_class(cls: type[SomeOtherT], /) -> None:
print("SomeOtherT!")
on_class(T) # T!
on_class(OtherT) # OtherT!
on_class(SomeOtherT) # SomeOtherT!
Installation
You might simply install it with pip:
pip install class-singledispatch
If you use Poetry, then you might want to run:
poetry add class-singledispatch
For Contributors
-
Fork the class_singledispatch repository on GitHub.
-
Install Poetry.
Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them. You might use pipx to install it globally (recommended):pipx install poetry
If you encounter any problems, refer to the official documentation for the most up-to-date installation instructions.
Be sure to have Python 3.8 installed—if you use pyenv, simply run:
pyenv install 3.8
-
Clone your fork locally and install dependencies.
git clone https://github.com/your-username/class_singledispatch path/to/class_singledispatch cd path/to/class_singledispatch poetry env use $(cat .python-version) poetry install
Next up, simply activate the virtual environment and install pre-commit hooks:
poetry shell pre-commit install
For more information on how to contribute, check out CONTRIBUTING.md.
Always happy to accept contributions! ❤️
Legal Info
© Copyright by Bartosz Sławecki (@bswck).
This software is licensed under the terms of MIT License.
Metadata
Release files for class-singledispatch 1.2.3.post2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| class_singledispatch-1.2.3.post2.tar.gz | 6.6 kB | Without provenance | Details for class_singledispatch-1.2.3.post2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| class_singledispatch-1.2.3.post2-py3-none-any.whl | Python 3 | none | any | Details for class_singledispatch-1.2.3.post2-py3-none-any.whl |
Total release size: 12.8 kB
Release files / class_singledispatch-1.2.3.post2.tar.gz
| Download URL | class_singledispatch-1.2.3.post2.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19a80f7c2a530e8f1909172a7b8544c552332f0194136e4bebdd725ffa104dc5
|
|
BLAKE2b-256 checksum How to use checksums |
585653eb6c5d15aab38e63da18ad55c3107779d1bcbef311e0e537c78f1e0010
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Release files / class_singledispatch-1.2.3.post2-py3-none-any.whl
| Download URL | class_singledispatch-1.2.3.post2-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a806473af00ad02cb34120b953074f3390e3a119dee872e1c251ed271c2b28ca
|
|
BLAKE2b-256 checksum How to use checksums |
dce9e59d3ec711acd41ab79691dd70453ede21dc29cf4c02d3bc50eafe2accd3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.0.0 CPython/3.12.3
|
Security for class-singledispatch 1.2.3.post2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.