class-singledispatch


Nameclass-singledispatch JSON
Version 1.2.3.post2 PyPI version JSON
download
home_pagehttps://github.com/bswck/class_singledispatch
SummaryA ``singledispatch()`` for arguments that are classes annotated as specific types.
upload_time2024-04-21 23:09:24
maintainerNone
docs_urlNone
authorbswck
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # <div align="center">class_singledispatch<br>[![skeleton](https://img.shields.io/badge/0.0.2rc–224–ge16504a-skeleton?label=%F0%9F%92%80%20skeleton-ci/skeleton-python&labelColor=black&color=grey&link=https%3A//github.com/skeleton-ci/skeleton-python)](https://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-224-ge16504a) [![Supported Python versions](https://img.shields.io/pypi/pyversions/class-singledispatch.svg?logo=python&label=Python)](https://pypi.org/project/class-singledispatch/) [![Package version](https://img.shields.io/pypi/v/class-singledispatch?label=PyPI)](https://pypi.org/project/class-singledispatch/)</div>

[![Tests](https://github.com/bswck/class_singledispatch/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/class_singledispatch/actions/workflows/test.yml)
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/class_singledispatch.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/class_singledispatch)
[![Documentation Status](https://readthedocs.org/projects/class-singledispatch/badge/?version=latest)](https://class-singledispatch.readthedocs.io/en/latest/?badge=latest)

A [``singledispatch()``](https://docs.python.org/3/library/functools.html#functools.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`.

```python
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:

```shell
pip install class-singledispatch
```
> [!Note]
> `class-singledispatch` supports modern type hinting.<br/>
> To use [PEP 585](https://peps.python.org/pep-0585/)
> or [PEP 604](https://peps.python.org/pep-0604/) annotations on Python <3.10, install
> `class-singledispatch[modern-type-hints]`.

If you use [Poetry](https://python-poetry.org/), then you might want to run:

```shell
poetry add class-singledispatch
```

## For Contributors
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
<!--
This section was generated from skeleton-ci/skeleton-python@0.0.2rc-224-ge16504a.
Instead of changing this particular file, you might want to alter the template:
https://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-224-ge16504a/project/README.md.jinja
-->
> [!Note]
> If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).
1.  Fork the [class_singledispatch repository](https://github.com/bswck/class_singledispatch) on GitHub.

1.  [Install Poetry](https://python-poetry.org/docs/#installation).<br/>
    Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them.
    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):

    ```shell
    pipx install poetry
    ```

    <sub>If you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.</sub>

    Be sure to have Python 3.8 installed—if you use [pyenv](https://github.com/pyenv/pyenv#readme), simply run:

    ```shell
    pyenv install 3.8
    ```

1.  Clone your fork locally and install dependencies.

    ```shell
    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:

    ```shell
    poetry shell
    pre-commit install
    ```

For more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/class_singledispatch/blob/HEAD/CONTRIBUTING.md).<br/>
Always happy to accept contributions! ❤️

# Legal Info
© Copyright by Bartosz Sławecki ([@bswck](https://github.com/bswck)).
<br />This software is licensed under the terms of [MIT License](https://github.com/bswck/class_singledispatch/blob/HEAD/LICENSE).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bswck/class_singledispatch",
    "name": "class-singledispatch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "bswck",
    "author_email": "bartoszpiotrslawecki@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/58/56/53eb6c5d15aab38e63da18ad55c3107779d1bcbef311e0e537c78f1e0010/class_singledispatch-1.2.3.post2.tar.gz",
    "platform": null,
    "description": "# <div align=\"center\">class_singledispatch<br>[![skeleton](https://img.shields.io/badge/0.0.2rc\u2013224\u2013ge16504a-skeleton?label=%F0%9F%92%80%20skeleton-ci/skeleton-python&labelColor=black&color=grey&link=https%3A//github.com/skeleton-ci/skeleton-python)](https://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-224-ge16504a) [![Supported Python versions](https://img.shields.io/pypi/pyversions/class-singledispatch.svg?logo=python&label=Python)](https://pypi.org/project/class-singledispatch/) [![Package version](https://img.shields.io/pypi/v/class-singledispatch?label=PyPI)](https://pypi.org/project/class-singledispatch/)</div>\n\n[![Tests](https://github.com/bswck/class_singledispatch/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/class_singledispatch/actions/workflows/test.yml)\n[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/class_singledispatch.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/class_singledispatch)\n[![Documentation Status](https://readthedocs.org/projects/class-singledispatch/badge/?version=latest)](https://class-singledispatch.readthedocs.io/en/latest/?badge=latest)\n\nA [``singledispatch()``](https://docs.python.org/3/library/functools.html#functools.singledispatch) for arguments that are classes annotated as specific types.\n\nInspired by https://github.com/python/cpython/issues/100623.\n\n# A Simple Example\nWhile `functools.singledispatch` examines the class of the first user argument,\n`class_singledispatch` uses the first argument as the class itself and performs\nthe same task with it as `functools.singledispatch`.\n\n```python\nfrom class_singledispatch import class_singledispatch\n\n\nclass T:\n    pass\n\n\nclass OtherT:\n    pass\n\n\n@class_singledispatch\ndef on_class(cls: type[T], /) -> None:\n    print(\"T!\")\n\n\n@on_class.register\ndef on_other_class(cls: type[OtherT], /) -> None:\n    print(\"OtherT!\")\n\n\n# Useful for <3.10 without eval_type_backport -- pass the class to the decorator\n# not to use the annotation from the function for the targeted class resolution\n@on_class.register(SomeOtherT)\ndef on_some_other_class(cls: type[SomeOtherT], /) -> None:\n    print(\"SomeOtherT!\")\n\n\non_class(T)  # T!\non_class(OtherT)  #  OtherT!\non_class(SomeOtherT)  #  SomeOtherT!\n```\n\n# Installation\nYou might simply install it with pip:\n\n```shell\npip install class-singledispatch\n```\n> [!Note]\n> `class-singledispatch` supports modern type hinting.<br/>\n> To use [PEP 585](https://peps.python.org/pep-0585/)\n> or [PEP 604](https://peps.python.org/pep-0604/) annotations on Python <3.10, install\n> `class-singledispatch[modern-type-hints]`.\n\nIf you use [Poetry](https://python-poetry.org/), then you might want to run:\n\n```shell\npoetry add class-singledispatch\n```\n\n## For Contributors\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)\n<!--\nThis section was generated from skeleton-ci/skeleton-python@0.0.2rc-224-ge16504a.\nInstead of changing this particular file, you might want to alter the template:\nhttps://github.com/skeleton-ci/skeleton-python/tree/0.0.2rc-224-ge16504a/project/README.md.jinja\n-->\n> [!Note]\n> If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).\n1.  Fork the [class_singledispatch repository](https://github.com/bswck/class_singledispatch) on GitHub.\n\n1.  [Install Poetry](https://python-poetry.org/docs/#installation).<br/>\n    Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them.\n    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):\n\n    ```shell\n    pipx install poetry\n    ```\n\n    <sub>If you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.</sub>\n\n    Be sure to have Python 3.8 installed\u2014if you use [pyenv](https://github.com/pyenv/pyenv#readme), simply run:\n\n    ```shell\n    pyenv install 3.8\n    ```\n\n1.  Clone your fork locally and install dependencies.\n\n    ```shell\n    git clone https://github.com/your-username/class_singledispatch path/to/class_singledispatch\n    cd path/to/class_singledispatch\n    poetry env use $(cat .python-version)\n    poetry install\n    ```\n\n    Next up, simply activate the virtual environment and install pre-commit hooks:\n\n    ```shell\n    poetry shell\n    pre-commit install\n    ```\n\nFor more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/class_singledispatch/blob/HEAD/CONTRIBUTING.md).<br/>\nAlways happy to accept contributions! \u2764\ufe0f\n\n# Legal Info\n\u00a9 Copyright by Bartosz S\u0142awecki ([@bswck](https://github.com/bswck)).\n<br />This software is licensed under the terms of [MIT License](https://github.com/bswck/class_singledispatch/blob/HEAD/LICENSE).\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A ``singledispatch()`` for arguments that are classes annotated as specific types.",
    "version": "1.2.3.post2",
    "project_urls": {
        "Coverage": "https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/class_singledispatch",
        "Distribution": "https://pypi.org/project/class-singledispatch/",
        "Documentation": "https://class-singledispatch.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/bswck/class_singledispatch",
        "Issues": "https://github.com/bswck/class_singledispatch/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dce9e59d3ec711acd41ab79691dd70453ede21dc29cf4c02d3bc50eafe2accd3",
                "md5": "d915916bb9268696e4d286b1243e483e",
                "sha256": "a806473af00ad02cb34120b953074f3390e3a119dee872e1c251ed271c2b28ca"
            },
            "downloads": -1,
            "filename": "class_singledispatch-1.2.3.post2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d915916bb9268696e4d286b1243e483e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6227,
            "upload_time": "2024-04-21T23:09:22",
            "upload_time_iso_8601": "2024-04-21T23:09:22.428668Z",
            "url": "https://files.pythonhosted.org/packages/dc/e9/e59d3ec711acd41ab79691dd70453ede21dc29cf4c02d3bc50eafe2accd3/class_singledispatch-1.2.3.post2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "585653eb6c5d15aab38e63da18ad55c3107779d1bcbef311e0e537c78f1e0010",
                "md5": "2bf71f5e7f1934eba368c0269a2cac5a",
                "sha256": "19a80f7c2a530e8f1909172a7b8544c552332f0194136e4bebdd725ffa104dc5"
            },
            "downloads": -1,
            "filename": "class_singledispatch-1.2.3.post2.tar.gz",
            "has_sig": false,
            "md5_digest": "2bf71f5e7f1934eba368c0269a2cac5a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6622,
            "upload_time": "2024-04-21T23:09:24",
            "upload_time_iso_8601": "2024-04-21T23:09:24.055803Z",
            "url": "https://files.pythonhosted.org/packages/58/56/53eb6c5d15aab38e63da18ad55c3107779d1bcbef311e0e537c78f1e0010/class_singledispatch-1.2.3.post2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-21 23:09:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bswck",
    "github_project": "class_singledispatch",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "class-singledispatch"
}
        
Elapsed time: 0.80898s