Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DN-37738: investigate downstream CI failure due to typing #3

Merged
merged 5 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/changes/DM-367738.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Make compatible with SQLAlchemy 2.0.

This retains compatibility with SQLAlchemy 1.4.x.
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ strict_equality = True
warn_unreachable = True
warn_unused_ignores = True

[mypy-lsst.daf.relation.tests]
# A type: ignore in this module is unnecessary for SQLALchemy < 2 and necessary
# for SQLalchemy >= 2, so we need this override as long as we're trying to
# support both.
warn_unused_ignores = False

# version.py is added by scons and may not exist when we run mypy.
[mypy-lsst.daf.relation.version]
ignore_missing_imports = True
18 changes: 12 additions & 6 deletions python/lsst/daf/relation/sql/_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

@dataclasses.dataclass(repr=False, eq=False, kw_only=True)
class Engine(
GenericConcreteEngine[Callable[..., sqlalchemy.sql.ColumnElement]],
GenericConcreteEngine[Callable[..., sqlalchemy.sql.ColumnElement[Any]]],
Generic[_L],
):
"""A concrete engine class for relations backed by a SQL database.
Expand Down Expand Up @@ -192,7 +192,9 @@ def get_join_identity_payload(self) -> Payload[_L]:

def get_doomed_payload(self, columns: Set[ColumnTag]) -> Payload[_L]:
# Docstring inherited.
select_columns = [sqlalchemy.sql.literal(None).label(tag.qualified_name) for tag in columns]
select_columns: list[sqlalchemy.sql.ColumnElement] = [
sqlalchemy.sql.literal(None).label(tag.qualified_name) for tag in columns
]
self.handle_empty_columns(select_columns)
subquery = sqlalchemy.sql.select(*select_columns).subquery()
return Payload(
Expand Down Expand Up @@ -443,7 +445,7 @@ def select_items(

This method must be overridden to support a custom logical columns.
"""
select_columns = [
select_columns: list[sqlalchemy.sql.ColumnElement] = [
cast(sqlalchemy.sql.ColumnElement, logical_column).label(tag.qualified_name)
for tag, logical_column in items
]
Expand Down Expand Up @@ -532,6 +534,7 @@ def _select_to_executable(
`to_payload` for all other relation types.
"""
columns_available: Mapping[ColumnTag, _L] | None = None
executable: sqlalchemy.sql.Select | sqlalchemy.sql.CompoundSelect

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we only supporting python 3.10 now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, daf_relation has been 3.10-only from its inception.

match select.skip_to:
case BinaryOperationRelation(operation=Chain(), lhs=lhs, rhs=rhs):
lhs_executable = self._select_to_executable(cast(Select, lhs), extra_columns)
Expand Down Expand Up @@ -614,7 +617,10 @@ def to_payload(self, relation: Relation) -> Payload[_L]:
on_terms: list[sqlalchemy.sql.ColumnElement] = []
if common_columns:
on_terms.extend(
lhs_payload.columns_available[tag] == rhs_payload.columns_available[tag]
cast(
sqlalchemy.sql.ColumnElement,
lhs_payload.columns_available[tag] == rhs_payload.columns_available[tag],
)
for tag in common_columns
)
columns_available = {**lhs_payload.columns_available, **rhs_payload.columns_available}
Expand Down Expand Up @@ -678,7 +684,7 @@ def convert_column_expression(
case ColumnFunction(name=name, args=args):
sql_args = [self.convert_column_expression(arg, columns_available) for arg in args]
if (function := self.get_function(name)) is not None:
return function(*sql_args)
return cast(_L, function(*sql_args))
return getattr(sql_args[0], name)(*sql_args[1:])
raise AssertionError(
f"matches should be exhaustive and all branches should return; got {expression!r}."
Expand All @@ -705,7 +711,7 @@ def convert_column_literal(self, value: Any) -> _L:
--------
:ref:`lsst.daf.relation-sql-logical-columns`
"""
return sqlalchemy.sql.literal(value)
return cast(_L, sqlalchemy.sql.literal(value))

def expect_column_scalar(self, logical_column: _L) -> sqlalchemy.sql.ColumnElement:
"""Convert a logical column value to a SQLAlchemy expression.
Expand Down
3 changes: 2 additions & 1 deletion python/lsst/daf/relation/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from sqlalchemy.dialects.sqlite.pysqlite import dialect as sql_dialect
except ImportError:

def sql_dialect() -> Any:
# MyPy doesn't like this trick.
def sql_dialect() -> Any: # type: ignore
raise unittest.SkipTest("sqlalchemy SQLite dialect not available")


Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lsst-utils
sqlalchemy >= 1.3
sqlalchemy >= 1.4