From 8f39aca57a3f64ac02dc2766a48f74ec37201203 Mon Sep 17 00:00:00 2001 From: roj516 <143634393+roj516@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:08:12 +0100 Subject: [PATCH] fix: avoid mutating caller's connect args (don't accumulate user_agents) --- duckdb_engine/__init__.py | 3 ++- duckdb_engine/tests/test_basic.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/duckdb_engine/__init__.py b/duckdb_engine/__init__.py index 4e73397a..edd833e3 100644 --- a/duckdb_engine/__init__.py +++ b/duckdb_engine/__init__.py @@ -261,7 +261,8 @@ def type_descriptor(self, typeobj: Type[sqltypes.TypeEngine]) -> Any: # type: i def connect(self, *cargs: Any, **cparams: Any) -> "Connection": core_keys = get_core_config() preload_extensions = cparams.pop("preload_extensions", []) - config = cparams.setdefault("config", {}) + config = dict(cparams.get("config", {})) + cparams["config"] = config config.update(cparams.pop("url_config", {})) ext = {k: config.pop(k) for k in list(config) if k not in core_keys} diff --git a/duckdb_engine/tests/test_basic.py b/duckdb_engine/tests/test_basic.py index 8c3dbb80..ed6d0416 100644 --- a/duckdb_engine/tests/test_basic.py +++ b/duckdb_engine/tests/test_basic.py @@ -539,9 +539,8 @@ def test_user_agent() -> None: reason="custom_user_agent is not supported for DuckDB version < 0.9.2", ) def test_user_agent_with_custom_user_agent() -> None: - eng = create_engine( - "duckdb:///:memory:", connect_args={"config": {"custom_user_agent": "custom"}} - ) + connect_args = {"config": {"custom_user_agent": "custom"}} + eng = create_engine("duckdb:///:memory:", connect_args=connect_args) with eng.connect() as conn: res = conn.execute(text("PRAGMA USER_AGENT")) @@ -551,6 +550,9 @@ def test_user_agent_with_custom_user_agent() -> None: r"duckdb/.*(.*) python duckdb_engine/.*(sqlalchemy/.*) custom", row[0] ) + # Check that connect hasn't mutated the caller's data. + assert connect_args["config"]["custom_user_agent"] == "custom" + def test_do_ping(tmp_path: Path, caplog: LogCaptureFixture) -> None: engine = create_engine(