Skip to content

Add Generic TypeVar Support to SQLAlchemy Adapters#62

Merged
HosseinNejatiJavaremi merged 1 commit intoSyntaxArc:masterfrom
younesious:feat/61-typing-sqlalchemy-adapter
Aug 10, 2025
Merged

Add Generic TypeVar Support to SQLAlchemy Adapters#62
HosseinNejatiJavaremi merged 1 commit intoSyntaxArc:masterfrom
younesious:feat/61-typing-sqlalchemy-adapter

Conversation

@younesious
Copy link
Copy Markdown

Add Generic TypeVar Support to SQLAlchemy Adapters

Description

This PR improves type safety in SQLAlchemy adapters by implementing generic TypeVar constraints, eliminating the need for manual type casting when working with specific entity types.

Changes

  • ✅ Added T = TypeVar('T', bound=BaseEntity) generic type variable
  • ✅ Updated method signatures for better type preservation:
    • create(entity: T) -> T | None
    • bulk_create(entities: list[T]) -> list[T] | None
    • get_by_uuid(entity_type: type[T], entity_uuid: UUID) -> T | None
  • ✅ Enhanced docstrings to reflect type preservation behavior
  • ✅ Applied changes to both sync (BaseSQLAlchemyAdapter) and async (AsyncBaseSQLAlchemyAdapter) adapters

Benefits

  • Eliminates Type Casting: No more cast() calls or type assertions
  • Better IDE Support: Full IntelliSense and autocomplete for specific entity attributes
  • Type Safety: Compile-time type checking prevents attribute access errors
  • Backward Compatible: No breaking changes to existing code

Before vs After

Before (requires casting):

entity = ChannelFrequencyCappingEntity(...)
created = cast(ChannelFrequencyCappingEntity, await adapter.create(entity))
uid = created.channel_frequency_capping_uid  # Manual cast required

After (type-safe):

entity = ChannelFrequencyCappingEntity(...)
created = await adapter.create(entity)  # Type: ChannelFrequencyCappingEntity | None
if created is None:
    raise RuntimeError("Creation failed")
uid = created.channel_frequency_capping_uid  # ✅ Type-safe!

Related Issues

Closes #61

@HosseinNejatiJavaremi HosseinNejatiJavaremi merged commit 61c1c85 into SyntaxArc:master Aug 10, 2025
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.

Improve Type Safety in SQLAlchemy Adapters with Generic TypeVar

2 participants