Repository navigation
Fix circular import in the Azure KMS driver - #2416
Robert Yokota (rayokota) merged 1 commit into
Conversation
|
🎉 All Contributor License Agreements have been signed. Ready to merge. |
|
/sem-approve |
There was a problem hiding this comment.
🟡 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
AzureKmsClientthrough 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
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
5fa9e31 to
b736657
Compare
|
/sem-approve |
| 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 |
There was a problem hiding this comment.
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
Robert Yokota (rayokota)
left a comment
There was a problem hiding this comment.
Thanks Pierre-Yves Le Borgne (@pylaterreur) , LGTM
4443998
into
confluentinc:master

What
Importing
confluent_kafka.schema_registry.rules.encryption.azurekms.azure_clientbeforeazure_driverraisesImportError: cannot import name 'AzureKmsClient' from partially initialized module ... (most likely due to a circular import)on every Python version:azure_clientimports theazure_drivermodule for its key version helpers, andazure_driverimports theAzureKmsClientclass fromazure_client. For example,pytest tests/schema_registry/test_azure_client.pyfails at collection when run on its own.azure_drivernow imports theazure_clientmodule rather than the class, asazure_clientalready does forazure_driver, so the cycle resolves whichever module is imported first.The new
tests/test_imports.pyimports 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
azure_clientcan be imported beforeazure_driver. No API change.tests/test_imports.py, which fails on master when therulesdependencies (tink, Azure SDK) are installed.References
Replaces #2401: its Python 3.8 fix is moot once #2406 drops 3.8.
Test & Review
requirements/requirements-tests-install.txt: on master,pytest tests/schema_registry/test_azure_client.pyerrors at collection, and the new test fails with theImportError; with this change, both files pass (7 tests).pytest tests --ignore=tests/integration: 2544 passed, 16 skipped. The full run does not show the bug on master, becausetests/schema_registry/_async/test_avro_serdes_rules.pyis collected beforetest_azure_client.pyand importsazure_driverfirst.tools/style-format.shon the changed files,flake8,mypy src/confluent_kafkaandtools/unasync.py --checkpass.Open questions / Follow-ups
confluent_kafka.aio(TypeError: 'type' object is not subscriptable, the bug Fix confluent_kafka.aio import on Python 3.8 #2401 fixed). No CI job runs the unit tests on 3.8, and Add Python 3.15 and 3.15t wheels, drop Python 3.8 #2406 drops 3.8.🤖 Generated with Claude Code
https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN