mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 09:36:48 +03:00
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:
parent
fa81cadf29
commit
4ddac663d9
|
@ -225,7 +225,7 @@ def _resolve_injections(fn: Callable[..., Any], providers_map: ProvidersMap) ->
|
||||||
|
|
||||||
|
|
||||||
def _fetch_modules(package):
|
def _fetch_modules(package):
|
||||||
modules = []
|
modules = [package]
|
||||||
for loader, module_name, is_pkg in pkgutil.walk_packages(
|
for loader, module_name, is_pkg in pkgutil.walk_packages(
|
||||||
path=package.__path__,
|
path=package.__path__,
|
||||||
prefix=package.__name__ + '.',
|
prefix=package.__name__ + '.',
|
||||||
|
|
11
tests/unit/samples/wiringsamples/package/__init__.py
Normal file
11
tests/unit/samples/wiringsamples/package/__init__.py
Normal 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
|
|
@ -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
|
|
@ -3,9 +3,20 @@ import unittest
|
||||||
|
|
||||||
from dependency_injector.wiring import wire, Provide
|
from dependency_injector.wiring import wire, Provide
|
||||||
|
|
||||||
from . import module, package
|
# Runtime import to avoid syntax errors in samples on Python < 3.5
|
||||||
from .service import Service
|
import os
|
||||||
from .container import Container
|
_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):
|
class WiringTest(unittest.TestCase):
|
||||||
|
@ -21,7 +32,17 @@ class WiringTest(unittest.TestCase):
|
||||||
self.addCleanup(self.container.unwire)
|
self.addCleanup(self.container.unwire)
|
||||||
|
|
||||||
def test_package_lookup(self):
|
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()
|
service = test_function()
|
||||||
self.assertIsInstance(service, Service)
|
self.assertIsInstance(service, Service)
|
||||||
|
|
||||||
|
@ -125,10 +146,10 @@ class WiringTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_unwire_package_function(self):
|
def test_unwire_package_function(self):
|
||||||
self.container.unwire()
|
self.container.unwire()
|
||||||
from .package.subpackage.submodule import test_function
|
from wiringsamples.package.subpackage.submodule import test_function
|
||||||
self.assertIsInstance(test_function(), Provide)
|
self.assertIsInstance(test_function(), Provide)
|
||||||
|
|
||||||
def test_unwire_package_function_by_reference(self):
|
def test_unwire_package_function_by_reference(self):
|
||||||
from .package.subpackage import submodule
|
from wiringsamples.package.subpackage import submodule
|
||||||
self.container.unwire()
|
self.container.unwire()
|
||||||
self.assertIsInstance(submodule.test_function(), Provide)
|
self.assertIsInstance(submodule.test_function(), Provide)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user