Skip to content

Commit

Permalink
fix: DependsAttrBinder.bind allow to use instance variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikakto committed Feb 4, 2023
1 parent eaaaa01 commit f73255c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
9 changes: 7 additions & 2 deletions fastapi_depends_ext/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ def depends_attr_bind(depends: DependsAttr, _base_class: type, instance) -> Depe
use_cache=depends.use_cache,
)

method_definition = getattr(_base_class, depends.method_name)
if hasattr(_base_class, depends.method_name):
method_definition = getattr(_base_class, depends.method_name)
else:
method_definition = getattr(instance, depends.method_name)

if isinstance(method_definition, property):
depends_copy.dependency = method_definition.fget(instance)
else:
Expand All @@ -67,7 +71,8 @@ def depends_attr_get_method(depends: DependsAttr, _base_class: type, instance) -
obj = super(_base_class, instance) if depends.from_super else instance
return getattr(obj, depends.method_name)

base_class = get_base_class(self, method.__name__, method)
method_name = getattr(method, "__name__", type(method).__name__)
base_class = get_base_class(self, method_name, method)
signature = get_typed_signature(method)
parameters = (param for param in signature.parameters.values() if isinstance(param.default, DependsAttr))
instance_method_params = {
Expand Down
9 changes: 6 additions & 3 deletions tests/depends/depends_attr/test_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def method(self, depends_method: Any = depends):
assert depends.dependency is function


def test_bind__dependence_depends_property_type__dependency_has_been_set():
def test_bind__dependence_depends_class_variable_type__dependency_has_been_set():
depends = DependsAttr("dependency")

class Dependency:
Expand All @@ -117,15 +117,18 @@ def method(self, depends_method: Any = depends):
assert depends.dependency is Dependency


def test_bind__dependence_depends_property_instance__dependency_has_been_set():
def test_bind__dependence_depends_instance_defined_property__dependency_has_been_set():
depends = DependsAttr("property")

class CallableClass:
def __call__(self):
pass

class TestClass(SimpleDependency):
property = CallableClass()
property: CallableClass

def __init__(self):
self.property = CallableClass()

def method(self, depends_method: Any = depends):
pass
Expand Down

0 comments on commit f73255c

Please sign in to comment.