Skip to main content

A Runtime method overload decorator.

Project description

strongtyping-pyoverload

Python 3.9 Code style: black Imports: isort Python application Python tox image

A Runtime method overload decorator which should behave like a compiled language

  • there is a override decorator from typing which works only for static type checking
  • this decorator works on runtime

Install

pip install strongtyping-pyoverload

Examples

from typing import List

from strongtyping_pyoverload import overload


class Example:
    @overload
    def my_func(self):
        """
        Base information about the func
        """
        return 0

    @overload
    def my_func(self, a: int, b: int):
        """
        Why this one
        :param a:
        :param b:
        :return:
        """
        return a * b

    @overload
    def my_func(self, a: int, b: int, c: int):
        """
        What the hell
        :param a:
        :param b:
        :param c:
        :return:
        """
        return a * b * c

    @overload
    def my_func(self, *, val: int, other_val: int):
        """
        Now kwargs only
        :param val:
        :param other_val:
        :return:
        """
        return val, other_val

    @overload
    def my_func(self, val: List[int], other_val, /):
        """
        Pos only
        :param val:
        :param other_val:
        :return:
        """
        return [other_val * v for v in val]

    @overload
    def my_func(self, val: List[str], other_val, /):
        """
        Pos only but special for `string` elements
        :param val:
        :param other_val:
        :return:
        """
        return ''.join(val), other_val


if __name__ == "__main__":
    example = Example()
    assert example.my_func() == 0
    assert example.my_func(2, 3, 4) == 24
    assert example.my_func([1, 2, 3], 3) == [3, 6, 9]
    assert example.my_func(2, "3") == "33"
    assert example.my_func([1, 2, 3, 4], 10) == [10, 20, 30, 40]
    assert example.my_func(["1", "2", "3", "4"], 2) == ('1234', 2)
    help(example.my_func)
"""
Help on method my_func:

my_func(val: List[str], other_val, /) method of __main__.Example instance
    Base information about the func
    
    
    Why special
    :param a:
    :param b:
    :return:
    
    
    Why this one
    :param a:
    :param b:
    :return:
    
    
    What the hell
    :param a:
    :param b:
    :param c:
    :return:
    
    
    Now kwargs only
    :param val:
    :param other_val:
    :return:
    
    
    Pos only
    :param val:
    :param other_val:
    :return:
    
    
    Pos only but special for `string` elements
    :param val:
    :param other_val:
    :return:

"""

Do I need to add a type hint for each parameter??

  • the is answer no you only need to have one typed parameter which differ
from strongtyping_pyoverload import overload


class Other:

    @overload
    def other_func(self, a: int, b):
        return (a + b) * (a + b)

    @overload
    def other_func(self, a: str, b):
        return f'{a.lower()}_{b.lower()}'

    @overload
    def other_func(self, a: list, b):
        return len(a) * b


>>> other = Other()
>>> other.other_func("Hello", "World")
hello_world
>>> other.other_func(2, 2)
16
>> > other.other_func([1, 2, 3], 2)
6
  • or have a different length for your parameters
from strongtyping_pyoverload import overload


class Other:

    @overload
    def other_func(self, a):
        return a ** a + a

    @overload
    def other_func(self, a, b):
        return (a * a) / b

    @overload
    def other_func(self, a, b, c):
        return a + b + c


>>> other = Other()
>>> other.other_func(2)
6
>>> other.other_func(2, 3)
1.333333333333333
>>> other.other_func(2, 3, 4)
9
  • subclasses can overwrite an existing function but these must match the exact type definition
from strongtyping_pyoverload import overload


class Example:
    @overload
    def other_func(self):
        return 0

    @overload
    def other_func(self, a: int, b: int):
        return (a * a) / b


class Other(Example):

    @overload
    def other_func(self, a):
        return a ** a + a

    @overload
    def other_func(self, a: int, b: int):
        return ((a * a) / b) + a


>>> other = Other()
>>> other.other_func()
0
>>> other.other_func(2)
6
>>> other.other_func(2, 3)
3.333333333333333

What happens when no function matches

  • when no function matches with the parameter you're using then an AttributError will be raised
from strongtyping_pyoverload import overload


class Example:
    @overload
    def other_func(self, a: int, b: int):
        return (a + b) * (a + b)

    
>>> example = Example()
>>> example.other_func("Not", "Supported")
Traceback (most recent call last):
...
AttributeError: Example has no function which matches with your parameters ("Not", "Supported") {}

Installation

  • pip install strongtyping-pyoverload

Versioning

  • For the versions available, see the tags on this repository.

Authors

  • Felix Eisenmenger

License

  • This project is licensed under the MIT License - see the LICENSE.md file for details

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

strongtyping-pyoverload-0.1.2.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

strongtyping_pyoverload-0.1.2-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

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