diff --git a/src/dependency_injector/wiring.py b/src/dependency_injector/wiring.py index 6f54c993..4dd77c09 100644 --- a/src/dependency_injector/wiring.py +++ b/src/dependency_injector/wiring.py @@ -552,6 +552,8 @@ def _unbind_injections(fn: Callable[..., Any]) -> None: def _fetch_modules(package): modules = [package] + if not hasattr(package, '__path__') or not hasattr(package, '__name__'): + return modules for module_info in pkgutil.walk_packages( path=package.__path__, prefix=package.__name__ + '.', diff --git a/tests/unit/wiring/test_wiring_py36.py b/tests/unit/wiring/test_wiring_py36.py index 64ff7fc7..7c503782 100644 --- a/tests/unit/wiring/test_wiring_py36.py +++ b/tests/unit/wiring/test_wiring_py36.py @@ -310,6 +310,18 @@ class WiringTest(unittest.TestCase): self.assertIsInstance(service, Service) +class ModuleAsPackagingTest(unittest.TestCase): + + def setUp(self): + self.container = Container(config={'a': {'b': {'c': 10}}}) + self.addCleanup(self.container.unwire) + + def test_module_as_package_wiring(self): + # See: https://github.com/ets-labs/python-dependency-injector/issues/481 + self.container.wire(packages=[module]) + self.assertIsInstance(module.service, Service) + + class WiringAndQueue(unittest.TestCase): def test_wire_queue(self) -> None: