haive.core.schema.utils¶
Utility functions for schema manipulation in the Haive framework.
This module provides the SchemaUtils class containing static methods for working with schemas in the Haive Schema System. It includes utilities for formatting type annotations, extracting field information, creating field definitions, and building schemas programmatically.
The utilities in this module are primarily used by the SchemaComposer and StateSchemaManager classes, but they can also be useful for custom schema manipulation tasks or when working with schemas directly.
Key capabilities include: - Type annotation formatting for readable representation of complex types - Field information extraction from Pydantic field objects - Pydantic field creation with proper metadata - Support for special types like Optional, Union, and generics - Helper functions for schema display and debug visualization
Examples
from haive.core.schema.utils import SchemaUtils from typing import List, Optional
# Format a type annotation type_str = SchemaUtils.format_type_annotation(List[Optional[str]]) print(type_str) # “List[Optional[str]]”
# Extract field info from a Pydantic model from pydantic import BaseModel, Field
- class MyModel(BaseModel):
name: str = Field(default=”default”, description=”User name”)
field_info = MyModel.model_fields[“name”] default, default_repr, desc = SchemaUtils.extract_field_info(field_info) # default = “default”, desc = “User name”
Classes¶
Utility functions for schema manipulation and formatting. |
Module Contents¶
- class haive.core.schema.utils.SchemaUtils[source]¶
Utility functions for schema manipulation and formatting.
This class provides static methods for working with schema-related tasks such as formatting type annotations, extracting field information, and building state schemas from components. The methods focus on common operations needed when programmatically working with schemas and Pydantic models in the Haive framework.
Key methods include:
format_type_annotation: Creates readable string representations of type annotations
extract_field_info: Extracts default values and descriptions from Pydantic fields
create_pydantic_field: Creates Pydantic Field objects with proper metadata
extract_model_info: Analyzes a Pydantic model to extract useful information
format_default_value: Creates string representations of default values
analyze_field_type: Determines characteristics of field types (generic, optional, etc.)
These utilities are designed to work with both Pydantic v1 and v2, handling differences in field structures and model internals. They’re used extensively throughout the Haive Schema System to provide consistent handling of types and field definitions.
- static add_field_to_schema(schema, name, field_type, default=None, description=None, shared=False, reducer=None)[source]¶
Add a field to an existing schema class.
- Parameters:
schema (type[pydantic.BaseModel]) – Existing schema class.
name (str) – Field name to add.
field_type (type) – Type of the field.
default (Any) – Default value.
description (str | None) – Optional field description.
shared (bool) – Whether the field is shared with parent.
reducer (collections.abc.Callable | None) – Optional reducer function.
- Returns:
Updated schema class with the new field.
- Return type:
type[pydantic.BaseModel]
- static build_state_schema(name, fields, shared_fields=None, reducers=None, base_class=None)[source]¶
Build a state schema from field definitions.
- Parameters:
name (str) – Name for the schema class.
fields (dict[str, tuple[type, Any]]) – Dictionary mapping field names to (type, default) tuples.
shared_fields (list[str] | None) – Optional list of fields shared with parent.
reducers (dict[str, collections.abc.Callable] | None) – Optional dictionary mapping field names to reducer functions.
base_class (type[pydantic.BaseModel] | None) – Optional base class (defaults to StateSchema).
- Returns:
A new schema class.
- Return type:
type[pydantic.BaseModel]
- static extract_field_info(field_info)[source]¶
Extract useful information from a Pydantic FieldInfo.
- static format_schema_as_python(schema_name, fields, properties=None, computed_properties=None, class_methods=None, static_methods=None, field_descriptions=None, shared_fields=None, reducer_fields=None, base_class='StateSchema')[source]¶
Format a schema definition as Python code.
Creates a string representation of a schema class with all its fields, properties, methods, and metadata.
- Parameters:
schema_name (str) – Name of the schema class.
fields (dict[str, tuple[Any, pydantic.fields.FieldInfo]]) – Dictionary of field names to (type, field_info) tuples.
properties (dict[str, Any] | None) – Optional dictionary of property names to property methods.
computed_properties (dict[str, Any] | None) – Optional dictionary of computed property definitions.
class_methods (dict[str, Any] | None) – Optional dictionary of class method names to methods.
static_methods (dict[str, Any] | None) – Optional dictionary of static method names to methods.
field_descriptions (dict[str, str] | None) – Optional dictionary of field descriptions.
shared_fields (set[str] | None) – Optional set of field names that are shared with parent.
reducer_fields (dict[str, collections.abc.Callable] | None) – Optional dictionary of fields with reducer functions.
base_class (str) – Base class name for the schema.
- Returns:
String containing the Python code representation.
- Return type: