Skip to content

Commit

Permalink
fix: get_base_class correct work with patched functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikakto committed Feb 4, 2023
1 parent f73255c commit 0ece289
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion fastapi_depends_ext/utils.py
Expand Up @@ -13,7 +13,11 @@ def _get_func(instance, func) -> callable:
if type(func) is property:
return _get_func(instance, func.fget(instance))
elif type(func) in (classmethod, staticmethod) or inspect.ismethod(func):
return func.__func__
f = func.__func__
if hasattr(f, "__origin__"):
return _get_func(instance, f.__origin__)
else:
return f
elif callable(func):
return func
else:
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/test_get_base_class.py
Expand Up @@ -3,6 +3,7 @@
import pytest

from fastapi_depends_ext.utils import get_base_class
from fastapi_depends_ext.utils import patch_defaults
from tests.utils_for_tests import SimpleDependency


Expand Down Expand Up @@ -150,3 +151,17 @@ def test_get_base_class__class_defined_not_callable__AttributeError(_method):

with pytest.raises(TypeError, match=re.escape(f"Incorrect type of `{instance.method}`")):
get_base_class(instance, "method", instance.method)


@pytest.mark.parametrize("_method", supported_callable)
def test_get_base_class__patched_method__correct_class(_method):
class TestClass:
def __init__(self):
setattr(self, "method", patch_defaults(self.method))

def method(self):
pass

instance = TestClass()

assert get_base_class(instance, "method", instance.method) is TestClass

0 comments on commit 0ece289

Please sign in to comment.