Skip to main content

a singledispatch alternative for PEP-585 type hinted functions

Project description

Complex 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.0.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.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: containerdispatch-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 397f45f5c1b4d65244442866c4d10898a69d34c98200d66c0123be4fd3ad01de
MD5 c4efa7cc741421324b4fe646bb1f0e36
BLAKE2b-256 7d068399c1def6e5103901bca4712da02fab2ee198a0b867b3a87cd4e6b3400b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for containerdispatch-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a54a8a26ff01c7597fec95ca063153acc3aee3d8607981309dd7e41835b15856
MD5 a03eedf5c8b798da96423e6d5c77c152
BLAKE2b-256 ef3d8d53d1658977d149833295124f46a80c4e916ddbb46ed62b7dd76c599189

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