From 57d9fbd260df9ad8683a83a40baaadedf20a69af Mon Sep 17 00:00:00 2001 From: Xravity <108474167+xravitycapital@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:22:11 -0800 Subject: [PATCH] Support SQLAlchemy 2.0 alongside 1.4 Widen the pin to sqlalchemy>=1.4.53,<2.1, import declarative_base from sqlalchemy.orm, set __allow_unmapped__ on Base so existing Column annotations keep working on 2.0, annotate the creator_id/creator declared_attrs with Mapped[...], and run the tests on 2.0 in CI. Closes #205 Co-Authored-By: Claude Opus 5.5 --- .github/workflows/cicd.yml | 3 +++ liminal/orm/base.py | 12 ++++++++++-- liminal/orm/base_model.py | 6 +++--- liminal/orm/base_results_model.py | 6 +++--- pyproject.toml | 2 +- uv.lock | 4 ++-- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index c89905b..1b2d57c 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -91,6 +91,9 @@ jobs: - name: Run tests run: uv run --locked --python "${{ matrix.python-version }}" pytest liminal/ + - name: Run tests on SQLAlchemy 2.0 + run: uv run --locked --python "${{ matrix.python-version }}" --with "sqlalchemy>=2.0,<2.1" pytest liminal/ + - name: Download wheel uses: actions/download-artifact@v8 with: diff --git a/liminal/orm/base.py b/liminal/orm/base.py index b941731..0e5048b 100644 --- a/liminal/orm/base.py +++ b/liminal/orm/base.py @@ -1,5 +1,5 @@ from sqlalchemy import MetaData -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import declarative_base # This is necessary for SQLAlchemy, all DBmodels inherit from Base, either directly or indirectly. # apply a naming convention to the metadata instance (for use in alembic revisions) @@ -12,5 +12,13 @@ "pk": "pk_%(table_name)s", } + +class _BaseMixin: + # Liminal models annotate columns as `name: SqlColumn = Column(...)`, including generated + # model files. SQLAlchemy 2.0 rejects non-`Mapped[]` annotations on mapped attributes unless + # this flag is set; SQLAlchemy 1.4 ignores it. Keeps existing user models working on both. + __allow_unmapped__ = True + + meta = MetaData(naming_convention=convention) -Base = declarative_base(metadata=meta) +Base = declarative_base(metadata=meta, cls=_BaseMixin) diff --git a/liminal/orm/base_model.py b/liminal/orm/base_model.py index 10937ca..320b8db 100644 --- a/liminal/orm/base_model.py +++ b/liminal/orm/base_model.py @@ -8,7 +8,7 @@ import pandas as pd # type: ignore from sqlalchemy import DATETIME, Boolean, ForeignKey, String from sqlalchemy import Column as SqlColumn -from sqlalchemy.orm import Query, RelationshipProperty, Session, relationship +from sqlalchemy.orm import Mapped, Query, Session, relationship from sqlalchemy.orm.decl_api import declared_attr from liminal.base.base_dropdown import BaseDropdown @@ -156,13 +156,13 @@ def __init_subclass__(cls, **kwargs: Any): cls._existing_schema_prefixes.append(cls.__schema_properties__.prefix.lower()) @declared_attr - def creator_id(cls) -> SqlColumn: + def creator_id(cls) -> Mapped[str | None]: return SqlColumn( "creator_id$", String, ForeignKey("user$raw.id"), nullable=True ) @declared_attr - def creator(cls) -> RelationshipProperty: + def creator(cls) -> Mapped[Any]: return relationship("User", foreign_keys=[cls.creator_id]) id = SqlColumn("id", String, nullable=True, primary_key=True) diff --git a/liminal/orm/base_results_model.py b/liminal/orm/base_results_model.py index 6a4fe80..1fc4a6e 100644 --- a/liminal/orm/base_results_model.py +++ b/liminal/orm/base_results_model.py @@ -8,7 +8,7 @@ import pandas as pd # type: ignore from sqlalchemy import DATETIME, Boolean, ForeignKey, String from sqlalchemy import Column as SqlColumn -from sqlalchemy.orm import Query, RelationshipProperty, Session, relationship +from sqlalchemy.orm import Mapped, Query, Session, relationship from sqlalchemy.orm.decl_api import declared_attr from liminal.base.base_validation_filters import BaseValidatorFilters @@ -31,13 +31,13 @@ class BaseResultsModel(Generic[T], Base): __schema_properties__: ResultsSchemaProperties @declared_attr - def creator_id(cls) -> SqlColumn: + def creator_id(cls) -> Mapped[str | None]: return SqlColumn( "creator_id$", String, ForeignKey("user$raw.id"), nullable=True ) @declared_attr - def creator(cls) -> RelationshipProperty: + def creator(cls) -> Mapped[Any]: return relationship("User", foreign_keys=[cls.creator_id]) id = SqlColumn("id", String, nullable=True, primary_key=True) diff --git a/pyproject.toml b/pyproject.toml index 749e53f..095dc54 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ dependencies = [ "pandas>=2.2.3,<4; python_version == '3.13'", "pydantic>=2,<=2.13", "rich>=13.9.2,<14", - "sqlalchemy>=1.4.53,<2", + "sqlalchemy>=1.4.53,<2.1", "tenacity>=8,<10", "typer>=0.12.5,<0.13", "psycopg2-binary>=2.9.10,<3", diff --git a/uv.lock b/uv.lock index 91f5298..c36ca28 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.10, <3.14" resolution-markers = [ "python_full_version >= '3.13'", @@ -692,7 +692,7 @@ requires-dist = [ { name = "psycopg2-binary", specifier = ">=2.9.10,<3" }, { name = "pydantic", specifier = ">=2,<=2.13" }, { name = "rich", specifier = ">=13.9.2,<14" }, - { name = "sqlalchemy", specifier = ">=1.4.53,<2" }, + { name = "sqlalchemy", specifier = ">=1.4.53,<2.1" }, { name = "tenacity", specifier = ">=8,<10" }, { name = "typer", specifier = ">=0.12.5,<0.13" }, ]