A robust Python library for validating and transforming type annotations with comprehensive safety checks and automatic import resolution.
- Type Annotation Validation: Validates Python type annotations for safety and correctness
- Automatic Import Resolution: Automatically resolves and adds proper module prefixes to type names
- Safety Checks: Uses
safelibto ensure all types are safe to use - Comprehensive Error Handling: Provides detailed error reporting and validation results
- Support for Complex Types: Handles Union types, Generics, Callables, and more
pip install -U safelib pydantic typing_extensionsfrom validator import TypeValidator
# Validate a simple type annotation
validator = TypeValidator('Union[Callable[[], int], int]')
result = validator.validate_names()
print(result.validated_type)
# Output: typing.Union[typing.Callable[[], builtins.int], builtins.int]
print(result.pytype)
# Output: typing.Union[typing.Callable[[], int], int]
print(result.is_valid)
# Output: TrueThe main class for validating type annotations.
TypeValidator(annotation: str)Parameters:
annotation(str): The type annotation string to validate
Validates all type names in the annotation and returns a comprehensive result.
Returns:
ValidationResult: A detailed result object containing validation information
Extracts all type names from the annotation string.
Parameters:
annotation(str, optional): Custom annotation string. Uses instance annotation if not provided.
Returns:
list[str]: List of extracted type names
A Pydantic model representing the validation result.
validated_type(str): The validated annotation with proper module prefixeserrors(list[Exception]): List of errors encountered during validationpytype(typing.Any | None): The actual Python type object if validation succeededinvalid_names(list[str]): List of names that failed validationtype_map(dict[str, str]): Mapping of type names to their import origins
Returns True if validation was successful (no invalid names and pytype is available).
Gets the module origin of a specific type name.
Parameters:
type_name(str): The name of the type
Returns:
str | None: The origin module or None if not found
validator = TypeValidator('list[str]')
result = validator.validate_names()
print(result.validated_type) # builtins.list[builtins.str]
print(result.type_map) # {'list': 'builtins', 'str': 'builtins'}validator = TypeValidator('Dict[str, Optional[List[int]]]')
result = validator.validate_names()
print(result.validated_type)
# typing.Dict[builtins.str, typing.Optional[builtins.list[builtins.int]]]validator = TypeValidator('InvalidType[str]')
result = validator.validate_names()
print(result.is_valid) # False
print(result.invalid_names) # ['InvalidType']
print(result.errors) # List of validation errorsvalidator = TypeValidator('Callable[[str, int], bool]')
result = validator.validate_names()
print(result.validated_type)
# typing.Callable[[builtins.str, builtins.int], builtins.bool]The validator automatically recognizes and handles types from:
builtins: Built-in Python types (int, str, list, dict, etc.)typing: Standard typing module types (Union, Optional, Generic, etc.)typing_extensions: Extended typing features
- Safe Import Resolution: Uses
safelibto ensure only safe, known types are resolved - Input Validation: Validates annotation strings before processing
- Error Isolation: Catches and reports errors without crashing
- Name Sanitization: Prevents injection of unsafe code through type names
The validator uses compiled regex patterns for efficient name extraction and handles complex nested types with minimal performance overhead.
Contributions are welcome! Please ensure all code follows the existing style and includes appropriate tests.
This project is licensed under the GPL License.
safelib>=0.6.0: For safe import resolution and validationpydantic: For result model validationtyping_extensions: For extended typing support
- Python 3.10+
- Supports all standard typing constructs
- Compatible with both
typingandtyping_extensionsmodules