Skip to content

Commit

Permalink
Merge pull request #1161 from roj516/roj516/fix_avoid_mutations
Browse files Browse the repository at this point in the history
fix: avoid mutating caller's connect args (don't accumulate user_agents)
  • Loading branch information
Mause authored Nov 24, 2024
2 parents f9d5911 + eca1dba commit 174df9e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion duckdb_engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 5 additions & 3 deletions duckdb_engine/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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(
Expand Down

0 comments on commit 174df9e

Please sign in to comment.