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 "<stdin>", line 1, in <module>
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
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.
Version History (in Brief)
1.1 altered the text of the invalid call TypeError. Removed debug prints.
1.0 the initial release
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
Metadata
Release files for overload 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| overload-1.1.tar.gz | 4.6 kB | Without provenance | Details for overload-1.1.tar.gz |
Release files / overload-1.1.tar.gz
| Download URL | overload-1.1.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
215740c5c374b6039e1fb196e127316d3203b76795e2aa137a7f9376621b5592
|
|
BLAKE2b-256 checksum How to use checksums |
3949241d870d1f93fcd9c6d426254128ed0bc3ca524cf963d8f5414540482853
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for overload 1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.