11import functools
22import inspect
33import re
4+ import types
45import typing
56import warnings
67from collections import defaultdict
@@ -541,20 +542,20 @@ def _modify_callable(
541542 modify_annotations : Callable [[dict [str , Any ]], dict [str , Any ]] = lambda a : a ,
542543 modify_defaults : Callable [[tuple [Any , ...]], tuple [Any , ...]] = lambda a : a ,
543544):
544- annotation_modifying_decorator = _copy_function (call )
545+ annotation_modifying_wrapper = _copy_function (call )
545546 old_params = inspect .signature (call ).parameters
546- callable_annotations = annotation_modifying_decorator .__annotations__
547+ callable_annotations = annotation_modifying_wrapper .__annotations__
547548
548- annotation_modifying_decorator .__annotations__ = modify_annotations (callable_annotations )
549- annotation_modifying_decorator .__defaults__ = modify_defaults (
549+ annotation_modifying_wrapper .__annotations__ = modify_annotations (callable_annotations )
550+ annotation_modifying_wrapper .__defaults__ = modify_defaults (
550551 tuple (p .default for p in old_params .values () if p .default is not inspect .Signature .empty ),
551552 )
552- annotation_modifying_decorator .__signature__ = _generate_signature (
553- annotation_modifying_decorator ,
553+ annotation_modifying_wrapper .__signature__ = _generate_signature (
554+ annotation_modifying_wrapper ,
554555 old_params ,
555556 )
556557
557- return annotation_modifying_decorator
558+ return annotation_modifying_wrapper
558559
559560
560561def _remake_endpoint_dependencies (route : fastapi .routing .APIRoute ):
@@ -718,10 +719,13 @@ def _copy_function(function: _T) -> _T:
718719 while hasattr (function , "__alt_wrapped__" ):
719720 function = function .__alt_wrapped__
720721
722+ if not isinstance (function , types .FunctionType ):
723+ # This means that the callable is actually an instance of a regular class
724+ function = function .__call__
721725 if inspect .iscoroutinefunction (function ):
722726
723727 @functools .wraps (function )
724- async def annotation_modifying_decorator ( # pyright: ignore[reportRedeclaration]
728+ async def annotation_modifying_wrapper ( # pyright: ignore[reportRedeclaration]
725729 * args : Any ,
726730 ** kwargs : Any ,
727731 ) -> Any :
@@ -730,16 +734,16 @@ async def annotation_modifying_decorator( # pyright: ignore[reportRedeclaration
730734 else :
731735
732736 @functools .wraps (function )
733- def annotation_modifying_decorator (
737+ def annotation_modifying_wrapper (
734738 * args : Any ,
735739 ** kwargs : Any ,
736740 ) -> Any :
737741 return function (* args , ** kwargs )
738742
739743 # Otherwise it will have the same signature as __wrapped__ due to how inspect module works
740- annotation_modifying_decorator .__alt_wrapped__ = ( # pyright: ignore[reportAttributeAccessIssue]
741- annotation_modifying_decorator .__wrapped__
744+ annotation_modifying_wrapper .__alt_wrapped__ = ( # pyright: ignore[reportAttributeAccessIssue]
745+ annotation_modifying_wrapper .__wrapped__
742746 )
743- del annotation_modifying_decorator .__wrapped__
747+ del annotation_modifying_wrapper .__wrapped__
744748
745- return cast (_T , annotation_modifying_decorator )
749+ return cast (_T , annotation_modifying_wrapper )
0 commit comments