Skip to content

Add rule: configure Python client with kwargs, not ConnectionPolicy#225

Open
yumnahussain wants to merge 1 commit into
AzureCosmosDB:mainfrom
yumnahussain:sdk-python-client-kwargs-rule
Open

Add rule: configure Python client with kwargs, not ConnectionPolicy#225
yumnahussain wants to merge 1 commit into
AzureCosmosDB:mainfrom
yumnahussain:sdk-python-client-kwargs-rule

Conversation

@yumnahussain

Copy link
Copy Markdown
Contributor

Summary

Adds a new Cosmos DB best-practices skill rule: sdk-python-client-kwargs — configure the Python CosmosClient with keyword arguments, not a hand-built ConnectionPolicy.

In the azure-cosmos v4 SDK, all client configuration is passed as keyword arguments to the constructor (connection_timeout, preferred_locations, retry_total, connection_verify, ...). The client maps them onto a ConnectionPolicy internally via _build_connection_policy(). Constructing azure.cosmos.documents.ConnectionPolicy and mutating its attributes (the classic policy.RequestTimeout = 10000 anti-pattern) is a legacy v3/pydocumentdb carry-over that also steers toward deprecated knobs (retry_options, connection_retry_policy).

Provenance

Verified against the installed azure-cosmos v4.7.0 source (cosmos_client.py _build_connection_policy) and cross-checked with SDK CHANGELOG history (connection_timeout became canonical in 4.4.0b2). SSLConfiguration / ProxyConfiguration objects are intentionally not discouraged — they remain the required surface for those settings.

Changes

  • New rule skills/cosmosdb-best-practices/rules/sdk-python-client-kwargs.md
  • Regenerated AGENTS.md (npm run build)

Note

A companion verifier test (TestClientConfigViaKwargs in check_source.py) that enforces this rule lives in the pipeline PR #222.

New skill rule sdk-python-client-kwargs. In the azure-cosmos v4 SDK all
CosmosClient configuration is passed as keyword arguments to the constructor;
the client maps them onto a ConnectionPolicy internally via
_build_connection_policy(). Hand-building a documents.ConnectionPolicy and
mutating its attributes (e.g. policy.RequestTimeout = 10000) is a legacy
v3/pydocumentdb carry-over and steers toward deprecated config knobs
(retry_options, connection_retry_policy). The rule documents the kwargs surface
with Incorrect/Correct examples and notes SSLConfiguration/ProxyConfiguration
remain the required objects for those specific settings.

Regenerated AGENTS.md (npm run build).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 14, 2026 19:45
yumnahussain added a commit to yumnahussain/cosmosdb-agent-kit that referenced this pull request Jul 14, 2026
The new skill rule and its AGENTS.md regeneration now live in a separate PR
(AzureCosmosDB#225). This PR keeps only the verifier test
TestClientConfigViaKwargs in check_source.py, which cites the rule id.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
yumnahussain added a commit to yumnahussain/cosmosdb-agent-kit that referenced this pull request Jul 14, 2026
The new skill rule and its AGENTS.md regeneration now live in a separate PR
(AzureCosmosDB#225). This PR keeps only the verifier test
TestClientConfigViaKwargs in check_source.py, which cites the rule id.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Cosmos DB best-practices skill rule intended to steer Python azure-cosmos v4 users toward configuring CosmosClient via constructor keyword arguments rather than constructing/mutating a documents.ConnectionPolicy, and regenerates the compiled AGENTS.md accordingly.

Changes:

  • Added new rule sdk-python-client-kwargs under skills/cosmosdb-best-practices/rules/.
  • Regenerated skills/cosmosdb-best-practices/AGENTS.md to include the new rule and updated section numbering/TOC.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
skills/cosmosdb-best-practices/rules/sdk-python-client-kwargs.md New rule describing the preferred Python SDK v4 client configuration surface (kwargs vs ConnectionPolicy).
skills/cosmosdb-best-practices/AGENTS.md Recompiled skill output to include the new rule content and updated TOC/section numbering.

Comment on lines +56 to +60
connection_timeout=10, # seconds (NOT the ms-based legacy request_timeout)
preferred_locations=["West US 2", "East US 2"],
retry_total=9, # azure-core throttle/retry, replaces RetryOptions
connection_verify=False, # emulator self-signed cert; True in production
enable_diagnostics_logging=True,
Comment on lines +51 to +52
# The documented v4 surface. The SDK maps these kwargs onto its internal
# ConnectionPolicy for you via _build_connection_policy().
Comment on lines +12 to +14
regions, TLS verification, diagnostics, and more. Internally the client maps those
kwargs onto a `ConnectionPolicy` via its own `_build_connection_policy()` helper, so
you never construct that object yourself.

@TheovanKraay TheovanKraay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yumnahussain, this is great, not covered by any existing rule, so no redundancy concerns. Two things before we merge, both from the Copilot pass: please flip connection_verify to True in the "Correct" example (with False only noted for the local emulator/self-signed case, ideally cross-referencing sdk-emulator-ssl), since it is an easy unsafe copy-paste as-is, and drop the references to the private _build_connection_policy() helper (just say the SDK maps the kwargs internally). Once those are in, this is good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants