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
This commit is contained in:
Roman Mogylatov 2020-10-20 17:48:54 -04:00 committed by GitHub
parent fa81cadf29
commit 4ddac663d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 50 additions and 7 deletions

View File

@ -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__ + '.',

View File

@ -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

View File

@ -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

View File

@ -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)