Dev-iL commented on code in PR #55954:
URL: https://github.com/apache/airflow/pull/55954#discussion_r2382719028
##########
airflow-core/src/airflow/models/backfill.py:
##########
@@ -39,7 +38,18 @@
select,
)
from sqlalchemy.exc import IntegrityError
-from sqlalchemy.orm import relationship, validates
+
+try:
+ from sqlalchemy.orm import mapped_column
+except ImportError:
+ # fallback for SQLAlchemy < 2.0
+ def mapped_column(*args, **kwargs):
+ from sqlalchemy import Column
+
+ return Column(*args, **kwargs)
Review Comment:
I agree with Ash's comment - that was, after all, the purpose of the shim
suggested by the LLM. However, I also see your point. Just to clarify, the
alternative is:
```python
"""
Shim for dual SQLAlchemy 1.4 / 2.0 support.
# Why this works?
* On 1.4: MyPy "sees" Mapped/mapped_column via stubs, so model attributes
resolve to instance types (str, int, datetime, …) instead of Column[...].
* At runtime on 1.4: mapped_column falls back to Column, so models still
construct.
* On 2.0: Native imports are used; the shim becomes a thin pass-through.
# Eventual removal of 1.4 support:
Delete the below and replace imports with `from sqlalchemy.orm import
Mapped, mapped_column`.
"""
# ruff: noqa: F401
from __future__ import annotations
TYPE_CHECKING: bool = False
if TYPE_CHECKING:
# For type-checkers, use real 2.0 types (via sqlalchemy2-stubs on 1.4)
from sqlalchemy import Select
from sqlalchemy.orm import Mapped, Session, mapped_column
else:
# At runtime, import what exists; gracefully fall back on 1.4
try: # SA 1.4+ often exposes these names; SA 2.0 surely does
from sqlalchemy.orm import Mapped, Session, mapped_column # type:
ignore
except Exception:
from typing import Any as Mapped # runtime only; type-checkers hit
the branch above
from sqlalchemy import Column as mapped_column # maps to 1.4
Column()
try:
from sqlalchemy import Select # SA 2.0
except Exception: # 1.4
from sqlalchemy.sql import Select # type: ignore
```
Then in the models it's as simple as:
```python
from airflow.utils.sqlalchemy import Mapped, Session, mapped_column
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]