Fix wiring for @classmethod

This commit is contained in:
Roman Mogylatov 2020-10-28 13:11:07 -04:00
parent 467e7ec9aa
commit 2565a1eab0
4 changed files with 25 additions and 1 deletions

View File

@ -7,6 +7,10 @@ that were made in every particular version.
From version 0.7.6 *Dependency Injector* framework strictly
follows `Semantic versioning`_
Develop
-----
- Fix wiring for ``@classmethod``.
4.1.5
-----
- Fix Travis CI windows and MacOS builds.

View File

@ -158,7 +158,7 @@ def wire(
if inspect.isfunction(member):
_patch_fn(module, name, member, providers_map)
elif inspect.isclass(member):
for method_name, method in inspect.getmembers(member, inspect.isfunction):
for method_name, method in inspect.getmembers(member, _is_method):
_patch_fn(member, method_name, method, providers_map)
@ -235,6 +235,10 @@ def _fetch_modules(package):
return modules
def _is_method(member):
return inspect.ismethod(member) or inspect.isfunction(member)
def _patch_with_injections(fn, injections):
if inspect.iscoroutinefunction(fn):
@functools.wraps(fn)

View File

@ -17,6 +17,14 @@ class TestClass:
def method(self, service: Service = Provide[Container.service]):
return service
@classmethod
def class_method(cls, service: Service = Provide[Container.service]):
return service
@staticmethod
def static_method(service: Service = Provide[Container.service]):
return service
def test_function(service: Service = Provide[Container.service]):
return service

View File

@ -61,6 +61,14 @@ class WiringTest(unittest.TestCase):
service = test_class_object.method()
self.assertIsInstance(service, Service)
def test_class_classmethod_wiring(self):
service = module.TestClass.class_method()
self.assertIsInstance(service, Service)
def test_class_staticmethod_wiring(self):
service = module.TestClass.static_method()
self.assertIsInstance(service, Service)
def test_function_wiring(self):
service = module.test_function()
self.assertIsInstance(service, Service)