| 73 | |
| 74 | |
| 75 | class FunctionSig(NamedTuple): |
| 76 | name: str |
| 77 | args: list[ArgSig] |
| 78 | ret_type: str | None |
| 79 | type_args: str = "" # TODO implement in stubgenc and remove the default |
| 80 | docstring: str | None = None |
| 81 | |
| 82 | def is_special_method(self) -> bool: |
| 83 | return bool( |
| 84 | self.name.startswith("__") |
| 85 | and self.name.endswith("__") |
| 86 | and self.args |
| 87 | and self.args[0].name in ("self", "cls") |
| 88 | ) |
| 89 | |
| 90 | def has_catchall_args(self) -> bool: |
| 91 | """Return if this signature has catchall args: (*args, **kwargs)""" |
| 92 | if self.args and self.args[0].name in ("self", "cls"): |
| 93 | args = self.args[1:] |
| 94 | else: |
| 95 | args = self.args |
| 96 | return ( |
| 97 | len(args) == 2 |
| 98 | and all(a.type in (None, "object", "Any", "typing.Any") for a in args) |
| 99 | and args[0].is_star_arg() |
| 100 | and args[1].is_star_kwarg() |
| 101 | ) |
| 102 | |
| 103 | def is_catchall_signature(self) -> bool: |
| 104 | """Return if this signature is the catchall identity: (*args, **kwargs) -> Any""" |
| 105 | return self.has_catchall_args() and self.ret_type in (None, "Any", "typing.Any") |
| 106 | |
| 107 | def format_sig( |
| 108 | self, |
| 109 | indent: str = "", |
| 110 | is_async: bool = False, |
| 111 | any_val: str | None = None, |
| 112 | docstring: str | None = None, |
| 113 | include_docstrings: bool = False, |
| 114 | ) -> str: |
| 115 | args: list[str] = [] |
| 116 | for arg in self.args: |
| 117 | arg_def = arg.name |
| 118 | |
| 119 | if arg_def in keyword.kwlist: |
| 120 | arg_def = "_" + arg_def |
| 121 | |
| 122 | if ( |
| 123 | arg.type is None |
| 124 | and any_val is not None |
| 125 | and arg.name not in ("self", "cls") |
| 126 | and not arg.name.startswith("*") |
| 127 | ): |
| 128 | arg_type: str | None = any_val |
| 129 | else: |
| 130 | arg_type = arg.type |
| 131 | if arg_type: |
| 132 | arg_def += ": " + arg_type |
no outgoing calls
searching dependent graphs…