Statica is a Python library for defining and validating structured data with type annotations and constraints. It provides an easy-to-use framework for creating type-safe models with comprehensive validation for both types and constraints.
Statica was created to address the need for a lightweight, flexible, and dependency-free alternative to libraries like Pydantic. While pydantic is a powerful tool for data validation and parsing, Statica offers some distinct advantages in specific situations:
- Lightweight: Statica has zero dependencies, making it ideal for projects where minimizing external dependencies is a priority.
- Performance: For use cases where performance is critical. Pydantic needs
3x
more memory than Statica for the same models. - Ease of Use: With its simple, Pythonic design, Statica is intuitive for developers already familiar with Python's
dataclasses
and type hinting. It avoids much of the magic and complexity of Pydantic. - Customizable: Statica allows fine-grained control over type and constraint validation through customizable fields and error classes.
- Type Validation: Automatically validates types for attributes based on type hints.
- Constraint Validation: Define constraints like minimum/maximum length, value ranges, and more.
- Customizable Error Handling: Use custom exception classes for type and constraint errors.
- Flexible Field Descriptors: Add constraints, casting, and other behaviors to your fields.
- Optional Fields: Support for optional fields with default values.
- Automatic Initialization: Automatically generate constructors (
__init__
) for your models. - String Manipulation: Strip whitespace from string fields if needed.
- Casting: Automatically cast values to the desired type.
- Field Aliasing: Support for field aliases for parsing and serialization.
You can install Statica via pip:
pip install statica
Define a model with type annotations and constraints:
from statica.core import Statica, Field
class Payload(Statica):
name: str = Field(min_length=3, max_length=50, strip_whitespace=True)
description: str | None = Field(max_length=200)
num: int | float
float_num: float | None
Instantiate the model using a dictionary:
data = {
"name": "Test Payload",
"description": "A short description.",
"num": 42,
"float_num": 3.14,
}
payload = Payload.from_map(data)
print(payload.name) # Output: "Test Payload"
Or instantiate directly:
payload = Payload(
name="Test",
description="This is a test description.",
num=42,
float_num=3.14,
)
Statica automatically validates attributes based on type annotations and constraints:
from statica.core import ConstraintValidationError, TypeValidationError
try:
payload = Payload(name="Te", description="Valid", num=42)
except ConstraintValidationError as e:
print(e) # Output: "name: length must be at least 3"
try:
payload = Payload(name="Test", description="Valid", num="Invalid")
except TypeValidationError as e:
print(e) # Output: "num: expected type 'int | float', got 'str'"
Fields annotated with | None
are optional and default to None
:
class OptionalPayload(Statica):
name: str | None
payload = OptionalPayload()
print(payload.name) # Output: None
You can specify constraints on fields:
- String Constraints:
min_length
,max_length
,strip_whitespace
- Numeric Constraints:
min_value
,max_value
- Casting:
cast_to
class StringTest(Statica):
name: str = Field(min_length=3, max_length=5, strip_whitespace=True)
class IntTest(Statica):
num: int = Field(min_value=1, max_value=10, cast_to=int)
You can define custom error classes for type and constraint validation:
class CustomError(Exception):
pass
class CustomPayload(Statica):
constraint_error_class = CustomError
num: int = Field(min_value=1, max_value=10)
try:
payload = CustomPayload(num=0)
except CustomError as e:
print(e) # Output: "num: must be at least 1"
Or, define a BaseClass which configures the error classes globally:
from statica.core import Statica, ConstraintValidationError, TypeValidationError
class BaseClass(Statica):
constraint_error_class = ConstraintValidationError
type_error_class = TypeValidationError
class CustomPayload(BaseClass):
num: int = Field(min_value=1, max_value=10)
try:
payload = CustomPayload(num=0)
except ConstraintValidationError as e:
print(e) # Output: "num: must be at least 1"
Statica supports field aliasing, allowing you to map different field names for parsing and serialization. This is particularly useful when working with external APIs that use different naming conventions.
Use the alias
parameter to define an alternative name for both parsing and serialization:
class User(Statica):
full_name: str = Field(alias="fullName")
age: int = Field(alias="userAge")
# Parse data with aliases
data = {"fullName": "John Doe", "userAge": 30}
user = User.from_map(data)
print(user.full_name) # Output: "John Doe"
print(user.age) # Output: 30
# Serialize back with aliases
result = user.to_dict()
print(result) # Output: {"fullName": "John Doe", "userAge": 30}
You can define different aliases for parsing and serialization:
class APIModel(Statica):
user_name: str = Field(
alias_for_parsing="userName",
alias_for_serialization="username"
)
user_id: int = Field(alias_for_parsing="userId")
# Parse from camelCase API response
api_data = {"userName": "alice", "userId": 123}
model = APIModel.from_map(api_data)
# Serialize to snake_case for internal use
internal_data = model.to_dict()
print(internal_data) # Output: {"username": "alice", "user_id": 123}
When multiple alias types are defined, the priority is:
alias_for_parsing
for parsing operationsalias_for_serialization
for serialization operationsalias
as a fallback for both operations
class PriorityExample(Statica):
field_name: str = Field(
alias="generalAlias",
alias_for_parsing="parseAlias",
alias_for_serialization="serializeAlias"
)
# Uses alias_for_parsing
instance = PriorityExample.from_map({"parseAlias": "value"})
# Uses alias_for_serialization
result = instance.to_dict()
print(result) # Output: {"serializeAlias": "value"}
Statica automatically generates an __init__
method based on type annotations, ensuring that all required fields are provided during initialization.
You can automatically cast input values to the desired type:
class CastingExample(Statica):
num: int = Field(cast_to=int)
instance = CastingExample(num="42")
print(instance.num) # Output: 42
We welcome contributions to Statica! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Write tests for your changes.
- Submit a pull request.
Statica is licensed under the MIT License. See the LICENSE file for more details.
Statica was built to simplify data validation and provide a robust and simple framework for type-safe models in Python, inspired by pydantic
and dataclasses
.