Skip to main content

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

Project description

Summary

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

PyPI package version conda-forge package version Supported Python versions Supported Python implementations Linux/macOS/Windows CI status Test coverage CodeQL

Features

  • checking a value type

  • validate a value for a type

  • convert a value from one 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

Installation

Installation: pip

pip install typepy

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

pip install typepy[datetime]

Installation: conda

conda install -c conda-forge typepy

Installation: apt

sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-typepy

Dependencies

Optional dependencies

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

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 was 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 class constructors as an argument. More information can be found in the API reference.

Documentation

https://typepy.rtfd.io/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

typepy-1.3.5.tar.gz (39.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typepy-1.3.5-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file typepy-1.3.5.tar.gz.

File metadata

  • Download URL: typepy-1.3.5.tar.gz
  • Upload date:
  • Size: 39.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typepy-1.3.5.tar.gz
Algorithm Hash digest
SHA256 a1c5f54c41860f89bab175f512b11e8c9a57cfe7b8b3d5ae5d52d828b756b6dd
MD5 afa392a775958479944be283ed420a4c
BLAKE2b-256 8b9fae119b0e0fd0fe8dcb0e1eeebfeb62f37fdc0b467267cff15cdb746ba38b

See more details on using hashes here.

Provenance

The following attestation bundles were made for typepy-1.3.5.tar.gz:

Publisher: publish.yml on thombashi/typepy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file typepy-1.3.5-py3-none-any.whl.

File metadata

  • Download URL: typepy-1.3.5-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typepy-1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 de361b59609c7503efc2edbe9d7a4e053ae71307bf90ae1678ec4d6bcd807922
MD5 01d9183c83e8d973a0155689e000dc11
BLAKE2b-256 647175cf08c49b64a9419f1f2cef9be072ac34f6b784da2851489470b7c7ba15

See more details on using hashes here.

Provenance

The following attestation bundles were made for typepy-1.3.5-py3-none-any.whl:

Publisher: publish.yml on thombashi/typepy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page