diff --git a/docs/main/changelog.rst b/docs/main/changelog.rst index 6f7fe075..ff45a34c 100644 --- a/docs/main/changelog.rst +++ b/docs/main/changelog.rst @@ -7,6 +7,14 @@ that were made in every particular version. From version 0.7.6 *Dependency Injector* framework strictly follows `Semantic versioning`_ +Development version +------------------- +- Fix a Python 3.9 specific bug in ``wiring`` module: introspection doesn't work for + builtin ``types.GenericAlias``. This resulted in wiring failure for modules + importing ``queue.Queue``. + See issue `#362 `_. + Thanks `@ventaquil `_ for the bug report. + 4.10.0 ------ - Add ``strict`` mode and ``required`` modifier for ``Configuration`` provider. diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 9318b654..c738bf02 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -28,6 +28,12 @@ else: class GenericMeta(type): ... +# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362 +if sys.version_info >= (3, 9): + from types import GenericAlias +else: + GenericAlias = None + try: from fastapi.params import Depends as FastAPIDepends @@ -333,6 +339,10 @@ def _unpatch( def _fetch_reference_injections( fn: Callable[..., Any], ) -> Tuple[Dict[str, Any], Dict[str, Any]]: + # # Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362 + if GenericAlias and fn is GenericAlias: + fn = fn.__init__ + signature = inspect.signature(fn) injections = {} diff --git a/tests/unit/samples/wiringsamples/queuemodule.py b/tests/unit/samples/wiringsamples/queuemodule.py new file mode 100644 index 00000000..5849a7bd --- /dev/null +++ b/tests/unit/samples/wiringsamples/queuemodule.py @@ -0,0 +1,5 @@ +# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/362 +from queue import Queue + + +__all__ = ('Queue',) diff --git a/tests/unit/wiring/test_wiring_py36.py b/tests/unit/wiring/test_wiring_py36.py index 1972b8bc..84938a6a 100644 --- a/tests/unit/wiring/test_wiring_py36.py +++ b/tests/unit/wiring/test_wiring_py36.py @@ -263,6 +263,21 @@ class WiringTest(unittest.TestCase): self.assertIsInstance(service, Service) +class WiringAndQueue(unittest.TestCase): + + def test_wire_queue(self) -> None: + from wiringsamples import queuemodule + container = Container() + self.addCleanup(container.unwire) + + # Should not raise exception + # See: https://github.com/ets-labs/python-dependency-injector/issues/362 + try: + container.wire(modules=[queuemodule]) + except: + raise + + class WiringAndFastAPITest(unittest.TestCase): container: Container