typepy: Runtime Type Checker, Validator, and Converter

1.3.4 · active · verified Thu Apr 09

typepy is a Python library for variable type checking, validation, and conversion at runtime. As of version 1.3.4, it supports Python 3.9 and newer. It maintains an active release cadence, with several minor updates per year addressing compatibility, new Python versions, and bug fixes.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `typepy.Type` class for generic type handling and conversion, including providing default values for `None`. It also shows the usage of specific type classes like `typepy.String` and `typepy.Integer` for more focused operations and how to handle potential conversion errors.

import typepy

# Generic type checking and conversion
tc = typepy.Type('100')
print(f"Converted '100' to integer: {tc.try_get(typepy.Type.INTEGER)}")

tc_none = typepy.Type(None)
print(f"Converted None to string with default: {tc_none.try_get(typepy.Type.STRING, default_value='a')}")

# Using specific type classes
str_value = typepy.String('  hello  ').strip()
print(f"Stripped string: '{str_value}'")

int_value = typepy.Integer('1,234').convert()
print(f"Converted '1,234' to integer: {int_value}")

try:
    # Conversion without try_get will raise an error on failure
    invalid_int = typepy.Integer('abc').convert()
    print(f"Converted 'abc' to integer: {invalid_int}")
except typepy.TypeConversionError as e:
    print(f"Error converting 'abc' to integer: {e}")

view raw JSON →