Skip to main content

Prototypes

py3.6 py3.7 py3.8 py3.9 py3.10

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

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.

Metadata

Release files for prototypes 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prototypes 1.0.0
File Size Uploaded Provenance
prototypes-1.0.0.tar.gz 4.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for prototypes 1.0.0
File Interpreter ABI Platform
prototypes-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / prototypes-1.0.0.tar.gz

Download URL prototypes-1.0.0.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e4fa68cb75334aa7d3972618afa74096570e4e45422de97376c4010046bb5cf4
BLAKE2b-256 checksum
How to use checksums
c5f04480b7e49e325372a65f95ec0f50ce588a9d6adf3083be306f268d7205c1
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / prototypes-1.0.0-py3-none-any.whl

Download URL prototypes-1.0.0-py3-none-any.whl
Size 3.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce0868d2ffbbe6ce0c3f28a9dbc85e5d2b739b21cfd318c7f84648f14b774a41
BLAKE2b-256 checksum
How to use checksums
f1675d845efd1f81c629f42adad7e5ec38a96a813dbc4492f6a9d5ebdf41b240
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for prototypes 1.0.0

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.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page