-
-
Notifications
You must be signed in to change notification settings - Fork 343
Support wiring of Cython-compiled modules #965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ZipFile
merged 10 commits into
ets-labs:develop
from
keyz182:fix/cython-cyfunction-wiring-discovery-on-develop
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
098b920
Support wiring of Cython-compiled modules
keyz182 ca3036c
refactor: simplify Cython wiring per review
keyz182 6cde4f2
fix(test): guard pyximport import for non-Cython tox envs
keyz182 74754f6
Update docs/wiring.rst
keyz182 ce9a94e
Update docs/wiring.rst
keyz182 4721621
Update tests/unit/samples/wiringcython/cythonmodule.pyx
keyz182 9715e4e
Update tox.ini
keyz182 945ddbd
Revert "fix(test): guard pyximport import for non-Cython tox envs"
keyz182 6eeb37f
docs: note @cython.annotation_typing(False) for FastAPI views/deps
keyz182 11fa4d2
fix(test): move pyximport into test_cython, drop wiring conftest
keyz182 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """DI container used by the Cython-compiled wiring fixture.""" | ||
|
|
||
| from dependency_injector import containers, providers | ||
|
|
||
|
|
||
| class Service: | ||
| """Simple service injected into the Cython-compiled fixture handlers.""" | ||
|
|
||
| def __init__(self, value: str = "default") -> None: | ||
| self.value = value | ||
|
|
||
| async def aget(self) -> str: | ||
| return self.value | ||
|
|
||
| def get(self) -> str: | ||
| return self.value | ||
|
|
||
|
|
||
| class Container(containers.DeclarativeContainer): | ||
| service = providers.Factory(Service, value="injected") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # cython: language_level=3, binding=True, embedsignature=True, annotation_typing=False | ||
|
|
||
| from dependency_injector.wiring import Provide | ||
|
|
||
| from samples.wiringcython.container import Container, Service | ||
|
|
||
|
|
||
| def sync_handler(svc: Service = Provide[Container.service]) -> str: | ||
| return svc.get() | ||
|
|
||
|
|
||
| async def async_handler(svc: Service = Provide[Container.service]) -> str: | ||
| return await svc.aget() | ||
|
|
||
|
|
||
| async def async_gen_handler(svc: Service = Provide[Container.service]): | ||
| yield svc.get() | ||
| yield svc.get() + "_2" | ||
|
|
||
|
|
||
| class HandlerClass: | ||
| async def __call__(self, svc: Service = Provide[Container.service]) -> str: | ||
| return svc.get() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Wiring discovery against Cython-compiled user modules.""" | ||
|
|
||
| import pytest | ||
|
|
||
| pytest.importorskip("Cython") | ||
|
|
||
| import pyximport # noqa: E402 | ||
|
|
||
| pyximport.install(language_level=3) | ||
|
|
||
| cythonmodule = pytest.importorskip( | ||
| "samples.wiringcython.cythonmodule", | ||
| reason="Cython fixture not built (Cython / C toolchain missing)", | ||
| ) | ||
|
|
||
| from samples.wiringcython.container import Container, Service # noqa: E402 | ||
|
|
||
| from dependency_injector import providers # noqa: E402 | ||
| from dependency_injector.wiring import ( # noqa: E402 | ||
| _is_cyfunction, | ||
| _is_function_like, | ||
| _patched_registry, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def container(): | ||
| c = Container() | ||
| c.wire(modules=[cythonmodule]) | ||
| yield c | ||
| c.unwire() | ||
|
|
||
|
|
||
| def _pure_python_fn(): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "obj,is_cy,is_func_like", | ||
| [ | ||
| pytest.param(lambda: cythonmodule.sync_handler, True, True, id="cython-sync"), | ||
| pytest.param(lambda: cythonmodule.async_handler, True, True, id="cython-async"), | ||
| pytest.param( | ||
| lambda: cythonmodule.async_gen_handler, True, True, id="cython-async-gen" | ||
| ), | ||
| pytest.param( | ||
| lambda: cythonmodule.HandlerClass.__call__, | ||
| True, | ||
| True, | ||
| id="cython-class-call", | ||
| ), | ||
| pytest.param(lambda: _pure_python_fn, False, True, id="pure-python"), | ||
| ], | ||
| ) | ||
| def test_function_like_predicate(obj, is_cy, is_func_like): | ||
| target = obj() | ||
| assert _is_cyfunction(target) is is_cy | ||
| assert _is_function_like(target) is is_func_like | ||
|
|
||
|
|
||
| def test_sync_handler_wired(container): | ||
| assert cythonmodule.sync_handler() == "injected" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_handler_wired(container): | ||
| assert await cythonmodule.async_handler() == "injected" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_gen_handler_wired(container): | ||
| results = [v async for v in cythonmodule.async_gen_handler()] | ||
| assert results == ["injected", "injected_2"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_class_method_wired(container): | ||
| handler = cythonmodule.HandlerClass() | ||
| assert await handler() == "injected" | ||
|
|
||
|
|
||
| def test_sync_handler_respects_provider_override(container): | ||
| with container.service.override(providers.Object(Service(value="overridden"))): | ||
| assert cythonmodule.sync_handler() == "overridden" | ||
| assert cythonmodule.sync_handler() == "injected" | ||
|
|
||
|
|
||
| def test_unwire_clears_injection_bindings_on_compiled_module(): | ||
| c = Container() | ||
| c.wire(modules=[cythonmodule]) | ||
|
|
||
| wrapper = cythonmodule.sync_handler | ||
| patched = _patched_registry.get_callable(wrapper) | ||
|
|
||
| assert patched is not None | ||
| assert patched.reference_injections | ||
| assert patched.injections | ||
|
|
||
| c.unwire() | ||
|
|
||
| assert patched.injections == {} | ||
| assert patched.reference_injections |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.