Skip to content

Apply Correct Typing to All ORM Models for Improved Mypy Compatibility #8

@calbrecht

Description

@calbrecht

Currently, our SQLAlchemy ORM models use legacy type declarations such as:
Python

id = Column(Integer, primary_key=True)

This pattern does not provide strong static type guarantees for tools like mypy, often forcing us to use explicit type casts (e.g., int(entity.id)) throughout the codebase. This impacts code clarity and maintainability.

Task:

Refactor all ORM models to use modern SQLAlchemy typing with Mapped and mapped_column:
Python

from sqlalchemy.orm import Mapped, mapped_column

class DomainEntity(Base):
    id: Mapped[int] = mapped_column(primary_key=True)
    # ...

Ensure all model attributes are correctly annotated for their types (e.g., str, int, float, bool, etc.).
Update imports as necessary to support new SQLAlchemy typing features.
Remove unnecessary type casts (int(...), str(...), etc.) from the codebase where possible, once proper typing is applied.
Verify with mypy that type errors are resolved for model field usage.

Benefits:

Stronger guarantees for static type checking and code correctness.
Cleaner code without redundant type casts.
Easier onboarding for contributors and maintainers.

References:

[SQLAlchemy ORM Typing Guide](https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#typing-orm-models)
[SQLAlchemy Mypy Plugin Guide](https://docs.sqlalchemy.org/en/20/orm/extensions/mypy.html)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions