SpiderType is a Python library designed for handling and managing data types with static typing, inspired by TypeScript. This library provides a foundation for creating and registering custom types, validating values against these types, and applying static type checks to class methods.
Clone the GitHub repository and install the requirements:
git clone https://github.com/simondev413/spidertype.git
cd spidertype
pip install -r requirements.txt
To register a new custom type, use the register_type
method of the SpiderTypeMap
class:
from spidertype.core import SpiderTypeMap
from spidertype.types import Type
class MyCustomType(Type):
def validate(self, value):
# Custom validation logic
pass
type_map = SpiderTypeMap()
my_type = MyCustomType('CustomType')
type_map.register_type('CustomType', my_type)
To retrieve a registered type, use the get_type
method of the SpiderTypeMap
class:
retrieved_type = type_map.get_type('CustomType')
To validate a value against an expected type, use the validate
method of the SpiderTypeMap
class:
try:
type_map.validate(value_to_validate, 'CustomType')
except TypeValidationError as e:
print(e)
To apply static type checking to class methods, use the static_type_check
decorator:
from spidertype.validation import static_type_check
class MyClass:
@static_type_check
def my_method(self, param: CustomType):
pass
The SpiderType library defines several exceptions for different errors:
SpiderTypeError
: Base exception for all errors in the SpiderType library.TypeValidationError
: Exception raised when type validation fails.RegistrationError
: Exception raised when an error occurs during type registration.TypeNotFoundError
: Exception raised when a specified type is not found.InvalidTypeError
: Exception raised when an invalid type is used.
register_type(name, type_def)
: Registers a new custom type.get_type(name)
: Retrieves a registered custom type.validate(value, type_name)
: Validates a value against an expected type.
- Base exception for all errors in the SpiderType library.
- Exception raised when type validation fails.
- Exception raised when an error occurs during type registration.
- Exception raised when a specified type is not found.
- Exception raised when an invalid type is used.
- Base class for custom static typed classes.
validate(value)
: Validates a value against the custom type.
- Decorator to apply static type checking to class methods.
- Function to validate a value against an expected type.
For more details, refer to the full library documentation.