Giter Club home page Giter Club logo

typy's Introduction

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

typy's People

Stargazers

Carlos Silva avatar

Watchers

Gabriel C. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.