@@ -1654,6 +1654,18 @@ def mro(self) -> list[Class]:
1654
1654
"""Return a list of classes in order corresponding to Python's MRO."""
1655
1655
return cast ("Class" , self .final_target ).mro ()
1656
1656
1657
+ def signature (self , * , return_type : bool = False , name : str | None = None ) -> str :
1658
+ """Construct the class/function signature.
1659
+
1660
+ Parameters:
1661
+ return_type: Whether to include the return type in the signature.
1662
+ name: The name of the class/function to use in the signature.
1663
+
1664
+ Returns:
1665
+ A string representation of the class/function signature.
1666
+ """
1667
+ return cast ("Union[Class, Function]" , self .final_target ).signature (return_type = return_type , name = name )
1668
+
1657
1669
# SPECIFIC ALIAS METHOD AND PROPERTIES -----------------
1658
1670
# These methods and properties do not exist on targets,
1659
1671
# they are specific to aliases.
@@ -1976,6 +1988,23 @@ def parameters(self) -> Parameters:
1976
1988
except KeyError :
1977
1989
return Parameters ()
1978
1990
1991
+ def signature (self , * , return_type : bool = False , name : str | None = None ) -> str :
1992
+ """Construct the class signature.
1993
+
1994
+ Parameters:
1995
+ return_type: Whether to include the return type in the signature.
1996
+ name: The name of the class to use in the signature.
1997
+
1998
+ Returns:
1999
+ A string representation of the class signature.
2000
+ """
2001
+ all_members = self .all_members
2002
+ if "__init__" in all_members :
2003
+ init = all_members ["__init__" ]
2004
+ if isinstance (init , Function ):
2005
+ return init .signature (return_type = return_type , name = name or self .name )
2006
+ return ""
2007
+
1979
2008
@property
1980
2009
def resolved_bases (self ) -> list [Object ]:
1981
2010
"""Resolved class bases.
@@ -2125,6 +2154,80 @@ def as_dict(self, **kwargs: Any) -> dict[str, Any]:
2125
2154
base ["returns" ] = self .returns
2126
2155
return base
2127
2156
2157
+ def signature (self , * , return_type : bool = True , name : str | None = None ) -> str :
2158
+ """Construct the function signature.
2159
+
2160
+ Parameters:
2161
+ return_type: Whether to include the return type in the signature.
2162
+ name: The name of the function to use in the signature.
2163
+
2164
+ Returns:
2165
+ A string representation of the function signature.
2166
+ """
2167
+ signature = f"{ name or self .name } ("
2168
+
2169
+ has_pos_only = any (p .kind == ParameterKind .positional_only for p in self .parameters )
2170
+ render_pos_only_separator = True
2171
+ render_kw_only_separator = True
2172
+
2173
+ param_strs = []
2174
+
2175
+ for index , param in enumerate (self .parameters ):
2176
+ # Skip 'self' or 'cls' for class methods if it's the first parameter.
2177
+ if index == 0 and param .name in ("self" , "cls" ) and self .parent and self .parent .is_class :
2178
+ continue
2179
+
2180
+ param_str = ""
2181
+
2182
+ # Handle parameter kind and separators.
2183
+ if param .kind != ParameterKind .positional_only :
2184
+ if has_pos_only and render_pos_only_separator :
2185
+ render_pos_only_separator = False
2186
+ param_strs .append ("/" )
2187
+
2188
+ if param .kind == ParameterKind .keyword_only and render_kw_only_separator :
2189
+ render_kw_only_separator = False
2190
+ param_strs .append ("*" )
2191
+
2192
+ # Handle variadic parameters.
2193
+ if param .kind == ParameterKind .var_positional :
2194
+ param_str = "*"
2195
+ render_kw_only_separator = False
2196
+ elif param .kind == ParameterKind .var_keyword :
2197
+ param_str = "**"
2198
+
2199
+ # Add parameter name.
2200
+ param_str += param .name
2201
+
2202
+ # Handle type annotation
2203
+ if param .annotation is not None :
2204
+ param_str += f": { param .annotation } "
2205
+ equal = " = " # Space around equal when annotation is present.
2206
+ else :
2207
+ equal = "=" # No space when no annotation.
2208
+
2209
+ # Handle default value.
2210
+ if param .default is not None and param .kind not in {
2211
+ ParameterKind .var_positional ,
2212
+ ParameterKind .var_keyword ,
2213
+ }:
2214
+ param_str += f"{ equal } { param .default } "
2215
+
2216
+ param_strs .append (param_str )
2217
+
2218
+ # If we have positional-only parameters but no '/' was added yet
2219
+ if has_pos_only and render_pos_only_separator :
2220
+ param_strs .append ("/" )
2221
+
2222
+ signature += ", " .join (param_strs )
2223
+ signature += ")"
2224
+
2225
+ # Add return type if present.
2226
+ if return_type and self .annotation :
2227
+ signature += f" -> { self .annotation } "
2228
+
2229
+ return signature
2230
+
2128
2231
2129
2232
class Attribute (Object ):
2130
2233
"""The class representing a Python module/class/instance attribute."""
0 commit comments