Skip to content

Conversation

Copy link

Copilot AI commented Aug 5, 2025

This PR migrates DiracX from the old-style SQLAlchemy declarative_base() function to the new-style DeclarativeBase class, following SQLAlchemy 2.0+ best practices and improving type safety throughout the codebase.

Key Changes

Before:

from sqlalchemy.orm import declarative_base
Base = declarative_base()

# Type hints using Any
async def _search(self, table: Any, ...):

After:

from sqlalchemy.orm import DeclarativeBase

class Base(DeclarativeBase):
    pass

# Proper type hints
async def _search(self, table: type[DeclarativeBase], ...):

Benefits

  • Enhanced Type Safety: Replaced Any type hints in _search and _summary methods with proper type[DeclarativeBase] annotations
  • Better IDE Support: Improved autocompletion and type checking for database models
  • Modern SQLAlchemy: Uses SQLAlchemy 2.0+ recommended patterns
  • Developer Experience: Type checkers (mypy/pyright) can now catch type errors at development time

Files Modified

  • 7 core schema files: Migrated all database schema base classes
  • 1 utils file: Improved type annotations for search methods
  • 2 extension files: Updated to use proper imports and DeclarativeBase

All changes are backward compatible with zero breaking changes to the existing API.

Fixes #612.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: chrisburr <5220533+chrisburr@users.noreply.github.com>
Copilot AI changed the title [WIP] Use the new-style sqlalchemy DeclarativeBase Migrate from declarative_base to DeclarativeBase and improve type safety Aug 5, 2025
Copilot AI requested a review from chrisburr August 5, 2025 08:24
@chrisburr
Copy link
Member

@copilot Pre-commit is failing:

diracx-db/src/diracx/db/sql/utils/base.py:287: error: "Iterable[NamedColumn[Any]]" has no attribute "columns"  [attr-defined]
diracx-db/src/diracx/db/sql/task_queue/db.py:150: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
diracx-db/src/diracx/db/sql/job_logging/db.py:59: error: "FromClause" has no attribute "insert"  [attr-defined]
diracx-db/src/diracx/db/sql/job/db.py:76: error: "FromClause" has no attribute "insert"  [attr-defined]
diracx-db/src/diracx/db/sql/job/db.py:92: error: "FromClause" has no attribute "insert"  [attr-defined]
diracx-db/src/diracx/db/sql/job/db.py:106: error: "FromClause" has no attribute "insert"  [attr-defined]
diracx-db/src/diracx/db/sql/job/db.py:119: error: "FromClause" has no attribute "update"  [attr-defined]
diracx-db/src/diracx/db/sql/job/db.py:270: error: "FromClause" has no attribute "insert"  [attr-defined]
diracx-routers/src/diracx/routers/factory.py:531: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
diracx-routers/src/diracx/routers/factory.py:533: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
Found 7 errors in 3 files (checked 161 source files)

You should use pixi run pre-commit --all-files to verify your changes.

Co-authored-by: chrisburr <5220533+chrisburr@users.noreply.github.com>
Copy link
Author

Copilot AI commented Aug 5, 2025

@copilot Pre-commit is failing:

...

Fixed the pre-commit type checking errors. The issues were caused by the migration to DeclarativeBase requiring more specific type annotations:

  • Added proper Table import and cast operations for __table__ attributes
  • Fixed _get_columns function parameter types
  • Used cast("Table", SomeModel.__table__) to help mypy understand table operations

All the attr-defined errors are now resolved (commit 0c4f83a).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use the new-style sqlalchemy DeclarativeBase

2 participants