"""models""" from collections.abc import Iterator from typing import Any from pydantic import BaseModel, ConfigDict, field_validator class SortedSet(set[Any]): def __iter__(self) -> Iterator[Any]: return iter( sorted(super().__iter__(), key=lambda x: getattr(x, "value", str(x))) ) class SortedFrozenSet(frozenset[Any]): def __iter__(self) -> Iterator[Any]: return iter( sorted(super().__iter__(), key=lambda x: getattr(x, "value", str(x))) ) class Model(BaseModel): model_config = ConfigDict(frozen=True) @field_validator("*", mode="before") @classmethod def apply_normalization(cls, value: Any) -> Any: if isinstance(value, str): return value.strip() or None return value @field_validator("*", mode="after") @classmethod def sort_sets(cls, value: Any) -> Any: if isinstance(value, (SortedSet, SortedFrozenSet)): return value if isinstance(value, frozenset): return SortedFrozenSet(value) if isinstance(value, set): return SortedSet(value) return value