Skip to content

Python V5 #46851

Draft
dibahlfi wants to merge 4 commits into
mainfrom
users/dibahl/python-sdk-with-rust-driver
Draft

Python V5 #46851
dibahlfi wants to merge 4 commits into
mainfrom
users/dibahl/python-sdk-with-rust-driver

Conversation

@dibahlfi
Copy link
Copy Markdown
Member

Description

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@dibahlfi dibahlfi requested a review from a team as a code owner May 13, 2026 00:29
Copilot AI review requested due to automatic review settings May 13, 2026 00:29
@dibahlfi dibahlfi marked this pull request as draft May 13, 2026 00:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces an internal “backend dispatch” layer for azure-cosmos (sync + async) to allow selecting between the existing core-Python implementation and an opt-in Rust/PyO3-backed path, including per-request backend tagging in the User-Agent and a unit-test suite to pin down the wiring/selection rules.

Changes:

  • Add sync/async backend abstractions, constants, and factories with selection precedence _backend kwarg > COSMOS_BACKEND env var > default core-python.
  • Wire backend selection into CosmosClient/async client construction, expose backend instances on client_connection, and add create_item dispatch + per-request backend stamping.
  • Extend CosmosUserAgentPolicy to append backend=<name> and add fast unit tests (including an import-guard) to verify wiring and behavior.

Reviewed changes

Copilot reviewed 15 out of 17 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
sdk/cosmos/azure-cosmos/tests/test_backend_wiring_unit.py New unit tests covering import-guard, backend selection precedence/validation, container dispatch behavior, and UA stamping.
sdk/cosmos/azure-cosmos/azure/cosmos/cosmos_client.py Selects/initializes sync backend(s) at client construction and attaches them to client_connection.
sdk/cosmos/azure-cosmos/azure/cosmos/container.py Adds sync create_item dispatch and stamps the chosen backend into per-request options.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client.py Selects/initializes async backend(s) at client construction and attaches them to client_connection.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py Adds async create_item dispatch and stamps the chosen backend into per-request options.
sdk/cosmos/azure-cosmos/azure/cosmos/_user_agent_policy.py Appends a backend=<name> token to the User-Agent based on per-request options.
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/base.py Defines the sync backend ABC and shared immutable request/response dataclasses.
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/constants.py Centralizes backend names, env var, and per-request option key.
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/factory.py Implements backend name resolution + sync backend instance factory.
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/core_python.py Sync “core-python” backend stub (returns None to fall through to existing path).
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/rust.py Sync Rust backend stub with guarded PyO3 import and loud NotImplementedError.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_backend/base.py Defines the async backend ABC and re-exports shared dataclasses.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_backend/factory.py Async backend factory mapping resolved backend name to async backend instance.
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_backend/core_python.py Async “core-python” backend stub (returns None to fall through).
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_backend/rust.py Async Rust backend stub with guarded PyO3 import and loud NotImplementedError.
sdk/cosmos/azure-cosmos/azure/cosmos/_backend/init.py Backend package marker/export surface (no diff shown).
sdk/cosmos/azure-cosmos/azure/cosmos/aio/_backend/init.py Async backend package marker/export surface (no diff shown).

Comment on lines +242 to +252
# Pick which backends this client will hold. Two attributes, named
# after the two backend types:
# - _core_python_backend: always present. The default, and the
# always-available path for any request whose kwargs the Rust
# backend doesn't support yet.
# - _rust_backend: present only when Rust is selected as default.
# Its presence (vs None) is the "Rust is default" signal — no
# extra attribute needed.
# Precedence for selection: kwarg `_backend=` > COSMOS_BACKEND env
# var > "core-python".
backend_choice = kwargs.pop("_backend", None)
self._rust_backend: Optional[RustBackend] = (
chosen if isinstance(chosen, RustBackend) else None
)
logging.getLogger(__name__).info(
self._rust_backend: Optional[AsyncRustBackend] = (
chosen if isinstance(chosen, AsyncRustBackend) else None
)
logging.getLogger(__name__).info(
backend = core_python_backend
if backend is not None:
# Stamp the backend that actually handled this call so the
# user-agent policy can append `; backend=<name>` per request.
backend = core_python_backend
if backend is not None:
# Stamp the backend that actually handled this call so the
# user-agent policy can append `; backend=<name>` per request.
Comment on lines +30 to +37
# Compose any per-request user-agent suffixes. Two sources today:
# 1. Cosmos feature flags derived from the global endpoint manager
# (existing behavior — circuit breaker, PPAF, etc.).
# 2. The backend that handled this request ("core-python" or "rust"),
# stamped per-request from the dispatch site so server-side
# logs reflect the path the call actually took even when a
# Rust-default client falls back to core-python for a single call.
suffix_parts = []
Comment on lines +272 to +277
with pytest.raises(Exception): # FrozenInstanceError
setattr(p, "body_bytes", b"different")

r = BackendResponse(status_code=201)
with pytest.raises(Exception):
setattr(r, "status_code", 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants