Skip to content

Fix confluent_kafka.aio import on Python 3.8 - #2401

Closed
Pierre-Yves Le Borgne (pylaterreur) wants to merge 3 commits into
confluentinc:masterfrom
pylaterreur:fix/py38-aio-import
Closed

Pierre-Yves Le Borgne (pylaterreur) wants to merge 3 commits into
confluentinc:masterfrom
pylaterreur:fix/py38-aio-import

Conversation

@pylaterreur

Copy link
Copy Markdown
Contributor

What

import confluent_kafka.aio fails on Python 3.8 with TypeError: 'type' object is not subscriptable (2.13.0 to 2.16.0 and master), although requires-python is >=3.8 and cp38 wheels are published. asyncio.Future is only subscriptable from 3.9, and three modules of the asyncio producer use asyncio.Future[Any] in annotations that are evaluated at runtime: the MessageBatch fields, function signatures, and the callback factory that _assign_future_callbacks() defines for each message. This adds from __future__ import annotations to those modules.

The new tests/test_imports.py imports every submodule in a fresh interpreter, skipping modules whose optional dependencies are not installed. It also found a circular import: importing azurekms.azure_client before azurekms.azure_driver raises ImportError on any Python version, so 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.

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • confluent_kafka.aio can be imported on Python 3.8 again. No API change.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • tests/test_imports.py, which fails on 3.8 without this change.

Test & Review

  • Python 3.8.20, pytest tests/test_*.py: on master, collection stops with 6 TypeError errors; with this change, the 148 tests of those 6 modules run and pass.
  • Python 3.9.25 and 3.15.0, pytest tests/test_*.py: same results as on master, plus the new test. On 3.9, with tink and the Azure SDK installed, the new test fails on master because of the circular import.
  • Python 3.15.0t: the new test passes and the GIL stays disabled.
  • Every submodule whose dependencies are installed imports on 3.8, 3.9, 3.15 and 3.15t. On 3.8, the rules and oauthbearer-aws modules also import with stand-ins for their dependencies, which cannot be installed on 3.8.
  • tools/style-format.sh (isort, black, flake8) on the changed files and mypy src/confluent_kafka pass.

Open questions / Follow-ups

  • No CI job imports confluent_kafka.aio on 3.8: the wheel builds only run tests/test_error.py. Adding tests/test_imports.py to CIBW_TEST_COMMAND would check every wheel (see also Update wheels CI task to run all unit tests #2303).
  • On 3.8 the rules and oauthbearer-aws extras cannot be installed (cel-python>=0.4.0 and boto3>=1.42.25 need 3.9+), so neither can requirements/requirements-tests-install.txt.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
On Python 3.8, `import confluent_kafka.aio` raised "TypeError: 'type'
object is not subscriptable", which also stopped the unit tests at
collection. asyncio.Future is only subscriptable from Python 3.9, and
three modules of the asyncio producer use asyncio.Future[Any] in
annotations that are evaluated at runtime: the MessageBatch fields,
function signatures, and the callback factory that
_assign_future_callbacks() defines for each message. Releases 2.13.0 to
2.16.0 are affected; pyproject.toml declares Python >= 3.8 and cp38
wheels are published.

Add `from __future__ import annotations` to these modules, so their
annotations are not evaluated, and add a test that imports every
submodule in a fresh interpreter, skipping those whose optional
dependencies are not installed. On Python 3.8 the test fails without
this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: 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.

@pylaterreur

Copy link
Copy Markdown
Contributor Author

Closing: #2406 drops Python 3.8. The Azure KMS circular-import fix from this PR is now #2416.

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.

1 participant