Code Monkey home page Code Monkey logo

python-type-valid's Introduction

python-type-valid

Python type valid Build passed Python type valid Code Coverage Maintainability

Python type validation decorator, validates input and output types

Installation

pip install type-valid

Usage

from type_valid import type_valid

@type_valid
def hello(name: str) -> str:
    return name

#valid
hello("Bob")

@type_valid
def hello_second(name: str) -> int:
    return name

#invalid output
hello_second("edds")

@type_valid
def user(name: str, age: int, salary: float) -> str:
    return name+str(age)+str(salary)

#valid
user(name="Bob", age=25, salary=1000.00)

Raises TypeError

TypeError: in method 'hello', Argument 'name' is not of type <class 'str'>, received <class 'int'>
TypeError: in method 'user', Argument 'name' is required
TypeError: in method 'hello_second', output 'edds' is not of type <class 'int'>, received <class 'str'>

Todo:

  • Validate input DONE
  • Validate Output DONE
  • Validate input **kwargs ?
  • Validate mandatory **kwargs ?
  • Multiple types ?

Types examples

str ""
int 4
float 10.5
list []
tuple ()
...

python-type-valid's People

Contributors

edmhs avatar

Stargazers

 avatar  avatar Gilberto Agostinho avatar

Watchers

 avatar

python-type-valid's Issues

support for typing module types

The Standard Library module typing introduces many useful types, which is very useful for type hinting. For instance, one can define the type of the elements expected of a list. The code below fails:

from type_valid import type_valid
from typing import List

@type_valid
def foobar(arg: List[float]) -> bool:
    return True

my_list = [1.1, 3.2, 7.2]
print(foobar(my_list))

While using a regular list type instead of List[float] doesn't:

from type_valid import type_valid

@type_valid
def foobar(arg: list) -> bool:
    return True

my_list = [1.1, 3.2, 7.2]
print(foobar(my_list))

It's worth noting that tools such as mypy support listings.

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.