
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Simple overloading of methods and functions through an @overload decorator.
This module allows one to provide multiple interfaces for a functions, methods, classmethods, staticmethods or classes. See below for some notes about overloading classes, you strange person you.
The appropriate implementation is chosen based on the calling argument pattern.
For example:
class A(object): ... @overload ... def method(self, a): ... return 'a' ... @method.add ... def method(self, a, b): ... return 'a, b' ... a = A() a.method(1) 'a' a.method(1, 2) 'a, b'
The overloading handles fixed, keyword, variable (*args
) and arbitrary
keyword (**keywords
) arguments.
It also handles annotations if those annotations are types:
@overload ... def func(a:int): ... return 'int' ... @func.add ... def func(a:str): ... return 'str' ... func(1) 'int' func('s') 'str' func(1.0) Traceback (most recent call last): File "", line 1, in File "overload.py", line 94, in f raise TypeError('invalid call argument(s)') TypeError: invalid call argument(s)
This feature (and currently the module in general) requires Python 3.
The docstring and name (ie. documentation) of the resultant callable will match that of the first callable overloaded.
Overloading classes allows you to select a class type based on the construction arguments of each alternative type's new method.
There's a catch though: the new method must explicitly invoke the base class new method, rather than use super() like usual. This is because after being @overloaded the class is a function, and super() doesn't like being passed functions. So instead of::
@overload
class A(object):
def __new__(cls):
# this will fail because "A" is a function now
return super(A, cls).__new__(cls)
you must::
@overload
class A(object):
def __new__(cls):
# must explicitly reference the base class
return object.__new__(cls)
I'll leave it up to the reader to justify their use of @overloading classes.
See the end of the source file for the license of use.
I would be interested to know whether this module is useful - if you use it please indicate so at https://www.ohloh.net/p/pyoverload
FAQs
Simple overloading of methods and functions through an @overload decorator
We found that overload demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.