Skip to main content

A ``singledispatch()`` for arguments that are classes annotated as specific types.

Project description

class_singledispatch
skeleton Supported Python versions Package version

Tests Coverage Documentation Status

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 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

Poetry Ruff Pre-commit

[!Note] If you use Windows, it is highly recommended to complete the installation in the way presented below through WSL2.

  1. Fork the class_singledispatch repository on GitHub.

  2. 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
    
  3. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

class_singledispatch-1.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

class_singledispatch-1.1.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file class_singledispatch-1.1.1.tar.gz.

File metadata

  • Download URL: class_singledispatch-1.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for class_singledispatch-1.1.1.tar.gz
Algorithm Hash digest
SHA256 af0a1385bc78e50d8401e69825f018333c5f61f09d39ee5face89d8995b734fa
MD5 d6f981fac3bef0a71968ac660e86d2e7
BLAKE2b-256 e35c8e08f47fd96b7ecf9353adb4e1f281e28dbe626a7f2b18c32d8ca7870915

See more details on using hashes here.

File details

Details for the file class_singledispatch-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for class_singledispatch-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93c20f742e0b5407c968c7c058be058178d4aa215b41f323f5b782de059cf110
MD5 78e56bf8b30f33ba5cf496982bb3ce3e
BLAKE2b-256 1f8206b6121a3c48fce3f7268fc2abc3b647f03a8c14ed2bf4f61661638e377e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page