Skip to content

Fix circular import in the Azure KMS driver - #2416

Merged
Robert Yokota (rayokota) merged 1 commit into
confluentinc:masterfrom
pylaterreur:fix/azure-kms-circular-import
Nov 12, 2026
Merged

Robert Yokota (rayokota) merged 1 commit into
confluentinc:masterfrom
pylaterreur:fix/azure-kms-circular-import

Conversation

@pylaterreur

Copy link
Copy Markdown
Contributor

What

Importing confluent_kafka.schema_registry.rules.encryption.azurekms.azure_client before azure_driver raises ImportError: cannot import name 'AzureKmsClient' from partially initialized module ... (most likely due to a circular import) on every Python version: azure_client imports the azure_driver module for its key version helpers, and azure_driver imports the AzureKmsClient class from azure_client. For example, pytest tests/schema_registry/test_azure_client.py fails at collection when run on its own. azure_driver now imports the azure_client module rather than the class, as azure_client already does for azure_driver, so the cycle resolves whichever module is imported first.

The new tests/test_imports.py imports every submodule in a fresh interpreter, skipping modules whose optional dependencies are not installed.

This is the part of #2401 that does not depend on Python 3.8.

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • azure_client can be imported before azure_driver. No API change.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • tests/test_imports.py, which fails on master when the rules dependencies (tink, Azure SDK) are installed.

References

Replaces #2401: its Python 3.8 fix is moot once #2406 drops 3.8.

Test & Review

  • Python 3.11.16 and 3.9.25 with requirements/requirements-tests-install.txt: on master, pytest tests/schema_registry/test_azure_client.py errors at collection, and the new test fails with the ImportError; with this change, both files pass (7 tests).
  • Python 3.11.16, pytest tests --ignore=tests/integration: 2544 passed, 16 skipped. The full run does not show the bug on master, because tests/schema_registry/_async/test_avro_serdes_rules.py is collected before test_azure_client.py and imports azure_driver first.
  • Python 3.15.0 and 3.15.0t: the new test passes. The Azure modules are skipped there, since tink has no cp315 wheels.
  • tools/style-format.sh on the changed files, flake8, mypy src/confluent_kafka and tools/unasync.py --check pass.

Open questions / Follow-ups

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

@confluent-cla-assistant

Copy link
Copy Markdown

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ pylaterreur
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

@rayokota
Robert Yokota (rayokota) requested a balanced review from Copilot November 9, 2026 16:34
@rayokota

Copy link
Copy Markdown
Member

/sem-approve

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The smoke test’s ImportError handling skips nonexistent-symbol imports from installed dependencies, allowing broken dependency API imports to pass undetected.

1 open issue
What changed in this PR

This PR fixes the Azure KMS circular import so azure_client can be imported before azure_driver, without changing the client API.

Changes:

  • Defers access to AzureKmsClient through a module import.
  • Adds a subprocess-based smoke test for package imports.
  • Documents the import-order fix in the changelog.
File Description
tests/​test_imports.py Adds import checks in a fresh interpreter, with optional-dependency skipping.
src/​confluent_kafka/​schema_registry/​rules/​encryption/​azurekms/​azure_driver.py Uses a module-qualified client reference to resolve the circular import.
CHANGELOG.md Records the Azure KMS import fix.

Review effort: Balanced

Comment thread tests/test_imports.py Resolved
@rayokota Robert Yokota (rayokota) added the component:schema-registry Any schema registry related isues rather than kafka isolated ones label Nov 9, 2026
azure_client imports the azure_driver module for its key version
helpers, and azure_driver imported the AzureKmsClient class from
azure_client. Importing azure_client before azure_driver therefore
failed on every Python version with "ImportError: cannot import name
'AzureKmsClient' from partially initialized module"; for example,
tests/schema_registry/test_azure_client.py fails at collection when run
on its own.

Import the azure_client module in azure_driver as well, so the cycle
resolves whichever module is imported first, and add a test that
imports every submodule in a fresh interpreter, skipping those whose
optional dependencies are not installed. With the test requirements
installed, the test fails without this change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
@rayokota

Copy link
Copy Markdown
Member

/sem-approve

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The import smoke test incorrectly fails for missing optional Azure or Google namespace components when another component in the same namespace is installed.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread tests/test_imports.py
Comment on lines +41 to +43
top = e.name.partition('.')[0] if e.name else None
if top and top != 'confluent_kafka' and importlib.util.find_spec(top) is None:
continue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, thanks. #2416 was merged before I got to this, so the fix is in #2424: a module is now also skipped when the missing module is a portion of a namespace package (azure.identity, azure.keyvault, google.cloud, google.oauth2, google.type), including the plain ImportError from from google.cloud import kms_v1, while a missing submodule or symbol of an installed regular package still fails. With only azure-core, only azure-identity, only tink, tink plus googleapis-common-protos, or only protobuf installed, the merged test fails and the new one passes on 3.9, 3.11 and 3.14.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

@rayokota Robert Yokota (rayokota) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rayokota
Robert Yokota (rayokota) merged commit 4443998 into confluentinc:master Nov 12, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:schema-registry Any schema registry related isues rather than kafka isolated ones

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants