Prototyping for functions
Project description
Prototypes
Summary
Prototypes allow you to define signatures for your functions and verify those signatures during both static type checking and runtime.
Installation
$ pip install prototypes
Requirements
typing-extensions >= 3.10 when using Python < 3.10
Basic usage
To validate your function against prototype, decorate your function with the prototype decorator and pass the prototype function as a parameter.
from prototypes import prototype
def add(x: int, y: int) -> int:
...
@prototype(add)
def custom_add(x: int, y: int) -> int:
return x + y
Static type checking is fully supported thanks to usage of both typing.TypeVar (PEP 484) and typing.ParamSpec (PEP 612) under the hood.
from prototypes import prototype
def add(x: int, y: int) -> int:
...
# This will report type error by static type checkers like `mypy` in the future
@prototype(add)
def compute() -> None:
pass
Advanced usage
Prototype functions does not have to be empty. They can be regular functions that already contain specific implementation. The prototype function act more like a signature template for the function, rather than an empty shell to be filled with the implementation.
from prototypes import prototype
def add(x: int, y: int) -> int:
return x + y
@prototype(add)
def custom_add(x: int, y: int) -> int:
...
return add(x, y)
By default, the prototype decorator verifies the signatures during runtime. Since decorators are executed during the function definition, this validation is performed right away, even if function is never called.
>>> from prototypes import prototype
>>>
>>> def add(x: int, y: int) -> int: ...
...
>>> @prototype(add)
... def compute() -> None:
... pass
...
Traceback (most recent call last):
...
prototypes.PrototypeError: Incompatible function implementation for given prototype
Function:
def compute() -> None @ compute
Prototype:
def add(x: int, y: int) -> int @ add
However, since closures (inner functions) are defined on function execution, using prototype decorator in the closure will have no effect until the outer function is called.
>>> from prototypes import prototype
>>>
>>> def add(x: int, y: int) -> int: ...
...
>>> def func() -> None:
... @prototype(add)
... def compute() -> None:
... ...
...
>>> # No exception is raised at that point
>>> func()
Traceback (most recent call last):
...
prototypes.PrototypeError: Incompatible function implementation for given prototype
Function:
def compute() -> None @ func.<locals>.compute
Prototype:
def add(x: int, y: int) -> int @ add
The runtime validation can be turned off when static type checking is performed to increase the code performance during runtime.
>>> from prototypes import prototype
>>>
>>> def add(x: int, y: int) -> int: ...
...
>>> @prototype(add, runtime=False)
... def compute() -> None:
... pass
...
>>> # No exception is raised during runtime
Bugs/Requests
Please use the GitHub issue tracker to submit bugs or request features.
License
Copyright Krzysztof Przybyła, 2021.
Distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file prototypes-1.0.0.tar.gz
.
File metadata
- Download URL: prototypes-1.0.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4fa68cb75334aa7d3972618afa74096570e4e45422de97376c4010046bb5cf4 |
|
MD5 | 61481a034053f869269ddb40416c2cab |
|
BLAKE2b-256 | c5f04480b7e49e325372a65f95ec0f50ce588a9d6adf3083be306f268d7205c1 |
File details
Details for the file prototypes-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: prototypes-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce0868d2ffbbe6ce0c3f28a9dbc85e5d2b739b21cfd318c7f84648f14b774a41 |
|
MD5 | 467ed79b9542e216cf8bcb33f5e73561 |
|
BLAKE2b-256 | f1675d845efd1f81c629f42adad7e5ec38a96a813dbc4492f6a9d5ebdf41b240 |