from strict_func.core import strict_func
@strict_func
def foo(a : int, b : str):
# do something
foo('foo', 'bar') # raises an error
Currently, strict-func cannot check for all possible annotations. The table below shows the current possible types
Annotation types | Descriptions | Examples |
---|---|---|
Standard types | type(annot) == type |
int, str, list, dict, classes etc... |
Union types | type(annot) == UnionType |
int | str |
Checker types | strict_func.checkers |
Dict, List |
strict_func supports standard types annotations. You can use custom python types and other types as annotation for the function.
class Bar:
pass
class Foo:
pass
@strict_func
def fooBar(foo : Foo, bar : Bar):
# do something
fooBar(Bar(), Foo()) # raises an error
strict_func also supports union types annotations. These are used when a parameter can be either of the union.
@strict_func
def fooBar(foo : int | str, bar : list | str):
# do something
fooBar('foo', 'bar') # no error
strict_func currently has two checker types Dict and List for the dictionaries and lists respectively. They are used to further check not only only the list and dictionaries but the items the contain
from strict_func.checkers import List, Dict
@strict_func
def foo(a : List[int]):
# do something
foo([1,3,4,5,6]) # no error
foo([1,2,3,'foo']) # raises an error
@strict_func
def foo(a : List[int, str, list]):
# do something
foo([1,'foo', [1,2]]) # no error
foo([1,2,3]) # raises an error
To resolve this add
...
at the end of the type list.List[type1, type2, ...]
@strict_func
def foo(a : Dict[str, int]):
# do something
foo({'age' : 2}) # no error
foo({'name' : 'foo'}) # raises an error
@strict_func
checker = Dict[{
'name' : str,
'age' : int,
'scores' : list
}]
def foo(a : checker):
# do something
foo({
'name' : 'foo',
'age' : 9,
'scores' : [10, 9, 10]
}) # no error
foo({'name' : 21}) # raises an error