Repository: bids-standard/pybids (language: python)

Issue:
SQLAlchemy 1.4 migration
Apparently SQLAlchemy is undergoing a massive refactor for version 2.0, and is using 1.4 as a stepping stone. The idea seems to be break some things, let people fix them with a bunch of docs about upcoming best practices, and hopefully the next breakage won't be as painful.

SQLAlchemy has a 1.4.0b1 release that broke our `--pre` tests, which was a good heads up. For the time being, we've set our dependency to `sqlalchemy < 1.4.0.dev0`, but we should try to migrate.


Discussion / hints:
Makes sense, and I'll try to get to it soon, but I am *not* looking forward to it 😬

Fix patch:
diff --git a/bids/layout/models.py b/bids/layout/models.py
index 79bdccac..b74a70ea 100644
--- a/bids/layout/models.py
+++ b/bids/layout/models.py
@@ -10,12 +10,16 @@
 from functools import lru_cache
 from collections import UserDict
 
-from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.ext.associationproxy import association_proxy
 from sqlalchemy.orm.collections import attribute_mapped_collection
-from sqlalchemy import (Column, String, Boolean, ForeignKey, Table)
+from sqlalchemy import Column, String, Boolean, ForeignKey, Table
 from sqlalchemy.orm import reconstructor, relationship, backref, object_session
 
+try:
+    from sqlalchemy.orm import declarative_base
+except ImportError:  # sqlalchemy < 1.4
+    from sqlalchemy.ext.declarative import declarative_base
+
 from ..utils import listify
 from .writing import build_path, write_to_file
 from ..config import get_option
diff --git a/pyproject.toml b/pyproject.toml
index 20258597..b5363367 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -29,7 +29,7 @@ dependencies = [
   "nibabel >=2.1",
   "pandas >=0.23",
   "formulaic >=0.2.4, <0.6",  # Tested on 0.2.4-0.5.2
-  "sqlalchemy <1.4.0.dev0",
+  "sqlalchemy",
   "bids-validator",
   "num2words",
   "click >=8.0",
@@ -41,7 +41,7 @@ doc = [
   "sphinx >=2.2, !=5.1.0",
   "numpydoc",
   "sphinx_rtd_theme",
-  "myst_nb!=0.17.2",  # Avoids pulling in jupyter_cache 0.6.0 which relies on sqlalchemy>=1.4.0
+  "myst_nb",
   "jupytext",
 ]
 plotting = ["graphviz"]


Test patch:
diff --git a/bids/layout/tests/test_models.py b/bids/layout/tests/test_models.py
index fcd04f6a..df60f89a 100644
--- a/bids/layout/tests/test_models.py
+++ b/bids/layout/tests/test_models.py
@@ -178,7 +178,8 @@ def test_load_existing_config():
     session.commit()
 
     from sqlalchemy.orm.exc import FlushError
-    with pytest.raises(FlushError):
+    from sqlalchemy.exc import DBAPIError
+    with pytest.raises((FlushError, DBAPIError)):
         second = Config.load({"name": "dummy"})
         session.add(second)
         session.commit()
diff --git a/bids/layout/tests/test_writing.py b/bids/layout/tests/test_writing.py
index a8442ba5..8d32efd5 100644
--- a/bids/layout/tests/test_writing.py
+++ b/bids/layout/tests/test_writing.py
@@ -37,7 +37,7 @@ def writable_file(tmpdir):
 
     session.add_all(list(ents.values()) + tags + [bf])
     session.commit()
-    return bf
+    yield bf
 
 
 @pytest.fixture(scope='module')