A ``singledispatch()`` for arguments that are classes annotated as specific types.
Project description
class_singledispatch
A singledispatch()
for arguments that are classes annotated as specific types.
Inspired by https://github.com/python/cpython/issues/100623.
A simple example
Use functools.singledispatch
to singledispatch classes as parameters.
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, as soon as `@class_singledispatch` allows specifying
# the class similarly to `.register` -- 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
If you want to…
…use this tool in your project 💻
You might simply install it with pip:
pip install class-singledispatch
If you use Poetry, then run:
poetry add class-singledispatch
…contribute to class_singledispatch 🚀
[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.
-
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 --hook-type pre-commit --hook-type pre-push
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file class_singledispatch-1.0.1.tar.gz
.
File metadata
- Download URL: class_singledispatch-1.0.1.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86863c8238cb22849aa0a82731c00ba4c7d6352bc01bb82bbffd413dc7ebbe29 |
|
MD5 | 38ce6d95c5d08bccfca30ebd27cd11d4 |
|
BLAKE2b-256 | 2357100aee792f3beb1f46678075c19e6704aa360a894e6fa37c8e684549c7a6 |
File details
Details for the file class_singledispatch-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: class_singledispatch-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9096ddfae994103842f7ef5e7204a9d36246c914366964186fe2462969a2d23b |
|
MD5 | 419ef8ed77b331ff15df5b34200eefd8 |
|
BLAKE2b-256 | afb88a606238a80117c7071561fb0c86efa593c6f653c16a32c9f0dca2ba399a |