Skip to main content

chatora.dispatch

Multiple argument dispatching for Python.

Usage

Transform a function into a dispatch generic function, such as the functools.singledispatch. Unlike the functools.singledispatch, it supports multi-dispatch.

from chatora.dispatch.api import dispatch


@dispatch
def func(arg0, arg1):
    return '1st func'


assert func(0, 0) == '1st func'
assert func('0', '0') == '1st func'


@func.register
def _(arg0, arg1: int):
    return '2nd func'


assert func(0, 0) == '2nd func'
assert func('0', 0) == '2nd func'
assert func('0', '0') == '1st func'


@func.register
def _(arg0: int, arg1: int):
    return '3rd func'


assert func(0, 0) == '3rd func'
assert func('0', 0) == '2nd func'
assert func('0', '0') == '1st func'

It partially supports arguments with typing.Union, typing.Optioanl and typing.Any. typing.Any is equivalent to empty annotation.

from chatora.dispatch.api import dispatch
import typing


@dispatch
def func(arg0: typing.Any, arg1: typing.Any):
    return '1st func'


@func.register
def _(arg0: typing.Optional[str], arg1: typing.Union[str, list, tuple]):
    return '2nd func'


assert func(0, 0) == '1st func'
assert func('0', 0) == '1st func'
assert func('0', '0') == '2nd func'
assert func('0', []) == '2nd func'
assert func(None, ()) == '2nd func'

It also partially supports return type annotation.

from chatora.dispatch.api import dispatch
import typing


class ResultTuple(tuple):
    def __new__(cls, *args):
        return super().__new__(cls, args)


class ResultClass:
    def __init__(self, a, b):
        self.a, self.b = a, b


@dispatch
def func(arg0: typing.Optional[str], arg1: tuple):
    return '1st func'


@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> typing.Sequence[str]:
    return ['2nd', 'func']


@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> typing.Tuple[str]:
    return ('3rd', 'func')


@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> ResultTuple:
    return ResultTuple('4th', 'func')


@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> ResultClass:
    return ResultClass('4th', 'func')


assert func('0', ()) == '1st func'
assert func('0', (), _return_type=typing.Sequence[str]) == ['2nd', 'func']
assert func('0', (), _return_type=typing.Tuple[str]) == ('3rd', 'func')
assert func('0', (), _return_type=ResultTuple) == ResultTuple('4th', 'func')
assert isinstance(func('0', (), _return_type=ResultClass), ResultClass)

Changelog

0.1 (2019-05-01)

  • Birth!

Metadata

Release files for chatora.dispatch 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chatora.dispatch 0.1
File Size Uploaded Provenance Actions
chatora.dispatch-0.1.tar.gz 8.0 kB Without provenance Details for chatora.dispatch-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chatora.dispatch 0.1
File Interpreter ABI Platform Actions
chatora.dispatch-0.1-py37-none-any.whl Python 3.7 none any Details for chatora.dispatch-0.1-py37-none-any.whl

Total release size: 16.3 kB

Release files / chatora.dispatch-0.1.tar.gz

Download URL chatora.dispatch-0.1.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
551cee894149404538642eeef544fb937b4843c596d6be97de6f0bc4796f1307
BLAKE2b-256 checksum
How to use checksums
df493e51a039ce9ca0b6b027782d0cc71cc7e24fe03aaa208e9c59530df99b13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Release files / chatora.dispatch-0.1-py37-none-any.whl

Download URL chatora.dispatch-0.1-py37-none-any.whl
Size 8.3 kB
Tags Python 3.7
SHA-256 checksum
How to use checksums
a2767494945542334975a3953de612131f43a9ae0e05807ea5046e9542c158b6
BLAKE2b-256 checksum
How to use checksums
d07481444c4ba2733ea111d5b2870ed90fb1ae356b998949ff535623ac3af588
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2

Security for chatora.dispatch 0.1

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.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page