Skip to content

Commit a749368

Browse files
authored
stubgen: Fix mis-parsing of double colon ("::") (#20285)
When parsing the docstrings of a function to find its set of overloads, C++ code samples that mention the function name were at risk of matching the C++ scoping operator "::" as a type annotation, leading to malformed output. For example "MyFunc(foo: int) -> bool" is a valid signature, but the sample code "MyFunc(Eigen::Lower)" was being mis-parsed. Fix this by patching stubgen to reset in case it sees a double colon where a single colon "name: type" stanza was expected.
1 parent 149f753 commit a749368

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

mypy/stubdoc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
233233
self.accumulator = ""
234234
self.state.append(STATE_ARGUMENT_TYPE)
235235

236+
elif (
237+
token.type == tokenize.OP
238+
and token.string == ":"
239+
and self.state[-1] == STATE_ARGUMENT_TYPE
240+
and self.accumulator == ""
241+
):
242+
# We thought we were after the colon of an "arg_name: arg_type"
243+
# stanza, so we were expecting an "arg_type" now. However, we ended
244+
# up with "arg_name::" (with two colons). That's a C++ type name,
245+
# not an argument name followed by a Python type. This function
246+
# signature is malformed / invalid.
247+
self.reset()
248+
236249
elif (
237250
token.type == tokenize.OP
238251
and token.string == "="

mypy/test/teststubgen.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ def test_infer_sig_from_docstring(self) -> None:
371371
[FunctionSig(name="func", args=[ArgSig(name="x", type=None)], ret_type="Any")],
372372
)
373373

374+
assert_equal(infer_sig_from_docstring("\nfunc(invalid::type)", "func"), [])
375+
374376
assert_equal(
375377
infer_sig_from_docstring('\nfunc(x: str="")', "func"),
376378
[

0 commit comments

Comments
 (0)