Skip to content

A smart type checking decorator for functions with useful features

License

Notifications You must be signed in to change notification settings

woodenbell/TyPy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TyPy 👾

One decorator, multiple possibilities of type checking

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


Before we get started, take a look at this example:

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

"""

Type checking specifications

Remember: values with a subclass type of the required type are also accepted (except if you use the NOT_SUBCLASS flag)

value: type

Simplest way of checking, only an object of specified type is allowed

value: { type1, type2, type3... }

Checks if value type is one of the given types

value: [type1, type2]

Works for set, tuple and list

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]] )

value: [dict_type, key_type, value_type]

Works for (since 1.1)

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.

value: (type1, type2, type3)

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))

Flags (since 1.1)

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:

{type-checking : [flag1, flag2....]}

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

none_safe decorator

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