A Python library for variable type checker/validator/converter at a run time.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.travis
docs
requirements
test
typepy
.coveragerc
.gitignore
.travis.yml
LICENSE
MANIFEST.in
README.rst
appveyor.yml
make_test_cases.py
pylama.ini
pyproject.toml
setup.cfg
setup.py
tox.ini

README.rst

typepy

Summary

A Python library for variable type checker/validator/converter at a run time.

https://img.shields.io/travis/thombashi/typepy/master.svg?label=Linux/macOS https://img.shields.io/appveyor/ci/thombashi/typepy/master.svg?label=Windows https://coveralls.io/repos/github/thombashi/typepy/badge.svg?branch=master https://img.shields.io/github/stars/thombashi/typepy.svg?style=social&label=Star

Features

  • checking a value type
  • validate a value for a type
  • convert a value from a type to the other type

The correspondence between Python types and typepy classes are as follows:

Supported Types
Python Type typepy: Type Class
bool Bool
datetime DateTime
dict Dictionary
float/decimal.Decimal (not infinity/NaN) RealNumber
float/decimal.Decimal (infinity) Infinity
float/decimal.Decimal (NaN) Nan
int Integer
list List
None None
str (not null) String
str (null) NullString
str (IP address) IpAddress

Usage

Type Check Method

Examples:
>>> from typepy import Integer
>>> Integer(1).is_type()
True
>>> Integer(1.1).is_type()
False

Type Validation Method

Examples:
>>> from typepy import Integer
>>> Integer(1).validate()
>>> try:
...     Integer(1.1).validate()
... except TypeError as e:
...     # validate() raised TypeError when the value unmatched the type class
...     print(e)
...
invalid value type: expected=INTEGER, actual=<type 'float'>

Type Conversion Methods

convert method

Examples:
>>> from typepy import Integer, TypeConversionError
>>> Integer("1").convert()
1
>>> try:
...     Integer(1.1).convert()
... except TypeConversionError as e:
...     # convert() raised TypeConversionError when conversion failed
...     print(e)
...
failed to convert from float to INTEGER

try_convert method

Examples:
>>> from typepy import Integer
>>> Integer("1").try_convert()
1
>>> print(Integer(1.1).try_convert())  # try_convert() returned None when conversion failed
None

force_convert

Examples:
>>> from typepy import Integer, TypeConversionError
>>> Integer("1").force_convert()  # force_convert() forcibly convert the value
1
>>> Integer(1.1).force_convert()
1
>>> try:
...     Integer("abc").force_convert()
... except TypeConversionError as e:
...     # force_convert() raised TypeConversionError when the value not convertible
...     print(e)
...
failed to force_convert to int: type=<class 'str'>

For more information

Type check/validate/convert results differed according to strict_level value which can pass to typepy classes constructors as an argument. More information can be found in the API reference.

Installation

pip install typepy

Install additional dependency packages with the following command if using typepy.DateTime class

pip install typepy[datetime]

Dependencies

Python 2.7+ or 3.4+

Optioal dependencies

These packages can be installed via pip install typepy[datetime]:

Test dependencies

Documentation

https://typepy.rtfd.io/