🐛 dataclasses don't have model_dump

This commit is contained in:
Ondrej Samohel 2025-03-18 17:11:43 +01:00
parent 7f419fe3ba
commit 0c5d95d3d1
No known key found for this signature in database
GPG key ID: 02376E18990A97C6
2 changed files with 15 additions and 6 deletions

View file

@ -391,7 +391,7 @@ class Representation(Generic[T]): # noqa: PLR0904
"""
return {
trait_id: trait.model_dump()
trait_id: trait.as_dict()
for trait_id, trait in self._data.items()
if trait and trait_id
}
@ -593,10 +593,6 @@ class Representation(Generic[T]): # noqa: PLR0904
)
raise IncompatibleTraitVersionError(msg) from e
if requested_version is None:
trait_class = e.found_trait
requested_version = found_version
if found_version is None:
msg = (
f"Trait {e.found_trait.id} found with no version, "
@ -604,6 +600,10 @@ class Representation(Generic[T]): # noqa: PLR0904
)
raise IncompatibleTraitVersionError(msg) from e
if requested_version is None:
trait_class = e.found_trait
requested_version = found_version
if requested_version > found_version:
error_msg = (
f"Requested trait version {requested_version} is "

View file

@ -3,7 +3,7 @@ from __future__ import annotations
import re
from abc import ABC, abstractmethod
from dataclasses import dataclass
from dataclasses import asdict, dataclass
from typing import TYPE_CHECKING, Generic, Optional, TypeVar
if TYPE_CHECKING:
@ -79,6 +79,15 @@ class TraitBase(ABC):
"""
return re.sub(r"\.v\d+$", "", str(cls.id))
def as_dict(self) -> dict:
"""Return trait as dictionary.
Returns:
dict: Trait as dictionary.
"""
return asdict(self)
class IncompatibleTraitVersionError(Exception):
"""Incompatible trait version exception.