TyPy can check types on data structures, tuples (separate types) and a list of allowed types.
Remember: TyPy decorator automatically skips arguments without annotations, so don't worry
if you want to keep some of them unchecked 😉
Warning: after version 1.1, TyPy is no longer None-safe by default.
To make an argument None-safe, use the NONE_SAFE
flag
import typy.typed
@typy.typed
def f(string: str, numeric: {float, int}, employees: [list, [dict, str, str]]) -> (str, bool):
#...
return ("OK", True)
"""
All subclasses of type are accepted by dafault, use the NOT_SUBCLASS flag if you don't want them to
be accepted.
string => must be of type str (or subclass)
numeric => must be one of the types listed (int , float)
employees => must be of a list containing dictionaries with keys and values of
type str
return type => a tuple with first element of type str and second
element of type bool
"""
Remember: values with a subclass type of the required type are also accepted (except if you use the NOT_SUBCLASS
flag)
Simplest way of checking, only an object of specified type is allowed
Checks if value type is one of the given types
Checks if the value type is on the first element, then checks if (expecting that the value is a data structure) it's elements are of type 2 Tip: the list can be nested so you can check for data structures inside other data structures (e.g. [type1, [type2, type3]] )
The third element was introduced in version 1.1 to separate the key
type and value type. Both types were indicated by the second value before
the update.
Checks for a tuple whose elements are of the given types.
For example, if the type checking is set to (bool, str, int), one valid value would be (True, "hello", 3)
This kind of type checking fails if the given tuple differs in length of the value.
This feature is useful for checking key-value values (e.g. ("number", 3))
Using flags is a new way to specify extra preferences to your type checking. TyPy currently has 2 flags:
- NONE_SAFE: specifies that the argument will not accept None values.
- NOT_SUBCLASS: specifies that subclasses of the type will not be accepted.
Using flags is very simple, take a look:
When using flags, you will use a dictionary where the only key is the type checking specification, and the only value is a list (only lists will be accepted) containing all the flags.
Take an example below:
@typy.typed
def func(a : {[list, int] : [typy.NONE_SAFE]}):
pass
The none_safe
decorator is a small utility for None-safety without type checking. Here's a small example:
import typy
@typy.none_safe
def func(never_none, never_none_too):
pass