Skip to main content

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

Metadata

Release files for overloadlib 0.3.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 overloadlib 0.3.0
File Size Uploaded
overloadlib-0.3.0.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for overloadlib 0.3.0
File Interpreter ABI Platform
overloadlib-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / overloadlib-0.3.0.tar.gz

Download URL overloadlib-0.3.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ec65bd940bb5fb8db1870e98b81a074b87f63a61ce61ec37a203ab5b2572bcad
BLAKE2b-256 checksum
How to use checksums
746f5e5359251c09fdd6b8dd424bbb289887e6f1ffe7fb20e60ef2ea3f118903
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / overloadlib-0.3.0-py3-none-any.whl

Download URL overloadlib-0.3.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e5f0db6bbb6bfbf6e7724d1124fdfb47df97db7796a766ecd20d274583b60843
BLAKE2b-256 checksum
How to use checksums
c29b28d26ddcbcc1512ca4835c5837622bbd39f4f2f6eb4da51b654607149fd2
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.1.2

2 release files

0.1.1

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