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+ from typing import Sequence
49+
50+ def get_overloads (func : Callable [..., object ]) -> Sequence [Callable [..., object ]]:
51+ return []
52+
53+
4254import warnings
4355
4456from pdoc import doc_ast
@@ -989,6 +1001,69 @@ def signature(self) -> inspect.Signature:
9891001 return inspect .Signature (
9901002 [inspect .Parameter ("unknown" , inspect .Parameter .POSITIONAL_OR_KEYWORD )]
9911003 )
1004+
1005+ return self ._process_signature (sig )
1006+
1007+ @cached_property
1008+ def signature_without_self (self ) -> inspect .Signature :
1009+ """Like `signature`, but without the first argument.
1010+
1011+ This is useful to display constructors.
1012+ """
1013+ return self .signature .replace (
1014+ parameters = list (self .signature .parameters .values ())[1 :]
1015+ )
1016+
1017+ @cached_property
1018+ def overloads (self ) -> list [inspect .Signature ]:
1019+ """
1020+ The function's overloaded signatures, if any.
1021+
1022+ This should do the same processing as `signature`, but can return a list
1023+ of additional signatures when available.
1024+ """
1025+
1026+ if self .obj is object .__init__ :
1027+ # See `signature`.
1028+ return [inspect .Signature ()]
1029+
1030+ try :
1031+ values = get_overloads (self .obj )
1032+ except Exception :
1033+ return []
1034+
1035+ results = []
1036+ for value in values :
1037+ try :
1038+ sig = _PrettySignature .from_callable (value )
1039+ results .append (self ._process_signature (sig ))
1040+ except Exception :
1041+ sig_err = inspect .Signature (
1042+ [
1043+ inspect .Parameter (
1044+ "unknown" , inspect .Parameter .POSITIONAL_OR_KEYWORD
1045+ )
1046+ ]
1047+ )
1048+ results .append (sig_err )
1049+ return results
1050+
1051+ @cached_property
1052+ def overloads_without_self (self ) -> list [inspect .Signature ]:
1053+ """Like `overloads`, but without the first argument.
1054+
1055+ This is useful to display constructors.
1056+ """
1057+ return [
1058+ sig .replace (parameters = list (self .signature .parameters .values ())[1 :])
1059+ for sig in self .overloads
1060+ ]
1061+
1062+ def _process_signature (self , sig : inspect .Signature ) -> inspect .Signature :
1063+ """
1064+ A helper method for `signature` and `overloads` which performs
1065+ necessary post-processing on a signature object.
1066+ """
9921067 mod = inspect .getmodule (self .obj )
9931068 globalns = _safe_getattr (mod , "__dict__" , {})
9941069 localns = globalns
@@ -1012,16 +1087,6 @@ def signature(self) -> inspect.Signature:
10121087 )
10131088 return sig
10141089
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-
10251090
10261091class Variable (Doc [None ]):
10271092 """
0 commit comments