Skip to main content

Python3 function overload lib, based on pure python and native typing.

Project description

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.

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

overloader-0.1.1.tar.gz (11.8 kB view hashes)

Uploaded Source

Supported by

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