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 details)

Uploaded Source

File details

Details for the file overloader-0.1.1.tar.gz.

File metadata

  • Download URL: overloader-0.1.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for overloader-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3fae7dd5ccc630d78c9f968c406d2b8301f60d4053d30e2a1f4429ad9387a87a
MD5 c4ecd84c7bb6558af199276a48a259ac
BLAKE2b-256 0e24a7b7d3dea05647253cef9fb967632c32f80cf311f1fa3ed2c340074c9775

See more details on using hashes here.

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