Rust Bridge Internals¶
These modules are private implementation details. The public runtime now uses Rust-owned database and table handles exposed through ormdantic._ormdantic; the remaining bridge modules are compatibility helpers while the Python facade stays small.
Pydantic Introspection¶
ormdantic._introspect.FieldMetadata
dataclass
¶
FieldMetadata(name, info)
Small compatibility wrapper around a Pydantic v2 field.
constraint
¶
constraint(name)
Return a Pydantic field constraint by name when present.
Source code in ormdantic/_introspect.py
def constraint(self, name: str) -> Any:
"""Return a Pydantic field constraint by name when present."""
if hasattr(self.info, name):
value = getattr(self.info, name)
if value is not None:
return value
for metadata in self.info.metadata:
if hasattr(metadata, name):
value = getattr(metadata, name)
if value is not None:
return value
return None
ormdantic._introspect.model_fields
¶
model_fields(model)
Return Ormdantic field metadata for a Pydantic model.
Source code in ormdantic/_introspect.py
def model_fields(model: Type[ModelType]) -> dict[str, FieldMetadata]:
"""Return Ormdantic field metadata for a Pydantic model."""
return {
name: FieldMetadata(name=name, info=field)
for name, field in model.model_fields.items()
}
ormdantic._introspect.model_field
¶
model_field(model, name)
Return field metadata for one field on a Pydantic model.
Source code in ormdantic/_introspect.py
def model_field(model: Type[ModelType], name: str) -> FieldMetadata:
"""Return field metadata for one field on a Pydantic model."""
return FieldMetadata(name=name, info=model.model_fields[name])
ormdantic._introspect.annotation_allows_none
¶
annotation_allows_none(annotation)
Return whether an annotation accepts None.
Source code in ormdantic/_introspect.py
def annotation_allows_none(annotation: Any) -> bool:
"""Return whether an annotation accepts `None`."""
if annotation is NoneType:
return True
return NoneType in get_args(annotation)
ormdantic._introspect.is_union_annotation
¶
is_union_annotation(annotation)
Return whether an annotation is a PEP 604 or typing union.
Source code in ormdantic/_introspect.py
def is_union_annotation(annotation: Any) -> bool:
"""Return whether an annotation is a PEP 604 or typing union."""
return get_origin(annotation) in {UnionType, Union}
ormdantic._introspect.is_list_annotation
¶
is_list_annotation(annotation)
Return whether an annotation is a concrete list annotation.
Source code in ormdantic/_introspect.py
def is_list_annotation(annotation: Any) -> bool:
"""Return whether an annotation is a concrete list annotation."""
return get_origin(annotation) is list
ormdantic._introspect.contains_list_annotation
¶
contains_list_annotation(annotation)
Return whether an annotation contains a list at any nesting level.
Source code in ormdantic/_introspect.py
def contains_list_annotation(annotation: Any) -> bool:
"""Return whether an annotation contains a list at any nesting level."""
if is_list_annotation(annotation):
return True
return any(contains_list_annotation(arg) for arg in get_args(annotation))
ormdantic._introspect.is_dict_annotation
¶
is_dict_annotation(annotation)
Return whether an annotation is a dictionary.
Source code in ormdantic/_introspect.py
def is_dict_annotation(annotation: Any) -> bool:
"""Return whether an annotation is a dictionary."""
return get_origin(annotation) is dict or annotation is dict
ormdantic._introspect.first_model_arg
¶
first_model_arg(annotation, table_models)
Return the first registered table model found in an annotation.
Source code in ormdantic/_introspect.py
def first_model_arg(
annotation: Any, table_models: set[type[BaseModel]]
) -> type[BaseModel] | None:
"""Return the first registered table model found in an annotation."""
try:
if annotation in table_models:
return annotation
except TypeError:
pass
for arg in get_args(annotation):
try:
if arg in table_models:
return arg
except TypeError:
pass
if nested := first_model_arg(arg, table_models):
return nested
return None
ormdantic._introspect.rebuild_model
¶
rebuild_model(model)
Rebuild a Pydantic model after forward references are available.
Source code in ormdantic/_introspect.py
def rebuild_model(model: Type[ModelType]) -> None:
"""Rebuild a Pydantic model after forward references are available."""
model.model_rebuild()
Schema Helpers¶
ormdantic.schema.validate_table_map
¶
validate_table_map(table_map)
Validate current Python table metadata through Rust when available.
Source code in ormdantic/schema.py
def validate_table_map(table_map: Map) -> int | None:
"""Validate current Python table metadata through Rust when available."""
if _ormdantic is None or not hasattr(_ormdantic, "validate_schema_tables"):
return None
tables = [
(table.tablename, table.pk, list(table.columns))
for table in table_map.name_to_data.values()
]
return int(_ormdantic.validate_schema_tables(tables))
ormdantic.schema.compile_create_table_sql
¶
compile_create_table_sql(table_map, tablename, dialect)
Compile create-table DDL statements for a registered table.
Source code in ormdantic/schema.py
def compile_create_table_sql(table_map: Map, tablename: str, dialect: str) -> list[str]:
"""Compile create-table DDL statements for a registered table."""
rust = _require_schema_symbol("compile_create_table_sql")
table = table_map.name_to_data[tablename]
columns = [
column_descriptor(table_map, table, field_name, field)
for field_name, field in model_fields(table.model).items()
if field_name not in table.back_references
]
return list(
rust.compile_create_table_sql(
dialect,
table.tablename,
columns,
index_descriptors(table),
table.unique_constraints,
)
)
ormdantic.schema.compile_drop_table_sql
¶
compile_drop_table_sql(tablename, dialect)
Compile a drop-table DDL statement for a table name.
Source code in ormdantic/schema.py
def compile_drop_table_sql(tablename: str, dialect: str) -> str:
"""Compile a drop-table DDL statement for a table name."""
rust = _require_schema_symbol("compile_drop_table_sql")
return str(rust.compile_drop_table_sql(dialect, tablename))
Hydration Bridge¶
ormdantic.hydration.hydrate_flat_payload
¶
hydrate_flat_payload(
*, tablename, pk, columns, rows, is_array
)
Hydrate flat SQL rows into dict payloads.
Source code in ormdantic/hydration.py
def hydrate_flat_payload(
*,
tablename: str,
pk: str,
columns: list[str],
rows: list[tuple[Any, ...]],
is_array: bool,
) -> dict[str, Any] | list[dict[str, Any]] | None:
"""Hydrate flat SQL rows into dict payloads."""
if _ormdantic is not None:
return cast(
dict[str, Any] | list[dict[str, Any]] | None,
_ormdantic.hydrate_flat(
tablename,
pk,
columns,
[list(row) for row in rows],
is_array,
),
)
return _hydrate_flat_payload_python(
tablename=tablename,
pk=pk,
columns=columns,
rows=rows,
is_array=is_array,
)
ormdantic.hydration.hydrate_joined_payload
¶
hydrate_joined_payload(
*, columns, rows, path_pks, array_paths
)
Hydrate joined SQL rows into nested dict payloads.
Source code in ormdantic/hydration.py
def hydrate_joined_payload(
*,
columns: list[str],
rows: list[tuple[Any, ...]],
path_pks: list[tuple[str, str]],
array_paths: list[str],
) -> dict[str, Any] | None:
"""Hydrate joined SQL rows into nested dict payloads."""
if _ormdantic is None or not hasattr(_ormdantic, "hydrate_joined"):
raise RuntimeError(
"Ormdantic requires the Rust extension for joined hydration. "
"Install the package with maturin or reinstall the wheel."
)
payload = cast(
dict[str, Any],
_ormdantic.hydrate_joined(
columns,
[list(row) for row in rows],
path_pks,
array_paths,
),
)
return payload or None
ormdantic.hydration.plan_result_shape
¶
plan_result_shape(*, root_table, columns, array_paths=None)
Describe joined result aliases with Rust when available.
Source code in ormdantic/hydration.py
def plan_result_shape(
*,
root_table: str,
columns: list[str],
array_paths: list[str] | None = None,
) -> dict[str, Any]:
"""Describe joined result aliases with Rust when available."""
array_paths = array_paths or []
if _ormdantic is not None and hasattr(_ormdantic, "plan_result_shape"):
return cast(
dict[str, Any],
_ormdantic.plan_result_shape(root_table, columns, array_paths),
)
return _plan_result_shape_python(root_table, columns, array_paths)