An easy to use decorator to enforce static typing for function and dataclasses.
Project description
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: [TBU]
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)
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 enforce-typing-1.0.0.tar.gz
.
File metadata
- Download URL: enforce-typing-1.0.0.tar.gz
- Upload date:
- Size: 15.5 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 560b69cfed663e0a4268f3a409d3f06db4e7084d8f2eeca768a7ebcf8b6f7996 |
|
MD5 | 86a6d6ab0beebc1f91e0fa1969d47860 |
|
BLAKE2b-256 | 8deb986a28a4597b00969cdd48c3d7974e94c6ca16e24e5040562a74355ee89a |
File details
Details for the file enforce_typing-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: enforce_typing-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 15.4 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34cee2afdbad84404e6ace8303c67de6144f0bcc21fac8c76638a2217e6d5568 |
|
MD5 | c0ecd3b2e9c447cec8534df5d64e2f8f |
|
BLAKE2b-256 | 78a7715551f12856fb48c51b6389fe7d113113d57fb9ce5275c0a75014de6349 |