Row-to-dataclass mapping with type coercion.
Converts raw database rows (tuples or dicts) into typed frozen dataclasses. Uses dataclass field introspection — no metaclass magic, no descriptors.
Type coercion handles the mismatch between database drivers (SQLite returns
strings for some column types) and Python dataclass annotations. Fields
annotated asint will coerce string values like "45" to 45,
and empty strings to0.
data._mapping
| Name | Type | Default | Description |
|---|---|---|---|
type
|
|
— | |
qualified_name
|
|
— | |
element_type
|
|
— | |
description
|
|
— | |
source_file
|
|
— | |
line_number
|
|
— | |
is_autodoc
|
|
— | |
autodoc_element
|
|
— | |
_autodoc_template
|
|
— | |
_autodoc_url_path
|
|
— | |
_autodoc_page_type
|
|
— | |
title
|
|
— | |
doc_content_hash
|
|
— |
Symbols on this page
Build a {field_name: target_type} map for coercible fields.
ReturnsNonefor fields that don't need coercion (complex types,
generics, etc.).
Coerce a single value to the target type, if needed.
Map a dict-like row to a frozen dataclass instance.
Only passes keys that match dataclass fields. Extra columns are silently ignored (SELECT * is…
Map a list of dict-like rows to frozen dataclass instances.
_build_coercion_map
function
def _build_coercion_map(cls: type) -> dict[str, type | None]
Build a {field_name: target_type} map for coercible fields.
ReturnsNonefor fields that don't need coercion (complex types,
generics, etc.).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type
|
— |
_coerce
function
def _coerce(value: Any, target: type | None) -> Any
Coerce a single value to the target type, if needed.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value
|
Any
|
— | |
target
|
type | None
|
— |
map_row
function
def map_row(cls: type[T], row: dict[str, Any]) -> T
Map a dict-like row to a frozen dataclass instance.
Only passes keys that match dataclass fields. Extra columns are silently ignored (SELECT * is fine even if the dataclass has fewer fields).
Values are coerced to match field annotations:int, float,
bool, and strfields handle driver type mismatches automatically.
Empty strings inint/float columns coerce to 0/0.0.
RaisesTypeErrorif required fields are missing from the row.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[T]
|
— | |
row
|
dict[str, Any]
|
— |
map_rows
function
def map_rows(cls: type[T], rows: list[dict[str, Any]]) -> list[T]
Map a list of dict-like rows to frozen dataclass instances.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
cls
|
type[T]
|
— | |
rows
|
list[dict[str, Any]]
|
— |
View source · /home/runner/work/chirp/chirp/site/../src/chirp/data/_mapping.py:1