Typing Validation

raw JSON →
1.2.12 verified Mon Apr 27 auth: no python

A lightweight, dependency-free runtime type-checking library for Python that validates values against arbitrary type annotations, including generics, TypeVars, and nested types. Current version is 1.2.12, released irregularly with incremental features and bug fixes.

pip install typing-validation
error AttributeError: module 'typing_validation' has no attribute 'valid'
cause Typo: 'valid' is not exported; correct function is 'validate'.
fix
Change to 'from typing_validation import validate'
error ImportError: cannot import name 'validate' from 'typing_validation'
cause Package not installed or installed incorrectly.
fix
Run 'pip install typing-validation' and ensure the virtual environment is activated.
breaking In v1.2.6, validate() return type changed from None to Literal[True]. Code relying on return value (e.g., if validate(...)) may break.
fix Use is_valid() for boolean checks, or use assert validate(...) to suppress return value.
deprecated Do not use from typing_validation import valid; that import does not exist.
fix Import validate or is_valid instead.
gotcha Validate does not perform coercion; it only checks types. If validation passes, the value is unchanged.
fix Cast or convert values explicitly before or after validation if needed.
gotcha Support for Type[T] is partial (see issue #18). Some edge cases with generic type variables may not raise.
fix Test thoroughly with complex generics, or use alternatives like pydantic for strict runtime checks.

Basic usage: validate() raises on mismatch, is_valid() returns bool, assertion trick for performance.

from typing_validation import validate

# Validate a simple type
x: int = 10
try:
    validate(x, int)
    print("Valid")
except InvalidTypeException as e:
    print(f"Invalid: {e}")

# Use with assertions (compiled away with -O)
from typing_validation import is_valid
if is_valid(x, int):
    print("x is int")