Skip to content

Commit adea2d7

Browse files
authored
feat(python/adbc_driver_manager): allow connect(profile="foo") (#4078)
Closes #4077.
1 parent 043bb22 commit adea2d7

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

python/adbc_driver_manager/adbc_driver_manager/dbapi.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,10 @@ def __eq__(self, other: Any) -> bool:
183183

184184

185185
def connect(
186-
driver: Union[str, pathlib.Path],
186+
driver: Optional[Union[str, pathlib.Path]] = None,
187187
uri: Optional[str] = None,
188188
*,
189+
profile: Optional[str] = None,
189190
entrypoint: Optional[str] = None,
190191
db_kwargs: Optional[Dict[str, Union[str, pathlib.Path]]] = None,
191192
conn_kwargs: Optional[Dict[str, str]] = None,
@@ -217,10 +218,17 @@ def connect(
217218
where the scheme happens to be the same as the driver name (so
218219
PostgreSQL works, but not SQLite, for example, as SQLite uses
219220
``file:`` URIs).
221+
222+
- If the URI begins with ``profile://``, then a connection profile
223+
will be loaded instead. See :doc:`/format/connection_profiles`.
220224
uri
221225
The "uri" parameter to the database (if applicable). This is
222226
equivalent to passing it in ``db_kwargs`` but is slightly cleaner.
223227
If given, takes precedence over any value in ``db_kwargs``.
228+
profile
229+
A connection profile to load. Loading ``profile="profile-name"`` is
230+
the same as loading the URI ``profile://profile-name``. See
231+
:doc:`/format/connection_profiles`.
224232
entrypoint
225233
The driver-specific entrypoint, if different than the default.
226234
db_kwargs
@@ -238,16 +246,22 @@ def connect(
238246
conn = None
239247

240248
db_kwargs = dict(db_kwargs or {})
241-
db_kwargs["driver"] = driver
249+
if driver:
250+
db_kwargs["driver"] = driver
242251
if uri:
243252
db_kwargs["uri"] = uri
244253
if entrypoint:
245254
db_kwargs["entrypoint"] = entrypoint
255+
if profile:
256+
db_kwargs["profile"] = profile
246257
if conn_kwargs is None:
247258
conn_kwargs = {}
248259
# N.B. treating uri = "postgresql://..." as driver = "postgresql", uri =
249260
# "..." is handled at the C driver manager layer
250261

262+
if all(k not in db_kwargs for k in ("driver", "uri", "profile")):
263+
raise TypeError("Must specify at least one of 'driver', 'uri', or 'profile'")
264+
251265
try:
252266
db = _lib.AdbcDatabase(**db_kwargs)
253267
conn = _lib.AdbcConnection(db, **conn_kwargs)

python/adbc_driver_manager/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ build-backend = "setuptools.build_meta"
4242
enable = ["cpython-freethreading"]
4343

4444
[tool.pytest.ini_options]
45+
filterwarnings = ["error"]
4546
markers = [
4647
"duckdb: tests that require DuckDB",
4748
"panicdummy: tests that require the testing-only panicdummy driver",

python/adbc_driver_manager/tests/test_dbapi.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def test_connect(tmp_path: pathlib.Path, monkeypatch) -> None:
735735
cur.execute("SELECT * FROM foo")
736736
assert cur.fetchone() == (1,)
737737

738-
monkeypatch.setenv("ADBC_DRIVER_PATH", tmp_path)
738+
monkeypatch.setenv("ADBC_DRIVER_PATH", str(tmp_path))
739739
with (tmp_path / "foobar.toml").open("w") as f:
740740
f.write(
741741
"""
@@ -747,3 +747,13 @@ def test_connect(tmp_path: pathlib.Path, monkeypatch) -> None:
747747
with pytest.raises(dbapi.ProgrammingError, match="NOT_FOUND"):
748748
with dbapi.connect("foobar://localhost:5439"):
749749
pass
750+
751+
# https://github.com/apache/arrow-adbc/issues/4077: allow profile argument
752+
# Just check that the profile gets detected and loaded (should fail)
753+
with pytest.raises(dbapi.ProgrammingError, match="NOT_FOUND.*Profile not found"):
754+
with dbapi.connect(profile="nonexistent"):
755+
pass
756+
757+
with pytest.raises(TypeError):
758+
with dbapi.connect():
759+
pass

0 commit comments

Comments
 (0)