Skip to content

Commit 7f94bca

Browse files
authored
update compatibility with sqlalchemy 2 (#1123)
2 parents 88cec96 + a3dcc0d commit 7f94bca

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Version 3.0.2
33

44
Unreleased
55

6+
- Update compatibility with SQLAlchemy 2. :issue:`1122`
7+
68

79
Version 3.0.1
810
-------------

src/flask_sqlalchemy/extension.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -978,8 +978,11 @@ def __getattr__(self, name: str) -> t.Any:
978978
if name == "event":
979979
return sa.event
980980

981+
if name.startswith("_"):
982+
raise AttributeError(name)
983+
981984
for mod in (sa, sa.orm):
982-
if name in mod.__all__:
985+
if hasattr(mod, name):
983986
return getattr(mod, name)
984987

985988
raise AttributeError(name)

tests/test_engine.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_sqlite_relative_path(app: Flask) -> None:
9292
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"
9393
db = SQLAlchemy(app)
9494
db.create_all()
95-
assert isinstance(db.engine.pool, sa.pool.NullPool)
95+
assert not isinstance(db.engine.pool, sa.pool.StaticPool)
9696
db_path = db.engine.url.database
9797
assert db_path.startswith(app.instance_path) # type: ignore[union-attr]
9898
assert os.path.exists(db_path) # type: ignore[arg-type]

tests/test_legacy_query.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
from __future__ import annotations
22

33
import typing as t
4+
import warnings
45

56
import pytest
67
import sqlalchemy as sa
8+
import sqlalchemy.exc
79
from flask import Flask
810
from werkzeug.exceptions import NotFound
911

1012
from flask_sqlalchemy import SQLAlchemy
1113
from flask_sqlalchemy.query import Query
1214

1315

16+
@pytest.fixture(autouse=True)
17+
def ignore_query_warning() -> t.Generator[None, None, None]:
18+
if hasattr(sa.exc, "LegacyAPIWarning"):
19+
with warnings.catch_warnings():
20+
exc = sa.exc.LegacyAPIWarning # type: ignore[attr-defined]
21+
warnings.simplefilter("ignore", exc)
22+
yield
23+
else:
24+
yield
25+
26+
1427
@pytest.mark.usefixtures("app_ctx")
1528
def test_get_or_404(db: SQLAlchemy, Todo: t.Any) -> None:
1629
item = Todo()

tests/test_model_name.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class Duck(db.Model):
154154

155155
class IdMixin:
156156
@sa.orm.declared_attr
157-
def id(cls) -> sa.Column[sa.Integer]: # noqa: B902
157+
def id(cls): # type: ignore[no-untyped-def] # noqa: B902
158158
return sa.Column(sa.Integer, sa.ForeignKey(Duck.id), primary_key=True)
159159

160160
class RubberDuck(IdMixin, Duck): # type: ignore[misc]

0 commit comments

Comments
 (0)