Skip to content

Commit a43a873

Browse files
committed
Support methods as annotate functions
1 parent f5ea8a0 commit a43a873

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Lib/annotationlib.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,11 @@ def _get_annotate_attr(annotate, attr, default):
891891
if (value := getattr(annotate, attr, None)) is not None:
892892
return value
893893

894+
if isinstance(annotate, types.MethodType):
895+
if call_func := getattr(annotate, "__func__", None):
896+
return getattr(call_func, attr, default)
897+
898+
# Class instances themselves aren't methods, their __call__ functions are.
894899
if isinstance(annotate.__call__, types.MethodType):
895900
if call_func := getattr(annotate.__call__, "__func__", None):
896901
return getattr(call_func, attr, default)
@@ -910,7 +915,14 @@ def _get_annotate_attr(annotate, attr, default):
910915
return default
911916

912917
def _direct_call_annotate(func, annotate, format):
913-
# If annotate is a method, we need to pass its self as the first param
918+
# If annotate is a method, we need to pass self as the first param.
919+
if (
920+
hasattr(annotate, "__func__") and
921+
(self := getattr(annotate, "__self__", None))
922+
):
923+
return func(self, format)
924+
925+
# If annotate is a class instance, its __call__ function is the method.
914926
if (
915927
hasattr(annotate.__call__, "__func__") and
916928
(self := getattr(annotate.__call__, "__self__", None))

0 commit comments

Comments
 (0)