Adds a simple decorator enforce_types that enables enforcing strict
typing on a function or dataclass using annotations.
Works with collection types and subtypes for example Dict[str, Tuple[int, int]], and with special types as Optional and Any.
Seeing as this uses type annotations from PEP 484, >=Python 3.5 is required.
PyPi project page: https://pypi.org/project/enforce-typing/
Heavily inspired from this SO post so credit goes mostly to him.
Installation
Other than downloading from PyPi with pip, you may also clone the repository and run the usual setuptools process:
$> git clone https://github.com/matchawine/python-enforce-typing.git && cd python-enforce-typing
$> python setup.py {build,install}
Usage
from typing import Any, Union, Optional
from dataclasses import dataclass
from enforce_typing import enforce_types
@enforce_types
@dataclass(frozen=True)
class Toto(object):
this_or_that: Union[str, int]
anything: Any
name: str = ""
value: int = 1
maybe_not: Optional[bool] = None
>>> Toto(this_or_that=list(), anything=2)
TypeError: Expected type 'typing.Union[str, int]' for attribute 'this_or_that' but received type '<class 'list'>')
>>> Toto(this_or_that=1, anything=2, maybe_not=0)
TypeError: Expected type 'typing.Union[bool, NoneType]' for attribute 'maybe_not' but received type '<class 'int'>')
>>> Toto(this_or_that=1, anything=2, name=3)
TypeError: Expected type '<class 'str'>' for attribute 'name' but received type '<class 'int'>')
>>> Toto(this_or_that=1, anything=2, value=3.0)
TypeError: Expected type '<class 'int'>' for attribute 'value' but received type '<class 'float'>')
>>> Toto(this_or_that=1, anything=2)
Toto(this_or_that=1, anything=2, name='', value=1, maybe_not=None)
>>> Toto(this_or_that="titi", anything=list(), maybe_not=False)
Toto(this_or_that='titi', anything=[], name='', value=1, maybe_not=False)
Metadata
Release files for enforce-typing 1.0.0.post1
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 |
|---|---|---|---|---|
| enforce-typing-1.0.0.post1.tar.gz | 15.7 kB | Without provenance | Details for enforce-typing-1.0.0.post1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| enforce_typing-1.0.0.post1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for enforce_typing-1.0.0.post1-py2.py3-none-any.whl |
Total release size: 31.3 kB
Release files / enforce-typing-1.0.0.post1.tar.gz
| Download URL | enforce-typing-1.0.0.post1.tar.gz |
|---|---|
| Size | 15.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
90347a61d08e7f7578d9714b4f0fd8abd9b6bc48c8ac8d46d7f290d413afabb7
|
|
BLAKE2b-256 checksum How to use checksums |
c07eaa1aef70bafe87d88184c20f140596995127ecc7949ca50410b43993ee10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
|
Release files / enforce_typing-1.0.0.post1-py2.py3-none-any.whl
| Download URL | enforce_typing-1.0.0.post1-py2.py3-none-any.whl |
|---|---|
| Size | 15.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
d3184dfdbfd7f9520c884986561751a6106c57cdd65d730470645d2d40c47e18
|
|
BLAKE2b-256 checksum How to use checksums |
6ab5eea1c6b9f5e739e00b2ceb2925ce25a2a36c7503a5e23d6d818d22ca2631
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0
|
Security for enforce-typing 1.0.0.post1
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.