ComfyUI/app/assets/services/schemas.py
Luke Mino-Altherr e3b8e512ca
Some checks failed
Python Linting / Run Ruff (push) Waiting to run
Python Linting / Run Pylint (push) Waiting to run
Build package / Build Test (3.10) (push) Has been cancelled
Build package / Build Test (3.11) (push) Has been cancelled
Build package / Build Test (3.12) (push) Has been cancelled
Build package / Build Test (3.13) (push) Has been cancelled
Build package / Build Test (3.14) (push) Has been cancelled
refactor: use explicit dataclasses instead of ORM objects in service layer
Replace dict/ORM object returns with explicit dataclasses to fix
DetachedInstanceError when accessing ORM attributes after session closes.

- Add app/assets/services/schemas.py with AssetData, AssetInfoData,
  AssetDetailResult, and RegisterAssetResult dataclasses
- Update asset_management.py and ingest.py to return dataclasses
- Update manager.py to use attribute access on dataclasses
- Fix created_new to be False in create_asset_from_hash (content exists)
- Add DependencyMissingError for better blake3 missing error handling
- Update tests to use attribute access instead of dict subscripting

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 18:39:07 -08:00

70 lines
1.7 KiB
Python

"""
Service layer data transfer objects.
These dataclasses represent the data returned by service functions,
providing explicit types instead of raw dicts or ORM objects.
"""
from dataclasses import dataclass
from datetime import datetime
@dataclass(frozen=True)
class AssetData:
"""Plain data extracted from an Asset ORM object."""
hash: str
size_bytes: int | None
mime_type: str | None
@dataclass(frozen=True)
class AssetInfoData:
"""Plain data extracted from an AssetInfo ORM object."""
id: str
name: str
user_metadata: dict | None
preview_id: str | None
created_at: datetime
updated_at: datetime
last_access_time: datetime | None
@dataclass(frozen=True)
class AssetDetailResult:
"""Result from get_asset_detail and similar operations."""
info: AssetInfoData
asset: AssetData | None
tags: list[str]
@dataclass(frozen=True)
class RegisterAssetResult:
"""Result from register_existing_asset."""
info: AssetInfoData
asset: AssetData
tags: list[str]
created: bool
def extract_info_data(info) -> AssetInfoData:
"""Extract plain data from an AssetInfo ORM object."""
return AssetInfoData(
id=info.id,
name=info.name,
user_metadata=info.user_metadata,
preview_id=info.preview_id,
created_at=info.created_at,
updated_at=info.updated_at,
last_access_time=info.last_access_time,
)
def extract_asset_data(asset) -> AssetData | None:
"""Extract plain data from an Asset ORM object."""
if asset is None:
return None
return AssetData(
hash=asset.hash,
size_bytes=asset.size_bytes,
mime_type=asset.mime_type,
)