Skip to main content

Dynamic Overload

Dynamic Overload is a python package that allows you to overload functions and classes using dynamic dispatch depending on the type of the arguments. This allows you to write more readable code and avoid using isinstance and type checks.

The package also detects collisions between the overloads and warns the user about them without breaking the code. In case of collision, the first overload is used. So, its recomended that you put the most specific overloads first before the more general ones.

Installation

Requires python 3.10 or higher

pip install dynamic-overload

Usage

For functions:

from dynamic_overload import overload
from typing import Iterable

@overload
def flatten(x: Iterable[int]) -> Iterable[int]:
    return [y for l in x for y in flatten(l)]

@overload
def flatten(x: int) -> Iterable[int]:
    yield x

assert list(flatten([1, [2, 3], 4])) == [1, 2, 3, 4]
assert list(flatten(1)) == [1]

For classes:

from dynamic_overload import OverloadMeta

class Integer(metaclass=OverloadMeta):
    x: int

    def __init__(self, x: int):
        self.x = x

    def __init__(self, x: str):
        self.x = int(x)

    def __init__(self, x: float):
        self.x = int(x)

assert Integer(1).x == 1
assert Integer('1').x == 1
assert Integer(1.0).x == 1

The package also supports inheritance similar to python abc module. So the previous example can be rewritten as:

from dynamic_overload import Overload

class Integer(Overload):
    x: int

    def __init__(self, x: int):
        self.x = x

    def __init__(self, x: str):
        self.x = int(x)

    def __init__(self, x: float):
        self.x = int(x)

assert Integer(1).x == 1
assert Integer('1').x == 1
assert Integer(1.0).x == 1

License

MIT

Metadata

Release files for dynamic-overload 0.2.0

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

Source distribution (sdist)

Source distribution for dynamic-overload 0.2.0
File Size Uploaded
dynamic_overload-0.2.0.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dynamic-overload 0.2.0
File Interpreter ABI Platform
dynamic_overload-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / dynamic_overload-0.2.0.tar.gz

Download URL dynamic_overload-0.2.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f095ae06b220842643481b61a0fb018ea1dbe7e48d7de70cacb292d8a9aa9de9
BLAKE2b-256 checksum
How to use checksums
11db7f6822c1a56f077b13a2190aa8828beadda48eb07b47e798f4d55a3c3f85
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release files / dynamic_overload-0.2.0-py3-none-any.whl

Download URL dynamic_overload-0.2.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a032bef0dfbbf33c5ec788de61c4d64066e8caa3f45cc552626a09577c1819cb
BLAKE2b-256 checksum
How to use checksums
ec735e674e038e93f32181fbdf646842953a897aa0cf4aa1e4b7dc7d1d73538d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.0

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

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