Skip to main content

Overload

versions

About

"Overload" is a Python 3 package for functions overloading. "Overload" use pure python3 typing and hasn't requirements. Support python version >= 3.7.

Usage

Install package

pip install overloader

Overloading functions example.

To overload function you need importing overload decorator from package and using it with your overloading target function. Now this function becomes a default implementation and will be called in the last place or when call arguments will success compare with it. For registering new implementation of it, use it "register" method. Untyped variables be ignored.

from overload import overload

@overload
def my_function(var1: int, var2: str):
    # function logic.
    print('I am a default function.')

@my_function.register
def _(var1:str, var2: int):
    # implementation logic.
    print('I am an implementation.')

When you call "overload object", will called implementation with success compare variables types or default implementation.

my_function(1, 'string')
# stdout: I am a default function.

my_function('string', 1)
# stdout: I am an implementation.

Overloading function specific parameters typing.

For typing specific parameters, like *args or **kwargs, must use special type Args and Kwargs.

from typing import Union
from overload import Args, Kwargs, overload

@overload
def my_function(*args: Args[int], **kwargs: Kwargs[Union[str, int]]):
    pass

If you ignored special types and set type for it like regular parameter *args: int, this will interpret not as *args parameter, but args.

Overloading function parameters.

Overload decorator has two settings:

  • strict (bool, default=True)
  • overlapping (bool, default=False)

Strict - activate validation of implementation annotations count compared overload object annotations.

@overload(strict=True)
def my_function(var1: str):
    ...

@my_function.register
def _(var1: str, var2: int):
    ...

# Will raised AnnotationCountError.

Overlapping - activate registration of implementation with same annotations as the default overload object.

@overload(overlapping=False)
def my_function(var1: str):
    ...

@my_function.register
def _(var1: str):
    ...

# Will raised OverlappingError.

Metadata

Release files for overloader 0.1.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 overloader 0.1.1
File Size Uploaded
overloader-0.1.1.tar.gz 11.8 kB Details

Release files / overloader-0.1.1.tar.gz

Download URL overloader-0.1.1.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3fae7dd5ccc630d78c9f968c406d2b8301f60d4053d30e2a1f4429ad9387a87a
BLAKE2b-256 checksum
How to use checksums
0e24a7b7d3dea05647253cef9fb967632c32f80cf311f1fa3ed2c340074c9775
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.9

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

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