flexible explicit type checking of function arguments (Python3-only)
Project description
A decorator for functions, @tc.typecheck, to be used together with Python3 annotations on function parameters and function results. The decorator will perform dynamic argument type checking for every call to the function.
@tc.typecheck def foo1(a:int, b=None, c:str="mydefault") -> bool : print(a, b, c) return b is not None and a != b
The parts :int, :str, and -> bool are annotations. This is a syntactic feature introduced in Python 3 where : (for parameters) and -> (for results) are delimiters and the rest can be an arbitrary expression. It is important to understand that, as such, annotations do not have any semantics whatsoever. There must be explicit Python code somewhere that looks at them and does something in order to give them a meaning.
The @tc.typecheck decorator gives the above annotations the following meaning: foo1’s argument a must have type int, b has no annotation and can have any type whatsoever, it will not be checked, c must have type string, and the function’s result must be either True (not 17 or "yes" or [3,7,44] or some such) or False (not 0 or None or [] or some such).
If any argument has the wrong type, a TypeCheckError exception will be raised. Class types, collection types, fixed-length collections and type predicates can be annotated as well. Here is a more complex example:
import typecheck as tc @tc.typecheck def foo2(record:(int,int,bool), rgb:tc.re("^[rgb]$")) -> tc.any(int,float) : a = record[0]; b = record[1] return a/b if (a/b == float(a)/b) else float(a)/b foo2((4,10,True), "r") # OK foo2([4,10,True], "g") # OK: list is acceptable in place of tuple foo2((4,10,1), "rg") # Wrong: 1 is not a bool, string is too long foo2(None, "R") # Wrong: None is no tuple, string has illegal character
These annotations mean that record is a 3-tuple of two ints and an actual bool and rgb is a one-character string that is either “r” or “g” or “b” by virtue of a regular expression test. The result will be a number that can be either int or float.
Other kinds of annotations:
tc.optional(int) will allow int and None,
tc.enum(1, 2.0, "three") allows to define ad-hoc enumeration types,
tc.map_of(str, tc.list_of(Person)) describes dictionaries where all keys are strings and all values are homogeneous lists of Persons,
and so on.
Find the documentation at https://github.com/prechelt/typecheck-decorator
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 Distributions
File details
Details for the file typecheck-decorator-stable-1.0.tar.gz
.
File metadata
- Download URL: typecheck-decorator-stable-1.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8145b7494679e8fe9e2fddddd5c8feab268eaa84366f5acab4ab6092c52bd71 |
|
MD5 | 4c210772810ea7e0d0a58c4f5e1aa088 |
|
BLAKE2b-256 | bad69bc4f046676c454f55c84c3f1fe643795b8fda2e78a7ea1904f3a8441180 |
File details
Details for the file typecheck_decorator_stable-1.0-py3-none-any.whl
.
File metadata
- Download URL: typecheck_decorator_stable-1.0-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 677ce713f902612c6a8c9d8afa15541f1095efe8dc4b29ae56ce238af0e9a491 |
|
MD5 | 92240f20ad47e1539755f533bd7b8854 |
|
BLAKE2b-256 | 46155a5e3a83dcaf31ccf4b518d59174b408cd94808d7345d3234f744cd8613b |
File details
Details for the file typecheck-decorator-stable-1.0.macosx-10.10-x86_64.tar.gz
.
File metadata
- Download URL: typecheck-decorator-stable-1.0.macosx-10.10-x86_64.tar.gz
- Upload date:
- Size: 34.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f83b18911dddd04f6d6656113299488c3f26aeeccec0bf28d347f2f3db1bd2d1 |
|
MD5 | b3048a8bb4408c119ddc47d9992507b6 |
|
BLAKE2b-256 | dbed23df1afb8c75c6927552da4402da6599c16e1ed6f22b1a1aca3a4d894c7f |