Skip to content

Commit 737aac6

Browse files
Copilotchrisburr
andcommitted
Migrate from declarative_base to DeclarativeBase and improve typing
Co-authored-by: chrisburr <5220533+chrisburr@users.noreply.github.com>
1 parent c5f3ea7 commit 737aac6

File tree

10 files changed

+39
-20
lines changed

10 files changed

+39
-20
lines changed

diracx-db/src/diracx/db/sql/auth/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
String,
99
Uuid,
1010
)
11-
from sqlalchemy.orm import declarative_base
11+
from sqlalchemy.orm import DeclarativeBase
1212

1313
from diracx.db.sql.utils import Column, DateNowColumn, EnumColumn, NullColumn
1414

1515
USER_CODE_LENGTH = 8
1616

17-
Base = declarative_base()
17+
18+
class Base(DeclarativeBase):
19+
pass
1820

1921

2022
class FlowStatus(Enum):

diracx-db/src/diracx/db/sql/dummy/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from __future__ import annotations
44

55
from sqlalchemy import ForeignKey, Integer, String, Uuid
6-
from sqlalchemy.orm import declarative_base
6+
from sqlalchemy.orm import DeclarativeBase
77

88
from diracx.db.sql.utils import Column, DateNowColumn
99

10-
Base = declarative_base()
10+
11+
class Base(DeclarativeBase):
12+
pass
1113

1214

1315
class Owners(Base):

diracx-db/src/diracx/db/sql/job/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
String,
1010
Text,
1111
)
12-
from sqlalchemy.orm import declarative_base
12+
from sqlalchemy.orm import DeclarativeBase
1313

1414
from ..utils import Column, EnumBackedBool, NullColumn
1515

16-
JobDBBase = declarative_base()
16+
17+
class JobDBBase(DeclarativeBase):
18+
pass
1719

1820

1921
class AccountedFlagEnum(types.TypeDecorator):

diracx-db/src/diracx/db/sql/job_logging/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
from datetime import UTC, datetime
44

55
from sqlalchemy import Integer, Numeric, PrimaryKeyConstraint, String, TypeDecorator
6-
from sqlalchemy.orm import declarative_base
6+
from sqlalchemy.orm import DeclarativeBase
77

88
from ..utils import Column, DateNowColumn
99

10-
JobLoggingDBBase = declarative_base()
10+
11+
class JobLoggingDBBase(DeclarativeBase):
12+
pass
1113

1214

1315
class MagicEpochDateTime(TypeDecorator):

diracx-db/src/diracx/db/sql/pilot_agents/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
String,
99
Text,
1010
)
11-
from sqlalchemy.orm import declarative_base
11+
from sqlalchemy.orm import DeclarativeBase
1212

1313
from ..utils import Column, EnumBackedBool, NullColumn
1414

15-
PilotAgentsDBBase = declarative_base()
15+
16+
class PilotAgentsDBBase(DeclarativeBase):
17+
pass
1618

1719

1820
class PilotAgents(PilotAgentsDBBase):

diracx-db/src/diracx/db/sql/sandbox_metadata/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
String,
1010
UniqueConstraint,
1111
)
12-
from sqlalchemy.orm import declarative_base
12+
from sqlalchemy.orm import DeclarativeBase
1313

1414
from diracx.db.sql.utils import Column, DateNowColumn
1515

16-
Base = declarative_base()
16+
17+
class Base(DeclarativeBase):
18+
pass
1719

1820

1921
class SBOwners(Base):

diracx-db/src/diracx/db/sql/task_queue/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
Integer,
1010
String,
1111
)
12-
from sqlalchemy.orm import declarative_base
12+
from sqlalchemy.orm import DeclarativeBase
1313

1414
from ..utils import Column
1515

16-
TaskQueueDBBase = declarative_base()
16+
17+
class TaskQueueDBBase(DeclarativeBase):
18+
pass
1719

1820

1921
class TaskQueues(TaskQueueDBBase):

diracx-db/src/diracx/db/sql/utils/base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
from collections.abc import AsyncIterator
99
from contextvars import ContextVar
1010
from datetime import datetime
11-
from typing import Any, Self, cast
11+
from typing import Any, Self, cast, TYPE_CHECKING
1212

1313
from pydantic import TypeAdapter
1414
from sqlalchemy import DateTime, MetaData, func, select
1515
from sqlalchemy.exc import OperationalError
1616
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
1717

18+
if TYPE_CHECKING:
19+
from sqlalchemy.orm import DeclarativeBase
20+
1821
from diracx.core.exceptions import InvalidQueryError
1922
from diracx.core.extensions import select_from_extension
2023
from diracx.core.models import (
@@ -233,7 +236,7 @@ async def ping(self):
233236

234237
async def _search(
235238
self,
236-
table: Any,
239+
table: type[DeclarativeBase],
237240
parameters: list[str] | None,
238241
search: list[SearchSpec],
239242
sorts: list[SortSpec],
@@ -273,7 +276,7 @@ async def _search(
273276
]
274277

275278
async def _summary(
276-
self, table: Any, group_by: list[str], search: list[SearchSpec]
279+
self, table: type[DeclarativeBase], group_by: list[str], search: list[SearchSpec]
277280
) -> list[dict[str, str | int]]:
278281
"""Get a summary of the elements of a table."""
279282
columns = _get_columns(table.__table__, group_by)

extensions/gubbins/gubbins-db/src/gubbins/db/sql/jobs/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from diracx.db.sql.job.db import JobDBBase
1+
from diracx.db.sql.job.schema import JobDBBase
22
from diracx.db.sql.utils import Column
33
from sqlalchemy import (
44
ForeignKey,

extensions/gubbins/gubbins-db/src/gubbins/db/sql/lollygag/schema.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
# in place of the SQLAlchemy one. Have a look at them
33
from diracx.db.sql.utils import Column, DateNowColumn
44
from sqlalchemy import ForeignKey, Integer, String, Uuid
5-
from sqlalchemy.orm import declarative_base
5+
from sqlalchemy.orm import DeclarativeBase
66

7-
Base = declarative_base()
7+
8+
class Base(DeclarativeBase):
9+
pass
810

911

1012
class Owners(Base):

0 commit comments

Comments
 (0)