3939from typing import TypeVar
4040from typing import Union
4141from typing import get_origin
42+
43+ try :
44+ # This only exists on Python 3.11 and later. On older versions,
45+ # we just replace it with a function that does nothing.
46+ from typing import get_overloads
47+ except ImportError :
48+
49+ def get_overloads (* args , ** kwargs ):
50+ return []
51+
52+
4253import warnings
4354
4455from pdoc import doc_ast
@@ -989,6 +1000,69 @@ def signature(self) -> inspect.Signature:
9891000 return inspect .Signature (
9901001 [inspect .Parameter ("unknown" , inspect .Parameter .POSITIONAL_OR_KEYWORD )]
9911002 )
1003+
1004+ return self ._process_signature (sig )
1005+
1006+ @cached_property
1007+ def signature_without_self (self ) -> inspect .Signature :
1008+ """Like `signature`, but without the first argument.
1009+
1010+ This is useful to display constructors.
1011+ """
1012+ return self .signature .replace (
1013+ parameters = list (self .signature .parameters .values ())[1 :]
1014+ )
1015+
1016+ @cached_property
1017+ def overloads (self ) -> list [inspect .Signature ]:
1018+ """
1019+ The function's overloaded signatures, if any.
1020+
1021+ This should do the same processing as `signature`, but can return a list
1022+ of additional signatures when available.
1023+ """
1024+
1025+ if self .obj is object .__init__ :
1026+ # See `signature`.
1027+ return inspect .Signature ()
1028+
1029+ try :
1030+ values = get_overloads (self .obj )
1031+ except Exception :
1032+ return []
1033+
1034+ results = []
1035+ for value in values :
1036+ try :
1037+ sig = _PrettySignature .from_callable (value )
1038+ results .append (self ._process_signature (sig ))
1039+ except Exception :
1040+ sig = inspect .Signature (
1041+ [
1042+ inspect .Parameter (
1043+ "unknown" , inspect .Parameter .POSITIONAL_OR_KEYWORD
1044+ )
1045+ ]
1046+ )
1047+ results .append (sig )
1048+ return results
1049+
1050+ @cached_property
1051+ def overloads_without_self (self ) -> list [inspect .Signature ]:
1052+ """Like `overloads`, but without the first argument.
1053+
1054+ This is useful to display constructors.
1055+ """
1056+ return [
1057+ sig .replace (parameters = list (self .signature .parameters .values ())[1 :])
1058+ for sig in self .overloads
1059+ ]
1060+
1061+ def _process_signature (self , sig : inspect .Signature ) -> inspect .Signature :
1062+ """
1063+ A helper method for `signature` and `overloads` which performs
1064+ necessary post-processing on a signature object.
1065+ """
9921066 mod = inspect .getmodule (self .obj )
9931067 globalns = _safe_getattr (mod , "__dict__" , {})
9941068 localns = globalns
@@ -1012,16 +1086,6 @@ def signature(self) -> inspect.Signature:
10121086 )
10131087 return sig
10141088
1015- @cached_property
1016- def signature_without_self (self ) -> inspect .Signature :
1017- """Like `signature`, but without the first argument.
1018-
1019- This is useful to display constructors.
1020- """
1021- return self .signature .replace (
1022- parameters = list (self .signature .parameters .values ())[1 :]
1023- )
1024-
10251089
10261090class Variable (Doc [None ]):
10271091 """
0 commit comments