MCPcopy Create free account
hub / github.com/python/mypy / format_sig

Method format_sig

mypy/stubdoc.py:107–155  ·  view source on GitHub ↗
(
        self,
        indent: str = "",
        is_async: bool = False,
        any_val: str | None = None,
        docstring: str | None = None,
        include_docstrings: bool = False,
    )

Source from the content-addressed store, hash-verified

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
133 if arg.default:
134 arg_def += f" = {arg.default_value}"
135
136 elif arg.default:
137 arg_def += f"={arg.default_value}"
138
139 args.append(arg_def)
140
141 retfield = ""
142 ret_type = self.ret_type if self.ret_type else any_val
143 if ret_type is not None:
144 retfield = " -> " + ret_type
145
146 prefix = "async " if is_async else ""
147 sig = f"{indent}{prefix}def {self.name}{self.type_args}({', '.join(args)}){retfield}:"
148 # if this object has a docstring it's probably produced by a SignatureGenerator, so it
149 # takes precedence over the passed docstring, which acts as a fallback.
150 doc = (self.docstring or docstring) if include_docstrings else None
151 if doc:
152 suffix = f"\n{indent} {mypy.util.quote_docstring(doc)}"
153 else:
154 suffix = " ..."
155 return f"{sig}{suffix}"
156
157
158# States of the docstring parser.

Callers 4

format_func_defMethod · 0.80
_from_sigMethod · 0.80
_from_sigsMethod · 0.80

Calls 3

appendMethod · 0.80
startswithMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected