Skip to main content

A python package to implement overloading of functions in python.

Project description

PyPI Status Python Version License

Read the documentation at https://overloadlib.readthedocs.io/ Tests Codecov

pre-commit Black

Features

  • Introduces @overload, @override and @<Function>.add decorators, allowing one to overload and override functions. Functions are then called according to their argument types:

@overload
def func(var: str):
   return var

# via @<Function>.add
@func.add
def _(var: int) -> str:
   return str(var * 5)

# via @overload
@overload
def func() -> str:
   return "Functions don't need to have arguments."

# via @override
@override(funcs=[func])
def new(str_1: str, int_1: int):
   return str_1 * int_1

assert func("a") == "a" == new("a")
assert func(1) == "5" == new(1)
assert func() == "Functions don't need to have arguments." == new()
assert new("house", 2) == "househouse"
  • Raises human readable errors, if no callable was determined with the given arguments. For example the following given:

@overload
def some_func(str_1: str, int_1: int):
   return str_1 + str(int_1)

@overload
def some_func(str_1: str):
   return str_1

Calling:

>>> some_func(str_1=2)
PyOverloadError:
Error when calling:
(__main__.some_func):
        def some_func(str_1: int):
                ...:
        'str_1' needs to be of type (<class 'str'>,) (is type <class 'int'>)

or

>>> some_func(10)
__main__.NoFunctionFoundError: No matching function found.
Following definitions of 'some_func' were found:
(__main__.some_func):
        def some_func(str_1: str, int_1: int):
                ...
(__main__.some_func):
        def some_func(str_1: str):
                ...
The following call was made:
(__main__.some_func):
        def some_func(int_1: int):
                ...
  • Any type of variables is allowed: Build-in ones like str, int, List but also own ones, like classes etc.

  • @overload uses get_type_hints to identify the right function call via type-checking. Hence, it may also be used as a type-checker for functions.

  • Forgot, which overloads of a specific function have been implemented? No worries, you can print them with their typing information using print(func_versions_info(<my_func>)), e.g.

>>> print(func_versions_info(some_func))

Following overloads of 'some_func' exist:
(__main__.some_func):
         def some_func(str_1: str, int_1: int):
               ...
(__main__.some_func):
         def some_func(str_1: str):
               ...

Requirements

Requires Python 3.7+.

Installation

You can install Overloadlib via pip from PyPI:

$ pip install overloadlib

or install with Poetry

$ poetry add overloadlib

Then you can run

$ overloadlib --help

or with Poetry:

$ poetry run overloadlib --help

<details> <summary>Installing Poetry</summary> <p>

To download and install Poetry run (with curl):

$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -

or on windows (without curl):

$ (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py -UseBasicParsing).Content | python -

</p> </details>

Uninstall

If you wan to uninstall the package, simply run

$ pip uninstall overloadlib

or with Poetry:

$ poetry remove overloadlib

Usage

Please see the Command-line Reference for details.

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, Overloadlib is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated by a template inspired by @cjolowicz’s Hypermodern Python Cookiecutter template and @TezRomacH’s python-package-template

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

overloadlib-0.3.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

overloadlib-0.3.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file overloadlib-0.3.0.tar.gz.

File metadata

  • Download URL: overloadlib-0.3.0.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for overloadlib-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ec65bd940bb5fb8db1870e98b81a074b87f63a61ce61ec37a203ab5b2572bcad
MD5 4ac41a9f75aebed39397c315ae6c9888
BLAKE2b-256 746f5e5359251c09fdd6b8dd424bbb289887e6f1ffe7fb20e60ef2ea3f118903

See more details on using hashes here.

File details

Details for the file overloadlib-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: overloadlib-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for overloadlib-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e5f0db6bbb6bfbf6e7724d1124fdfb47df97db7796a766ecd20d274583b60843
MD5 6c138be4a8dde195bb977ee2ffb644f0
BLAKE2b-256 c29b28d26ddcbcc1512ca4835c5837622bbd39f4f2f6eb4da51b654607149fd2

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