Skip to content

Changed sqlalchemy adapter base model typing#85

Merged
HosseinNejatiJavaremi merged 1 commit intoSyntaxArc:masterfrom
miladmahmoodi:refactor/typing
Oct 8, 2025
Merged

Changed sqlalchemy adapter base model typing#85
HosseinNejatiJavaremi merged 1 commit intoSyntaxArc:masterfrom
miladmahmoodi:refactor/typing

Conversation

@miladmahmoodi
Copy link
Copy Markdown
Contributor

Pull Request: Improve SQLAlchemy Adapter Type Safety

Description

This PR improves type safety in the SQLAlchemy base adapter by replacing generic BaseEntity type annotations with the more specific generic type variable T that is bound to BaseEntity. This change enhances type checking and provides better IDE support while maintaining backward compatibility.

Changes Made

  • Enhanced Type Safety: Replaced BaseEntity with T (TypeVar bound to BaseEntity) in method signatures across both sync and async SQLAlchemy adapters
  • Improved Generic Type Support: Updated method signatures in BaseSQLAlchemyAdapter and AsyncBaseSQLAlchemyAdapter to use the generic type variable T
  • Better IDE Support: Enhanced IntelliSense and type checking capabilities for developers using these adapters

Specific Method Updates

The following methods now use proper generic typing:

  • _apply_sorting() in SQLAlchemySortMixin
  • execute_search_query() in both sync and async adapters
  • delete() and bulk_delete() methods
  • Return types for search operations now properly typed as tuple[list[T], int]

Type of change

  • Refactoring (no functional changes, no API changes)
  • This change requires a documentation update

How Has This Been Tested?

  • Type Checking: Verified MyPy compliance for the modified SQLAlchemy adapter
  • Existing Tests: All existing Behave tests continue to pass, ensuring no functional regression
  • IDE Verification: Confirmed improved type hints and IntelliSense support

The changes are purely type-related and maintain full backward compatibility. No runtime behavior is modified.

Checklist:

  • My code follows the style guidelines of this project (Python 3.13 type hints with | unions)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas (existing docstrings maintained)
  • My changes generate no new warnings (type-only changes)
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules (N/A)
  • I have checked my code and corrected any misspellings

Additional context

This refactoring aligns with the project's commitment to modern Python 3.13 typing practices and follows the established patterns in the codebase. The use of TypeVar bound to BaseEntity provides:

  1. Better Type Safety: Ensures methods return the same entity type that was passed in
  2. Enhanced Developer Experience: Improved autocomplete and type checking in IDEs
  3. Consistency: Aligns with the existing generic type patterns used throughout the ArchiPy codebase
  4. Future-Proofing: Establishes a foundation for more advanced generic typing patterns

The changes maintain full compatibility with existing code while providing enhanced type safety for future development.

Technical Details

Before (using BaseEntity):

def execute_search_query(
    self,
    entity: type[BaseEntity],
    query: Select,
    pagination: PaginationDTO | None = None,
    sort_info: SortDTO | None = None,
    has_multiple_entities: bool = False,
) -> tuple[list[BaseEntity], int]:

After (using generic T):

def execute_search_query(
    self,
    entity: type[T],
    query: Select,
    pagination: PaginationDTO | None = None,
    sort_info: SortDTO | None = None,
    has_multiple_entities: bool = False,
) -> tuple[list[T], int]:

This change ensures that if you pass in User as the entity type, the return type will be tuple[list[User], int] instead of the less specific tuple[list[BaseEntity], int].

Files Modified

  • archipy/adapters/base/sqlalchemy/adapters.py - Updated type annotations for better type

@HosseinNejatiJavaremi HosseinNejatiJavaremi merged commit 4c22e63 into SyntaxArc:master Oct 8, 2025
4 checks passed
HosseinNejatiJavaremi added a commit that referenced this pull request Oct 12, 2025
- Add comprehensive dependency updates (Pydantic, FastAPI, Ruff, etc.)
- Document test environment Docker image updates
- Include SQLAlchemy typing improvements from PR #85
- Add Temporal custom workflow ID support from PR #87
- Document CI/CD infrastructure updates from PR #88
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.

2 participants