Skip to main content

a singledispatch alternative for PEP-585 type hinted functions

Project description

Container Dispatch

why?

Because this code doesnt work natively:

@dispatch.register
def _(arg: list[int|str]):
    return "this is a list of ints and strings!"

the stdlib functools.singledispatch returns:

TypeError: Invalid annotation for 'arg'. list[int|str] is not a class.

This means we have to simplify the annotation at the expense of complicating the function, and less support for mypy:

@dispatch.register
def _(arg: list):
    # list[int|str]
    if all(isinstance(i, (int,str)) for i in arg):
        return "this is a list of ints and strings!"

    # elif <other types>:
        # other logic

    else:
        # list[any]

Personally I find this antitheitcal to singledispatch, which is supposed to solve this exact problem! The entire reason I reached for singledispatch was to replace these if-ridden router functions, and I refused to accept this.

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

containerdispatch-0.1.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

containerdispatch-0.1.1-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file containerdispatch-0.1.1.tar.gz.

File metadata

  • Download URL: containerdispatch-0.1.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for containerdispatch-0.1.1.tar.gz
Algorithm Hash digest
SHA256 088712041ae8e476a513f54ff377c7500afff3efd1c19a7ee6480489a450c337
MD5 f46d3d5ef2697160fd84648837980626
BLAKE2b-256 49aa06b362d7af1d104fefe9d6669dd30750d12ae240c6737f03fc3ce0c3de55

See more details on using hashes here.

File details

Details for the file containerdispatch-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for containerdispatch-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b261000660856202a929a16b4d5776f609134e94b9b4291f0ff2cb0fc99b57b
MD5 02b5b8a162fbae768211a47370cf906f
BLAKE2b-256 5b4bf76a87df1b3d4a2bb1fb18b2ab8ebf81bd8cc4cc959f120d5e56fcd5e8b5

See more details on using hashes here.

Supported by

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