Update wiring to support modules provided as packages

This commit is contained in:
Roman Mogylatov 2021-08-06 15:56:24 -04:00
parent b4ddf61939
commit 7bdcc33eda
2 changed files with 14 additions and 0 deletions

View File

@ -552,6 +552,8 @@ def _unbind_injections(fn: Callable[..., Any]) -> None:
def _fetch_modules(package): def _fetch_modules(package):
modules = [package] modules = [package]
if not hasattr(package, '__path__') or not hasattr(package, '__name__'):
return modules
for module_info in pkgutil.walk_packages( for module_info in pkgutil.walk_packages(
path=package.__path__, path=package.__path__,
prefix=package.__name__ + '.', prefix=package.__name__ + '.',

View File

@ -310,6 +310,18 @@ class WiringTest(unittest.TestCase):
self.assertIsInstance(service, Service) 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): class WiringAndQueue(unittest.TestCase):
def test_wire_queue(self) -> None: def test_wire_queue(self) -> None: