Skip to main content

A Runtime method overload decorator.

Project description

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

Example

from typing import List

from pyoverload.class_tools 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 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 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:

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

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

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


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

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.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

strongtyping_pyoverload-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file strongtyping-pyoverload-0.1.0.tar.gz.

File metadata

  • Download URL: strongtyping-pyoverload-0.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for strongtyping-pyoverload-0.1.0.tar.gz
Algorithm Hash digest
SHA256 87e713872b02d439078cde45238b544b45fd89429995034e508c02ade4fd206b
MD5 c1ee2e6b9a3cd6660454b1ff97f377f6
BLAKE2b-256 294164dddc079ddc47a15fb164f6d0f7fbeb8328a94ae12693d271cf4d658bc9

See more details on using hashes here.

File details

Details for the file strongtyping_pyoverload-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strongtyping_pyoverload-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for strongtyping_pyoverload-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36867888781667955a4eced71231533214614a6b1b23df293e4bfa09e21ac31b
MD5 7352afcf05d2e2a7097d13e6682a1595
BLAKE2b-256 8024b64b8d9096fea8bc99deb6601359114215c5014ed450df1431c07b2c1078

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