katana_public_api_client.models_pydantic¶
katana_public_api_client.models_pydantic
¶
Pydantic v2 models for Katana API.
This module provides Pydantic v2 models that mirror the attrs models
in the main models/ package. These models offer:
- Strong validation: Pydantic's validation ensures data integrity
- Immutability: All models are frozen to prevent accidental modification
- Serialization: Easy conversion to/from JSON and dictionaries
- IDE support: Full type hints and autocomplete
Usage¶
Converting from API Responses¶
from katana_public_api_client import KatanaClient
from katana_public_api_client.api.product import get_all_products
from katana_public_api_client.models_pydantic import Product
from katana_public_api_client.models_pydantic.converters import (
convert_response,
)
async with KatanaClient() as client:
response = await get_all_products.asyncio_detailed(client=client)
products: list[Product] = convert_response(response, Product)
for product in products:
print(f"{product.name}: {product.id}")
Converting Individual Objects¶
from katana_public_api_client.models_pydantic import Product
from katana_public_api_client.models_pydantic.converters import (
to_pydantic,
to_attrs,
)
# Convert attrs -> pydantic
pydantic_product = Product.from_attrs(attrs_product)
# Or use the convenience function
pydantic_product = to_pydantic(attrs_product)
# Convert back to attrs for API calls
attrs_product = pydantic_product.to_attrs()
Model Layers¶
This package contains three distinct model layers:
models/(attrs): Auto-generated from OpenAPI, used by API transportmodels_pydantic/(pydantic): Auto-generated from OpenAPI, user-facingdomain/(hand-written): Custom business logic models (e.g., ItemSearchResult)
The Pydantic models here are designed for user-facing operations while the attrs models handle API communication internally.
Classes¶
KatanaPydanticBase
¶
Bases: BaseModel
Base class for all generated Pydantic models.
This base class provides: - Immutable (frozen) models for data integrity - Strict validation that forbids extra fields - Bi-directional conversion with attrs models
Example
from katana_public_api_client.models import Product as AttrsProduct
from katana_public_api_client.models_pydantic import (
Product as PydanticProduct,
)
# Convert attrs -> pydantic
attrs_product = await get_product(client, 123)
pydantic_product = PydanticProduct.from_attrs(attrs_product)
# Convert pydantic -> attrs (for API calls)
attrs_product = pydantic_product.to_attrs()
Functions¶
from_attrs(attrs_obj)
classmethod
¶
Convert an attrs model instance to this Pydantic model.
Handles: - UNSET sentinel -> None conversion - Nested object conversion (via registry lookup) - Enum value extraction - Field name mapping (type_ -> type)
Parameters:
-
attrs_obj(Any) –An instance of the corresponding attrs model.
Returns:
-
T–A new instance of this Pydantic model.
Raises:
-
ValueError–If attrs_obj is None or type doesn't match expected.
Source code in katana_public_api_client/models_pydantic/_base.py
to_attrs()
¶
Convert this Pydantic model to the corresponding attrs model.
Handles: - None -> UNSET conversion (where appropriate based on attrs field types) - Nested object conversion (via registry lookup) - Enum reconstruction from values - Field name mapping (type -> type_)
Returns:
-
Any–An instance of the corresponding attrs model.
Raises:
-
RuntimeError–If no attrs model is registered for this class.
Source code in katana_public_api_client/models_pydantic/_base.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | |
Functions¶
get_attrs_class(pydantic_class)
¶
Get the attrs class for a given Pydantic class.
Parameters:
-
pydantic_class(type[KatanaPydanticBase]) –A Pydantic model class.
Returns:
-
type | None–The corresponding attrs model class, or None if not registered.
Source code in katana_public_api_client/models_pydantic/_registry.py
get_attrs_class_by_name(name)
¶
get_pydantic_class(attrs_class)
¶
Get the Pydantic class for a given attrs class.
Parameters:
-
attrs_class(type) –An attrs model class.
Returns:
-
type[KatanaPydanticBase] | None–The corresponding Pydantic model class, or None if not registered.
Source code in katana_public_api_client/models_pydantic/_registry.py
get_pydantic_class_by_name(name)
¶
Get a Pydantic class by its name.
Parameters:
-
name(str) –The class name to look up.
Returns:
-
type[KatanaPydanticBase] | None–The Pydantic model class, or None if not found.
Source code in katana_public_api_client/models_pydantic/_registry.py
get_registration_stats()
¶
Get statistics about the current registry state.
Returns:
Source code in katana_public_api_client/models_pydantic/_registry.py
is_registered(model_class)
¶
Check if a model class is registered (either attrs or pydantic).
Parameters:
-
model_class(type) –A model class to check.
Returns:
-
bool–True if the class is registered in either direction.
Source code in katana_public_api_client/models_pydantic/_registry.py
list_registered_models()
¶
List all registered model pairs.
Returns:
Source code in katana_public_api_client/models_pydantic/_registry.py
register(attrs_class, pydantic_class)
¶
Register a mapping between an attrs class and a Pydantic class.
This function should be called for each model pair after generation. It enables the from_attrs() and to_attrs() conversion methods to work.
Parameters:
-
attrs_class(type) –The attrs model class (from models/).
-
pydantic_class(type[KatanaPydanticBase]) –The corresponding Pydantic model class.
Raises:
-
TypeError–If attrs_class is not an attrs class or pydantic_class is not a subclass of KatanaPydanticBase.
-
ValueError–If the classes are already registered with different mappings.