Do not override methods without patching (#886)

This commit is contained in:
ZipFile 2025-05-18 12:17:54 +03:00 committed by GitHub
parent 9a08bfcede
commit dbf86e4eb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 0 deletions

View File

@ -523,6 +523,10 @@ def _patch_method(
_bind_injections(fn, providers_map)
if fn is method:
# Hotfix, see: https://github.com/ets-labs/python-dependency-injector/issues/884
return
if isinstance(method, (classmethod, staticmethod)):
fn = type(method)(fn)

View File

@ -0,0 +1,40 @@
from typing import Any, Iterator
from pytest import fixture
from dependency_injector.containers import DeclarativeContainer
from dependency_injector.providers import Object
from dependency_injector.wiring import Provide, inject
class A:
@inject
def foo(self, value: str = Provide["value"]) -> str:
return "A" + value
class B(A): ...
class C(A):
def foo(self, *args: Any, **kwargs: Any) -> str:
return "C" + super().foo()
class D(B, C): ...
class Container(DeclarativeContainer):
value = Object("X")
@fixture
def container() -> Iterator[Container]:
c = Container()
c.wire(modules=[__name__])
yield c
c.unwire()
def test_preserve_mro(container: Container) -> None:
assert D().foo() == "CAX"