Enforce type annotations
Project description
statictypes
statictypes
helps your Python functions do exactly what you asked them to do, and not anything else.
This package delivers three simple decorators:
@statictypes.enforce
: Raise an error if the incorrect argument or return type is given.@statictypes.warn
: Give a warning if the incorrect argument or return type is given.@statictypes.convert
: Try to convert into the given argument type, or return an error.
Example 1
Let's define a simple function, and enforce Python's type annotations.
import statictypes
@statictypes.enforce
def myfunc(text: str, number: int) -> str:
return text + " " + str(number)
myfunc("my number is", 1) # This works as intended
myfunc("my number is", 1.1) # This raises an error
Calling myfunc("my number is", 1)
is valid, but myfunc("my number is", 1.1)
results in:
statictypes.StaticTypeError: Argument 'number' got incorrect type <class'float'>, expected <class'int'>.
Example 2
Let's instead choose to convert the arguments to the given type annotations.
import statictypes
@statictypes.convert
def myfunc(text: str, number: int) -> str:
return text + " " + str(number)
myfunc("my number is", 1) # This works as intended
myfunc("my number is", 1.1) # This gives the same output as above
This time, both myfunc("my number is", 1)
and myfunc("my number is", 1.1)
is valid.
Note, however, that both expressions give the output "my number is 1"
, since number
is in this case always converted to an integer.
Limitations
@statictypes.convert
only works for simple types from where a constructor method can be called, e.g.str
,int
. The decorator does not work on e.g.List[float] -> List[int]
since the following incorrect conversion is attempted:List[int](list_of_floats)
.- Only the builtin generic types
Any, Union, Optional, Dict, List
andTuple
are currently supported. - When using
@statictypes.convert
, if alist
is given to a function expecting anumpy.ndarray
, an emptynumpy.ndarray
is returned with the shape of thelist
. This is because thenumpy.ndarray
hasshape
as its first positional argument, leading to a working but arguably unexpected result.
Requirements
- Python 3.6 or above.
Installation
pip install statictypes
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 statictypes-0.0.2.tar.gz
.
File metadata
- Download URL: statictypes-0.0.2.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56e4810fca3d5890b013874957f4b9d05e1e61c1e65184e28f6f8a15846235c6 |
|
MD5 | 4572ebbd52ca0b09ce8164c7c27422fa |
|
BLAKE2b-256 | 2da12d2fecfeb4e4169e80f5133da97b6320e84a26c49d2e841589653626ef90 |
File details
Details for the file statictypes-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: statictypes-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22072b51df34c90da4db237eba52820a7c3565b79e545ac16e84e0170a504132 |
|
MD5 | 232b7b102266551458a9e48e3eb2ff0a |
|
BLAKE2b-256 | 8e83786fe4a9d607ccda76b1c3f3c60bf4fb90d37055bef2e8253e2133ff82d3 |