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

Class DocStringParser

mypy/stubdoc.py:168–382  ·  view source on GitHub ↗

Parse function signatures in documentation.

Source from the content-addressed store, hash-verified

166
167
168class DocStringParser:
169 """Parse function signatures in documentation."""
170
171 def __init__(self, function_name: str) -> None:
172 # Only search for signatures of function with this name.
173 self.function_name = function_name
174 self.state = [STATE_INIT]
175 self.accumulator = ""
176 self.arg_type: str | None = None
177 self.arg_name = ""
178 self.arg_default: str | None = None
179 self.ret_type = "Any"
180 self.found = False
181 self.args: list[ArgSig] = []
182 self.pos_only: int | None = None
183 self.keyword_only: int | None = None
184 # Valid signatures found so far.
185 self.signatures: list[FunctionSig] = []
186
187 def add_token(self, token: tokenize.TokenInfo) -> None:
188 """Process next token from the token stream."""
189 if (
190 token.type == tokenize.NAME
191 and token.string == self.function_name
192 and self.state[-1] == STATE_INIT
193 ):
194 self.state.append(STATE_FUNCTION_NAME)
195
196 elif (
197 token.type == tokenize.OP
198 and token.string == "("
199 and self.state[-1] == STATE_FUNCTION_NAME
200 ):
201 self.state.pop()
202 self.accumulator = ""
203 self.found = True
204 self.state.append(STATE_ARGUMENT_LIST)
205
206 elif self.state[-1] == STATE_FUNCTION_NAME:
207 # Reset state, function name not followed by '('.
208 self.state.pop()
209
210 elif (
211 token.type == tokenize.OP
212 and token.string in ("[", "(", "{")
213 and self.state[-1] != STATE_INIT
214 ):
215 self.accumulator += token.string
216 self.state.append(STATE_OPEN_BRACKET)
217
218 elif (
219 token.type == tokenize.OP
220 and token.string in ("]", ")", "}")
221 and self.state[-1] == STATE_OPEN_BRACKET
222 ):
223 self.accumulator += token.string
224 self.state.pop()
225

Callers 1

infer_sig_from_docstringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…