Skip to content

Commit

Permalink
refactor: move custom ORM class __repr__ to a base class
Browse files Browse the repository at this point in the history
So that all ORM classes get depth-limited reprs automatically.
  • Loading branch information
chisholm committed Oct 28, 2024
1 parent 6710cc9 commit 6dd1f1c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/dioptra/restapi/db/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from sqlalchemy.ext.mutable import MutableDict
from sqlalchemy.orm import DeclarativeBase, MappedAsDataclass, mapped_column

from dioptra.restapi.db.models.utils import depth_limited_repr

from .custom_types import GUID, TZDateTime

intpk = Annotated[
Expand Down Expand Up @@ -79,10 +81,13 @@ def _set_sqlite_pragma(
cursor.close()


class Base(DeclarativeBase, MappedAsDataclass):
class Base(DeclarativeBase, MappedAsDataclass, repr=False):
"""The base ORM class."""

metadata = metadata_obj

def __repr__(self):
return depth_limited_repr(self)


db = SQLAlchemy(model_class=Base)
4 changes: 0 additions & 4 deletions src/dioptra/restapi/db/models/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from dioptra.restapi.db.db import bigint, intpk, text_
from dioptra.restapi.db.models.utils import depth_limited_repr

from .resources import ResourceSnapshot

Expand Down Expand Up @@ -55,6 +54,3 @@ class Queue(ResourceSnapshot):
__mapper_args__ = {
"polymorphic_identity": "queue",
}

def __repr__(self):
return depth_limited_repr(self)
4 changes: 0 additions & 4 deletions src/dioptra/restapi/db/models/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
optionalstr,
text_,
)
from dioptra.restapi.db.models.utils import depth_limited_repr

from .constants import resource_lock_types
from .locks import ResourceLock
Expand Down Expand Up @@ -355,9 +354,6 @@ def __post_init__(self) -> None:
timestamp = datetime.datetime.now(tz=datetime.timezone.utc)
self.created_on = timestamp

def __repr__(self):
return depth_limited_repr(self)


class SharedResource(db.Model): # type: ignore[name-defined]
__tablename__ = "shared_resources"
Expand Down
4 changes: 0 additions & 4 deletions src/dioptra/restapi/db/models/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from sqlalchemy.orm import Mapped, mapped_column, relationship

from dioptra.restapi.db.db import bigint, datetimetz, db, intpk, text_
from dioptra.restapi.db.models.utils import depth_limited_repr

if TYPE_CHECKING:
from .groups import Group
Expand Down Expand Up @@ -65,6 +64,3 @@ def __post_init__(self) -> None:
timestamp = datetime.datetime.now(tz=datetime.timezone.utc)
self.created_on = timestamp
self.last_modified_on = timestamp

def __repr__(self):
return depth_limited_repr(self)

0 comments on commit 6dd1f1c

Please sign in to comment.