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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
088712041ae8e476a513f54ff377c7500afff3efd1c19a7ee6480489a450c337
|
|
| MD5 |
f46d3d5ef2697160fd84648837980626
|
|
| BLAKE2b-256 |
49aa06b362d7af1d104fefe9d6669dd30750d12ae240c6737f03fc3ce0c3de55
|
File details
Details for the file containerdispatch-0.1.1-py3-none-any.whl.
File metadata
- Download URL: containerdispatch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6b261000660856202a929a16b4d5776f609134e94b9b4291f0ff2cb0fc99b57b
|
|
| MD5 |
02b5b8a162fbae768211a47370cf906f
|
|
| BLAKE2b-256 |
5b4bf76a87df1b3d4a2bb1fb18b2ab8ebf81bd8cc4cc959f120d5e56fcd5e8b5
|