From 4ddac663d9a81fa02f45eedce2b0cd0c5a1787e3 Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Tue, 20 Oct 2020 17:48:54 -0400 Subject: [PATCH] Fix wiring for package init (#308) * Add test * Add fix * Add extra test * Remove package imports on discovery for Python versions < 3.6 * Move wiring samples to a different directory --- src/dependency_injector/wiring.py | 2 +- .../wiringsamples}/__init__.py | 0 .../wiringsamples}/container.py | 0 .../wiringsamples}/module.py | 0 .../samples/wiringsamples/package/__init__.py | 11 +++++++ .../package/subpackage/__init__.py | 11 +++++++ .../package/subpackage/submodule.py | 0 .../wiringsamples}/service.py | 0 .../wiring/package/subpackage/__init__.py | 0 tests/unit/wiring/test_wiring_py36.py | 33 +++++++++++++++---- 10 files changed, 50 insertions(+), 7 deletions(-) rename tests/unit/{wiring/package => samples/wiringsamples}/__init__.py (100%) rename tests/unit/{wiring => samples/wiringsamples}/container.py (100%) rename tests/unit/{wiring => samples/wiringsamples}/module.py (100%) create mode 100644 tests/unit/samples/wiringsamples/package/__init__.py create mode 100644 tests/unit/samples/wiringsamples/package/subpackage/__init__.py rename tests/unit/{wiring => samples/wiringsamples}/package/subpackage/submodule.py (100%) rename tests/unit/{wiring => samples/wiringsamples}/service.py (100%) delete mode 100644 tests/unit/wiring/package/subpackage/__init__.py diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 27e62cbb..dbc5f9ba 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -225,7 +225,7 @@ def _resolve_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> def _fetch_modules(package): - modules = [] + modules = [package] for loader, module_name, is_pkg in pkgutil.walk_packages( path=package.__path__, prefix=package.__name__ + '.', diff --git a/tests/unit/wiring/package/__init__.py b/tests/unit/samples/wiringsamples/__init__.py similarity index 100% rename from tests/unit/wiring/package/__init__.py rename to tests/unit/samples/wiringsamples/__init__.py diff --git a/tests/unit/wiring/container.py b/tests/unit/samples/wiringsamples/container.py similarity index 100% rename from tests/unit/wiring/container.py rename to tests/unit/samples/wiringsamples/container.py diff --git a/tests/unit/wiring/module.py b/tests/unit/samples/wiringsamples/module.py similarity index 100% rename from tests/unit/wiring/module.py rename to tests/unit/samples/wiringsamples/module.py diff --git a/tests/unit/samples/wiringsamples/package/__init__.py b/tests/unit/samples/wiringsamples/package/__init__.py new file mode 100644 index 00000000..bdc34ed2 --- /dev/null +++ b/tests/unit/samples/wiringsamples/package/__init__.py @@ -0,0 +1,11 @@ +import sys + +if sys.version_info >= (3, 6): + from dependency_injector.wiring import Provide + + from ..container import Container + from ..service import Service + + + def test_package_function(service: Service = Provide[Container.service]): + return service diff --git a/tests/unit/samples/wiringsamples/package/subpackage/__init__.py b/tests/unit/samples/wiringsamples/package/subpackage/__init__.py new file mode 100644 index 00000000..b2d3278b --- /dev/null +++ b/tests/unit/samples/wiringsamples/package/subpackage/__init__.py @@ -0,0 +1,11 @@ +import sys + +if sys.version_info >= (3, 6): + from dependency_injector.wiring import Provide + + from ...container import Container + from ...service import Service + + + def test_package_function(service: Service = Provide[Container.service]): + return service diff --git a/tests/unit/wiring/package/subpackage/submodule.py b/tests/unit/samples/wiringsamples/package/subpackage/submodule.py similarity index 100% rename from tests/unit/wiring/package/subpackage/submodule.py rename to tests/unit/samples/wiringsamples/package/subpackage/submodule.py diff --git a/tests/unit/wiring/service.py b/tests/unit/samples/wiringsamples/service.py similarity index 100% rename from tests/unit/wiring/service.py rename to tests/unit/samples/wiringsamples/service.py diff --git a/tests/unit/wiring/package/subpackage/__init__.py b/tests/unit/wiring/package/subpackage/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/unit/wiring/test_wiring_py36.py b/tests/unit/wiring/test_wiring_py36.py index 862a8386..6e88be66 100644 --- a/tests/unit/wiring/test_wiring_py36.py +++ b/tests/unit/wiring/test_wiring_py36.py @@ -3,9 +3,20 @@ import unittest from dependency_injector.wiring import wire, Provide -from . import module, package -from .service import Service -from .container import Container +# Runtime import to avoid syntax errors in samples on Python < 3.5 +import os +_SAMPLES_DIR = os.path.abspath( + os.path.sep.join(( + os.path.dirname(__file__), + '../samples/', + )), +) +import sys +sys.path.append(_SAMPLES_DIR) + +from wiringsamples import module, package +from wiringsamples.service import Service +from wiringsamples.container import Container class WiringTest(unittest.TestCase): @@ -21,7 +32,17 @@ class WiringTest(unittest.TestCase): self.addCleanup(self.container.unwire) def test_package_lookup(self): - from .package.subpackage.submodule import test_function + from wiringsamples.package import test_package_function + service = test_package_function() + self.assertIsInstance(service, Service) + + def test_package_subpackage_lookup(self): + from wiringsamples.package.subpackage import test_package_function + service = test_package_function() + self.assertIsInstance(service, Service) + + def test_package_submodule_lookup(self): + from wiringsamples.package.subpackage.submodule import test_function service = test_function() self.assertIsInstance(service, Service) @@ -125,10 +146,10 @@ class WiringTest(unittest.TestCase): def test_unwire_package_function(self): self.container.unwire() - from .package.subpackage.submodule import test_function + from wiringsamples.package.subpackage.submodule import test_function self.assertIsInstance(test_function(), Provide) def test_unwire_package_function_by_reference(self): - from .package.subpackage import submodule + from wiringsamples.package.subpackage import submodule self.container.unwire() self.assertIsInstance(submodule.test_function(), Provide)