Overloadlib¶
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
usesget_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