Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stub generation false reporting #17658

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ def __init__(self, function_name: str) -> None:

def add_token(self, token: tokenize.TokenInfo) -> None:
"""Process next token from the token stream."""

if (
self.state[-1] == STATE_ARGUMENT_TYPE
and token.type != tokenize.NAME
and len(self.accumulator) == 0
):
# the next token after : must be a name
self.reset()
return

if (
token.type == tokenize.NAME
and token.string == self.function_name
Expand Down Expand Up @@ -281,8 +291,13 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.accumulator = ""

elif token.type == tokenize.OP and token.string == "->" and self.state[-1] == STATE_INIT:
if len(self.accumulator) == 0:
self.state.append(STATE_RETURN_VALUE)
else:
# ) is not directly followed by ->
self.reset()
return
self.accumulator = ""
self.state.append(STATE_RETURN_VALUE)

# ENDMAKER is necessary for python 3.4 and 3.5.
elif token.type in (tokenize.NEWLINE, tokenize.ENDMARKER) and self.state[-1] in (
Expand All @@ -305,6 +320,17 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.args = []
self.ret_type = "Any"
# Leave state as INIT.
elif (
token.type == tokenize.NAME
and self.state[-1] == STATE_ARGUMENT_LIST
and len(self.accumulator) != 0
and self.accumulator != "*"
and self.accumulator != "**"
):
# not the token after the argument list started
# special case for *args and **kwargs
self.reset()
return
else:
self.accumulator += token.string

Expand Down
92 changes: 92 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,27 @@ def test_infer_sig_from_docstring(self) -> None:
],
)

assert_equal(
infer_sig_from_docstring("\nfunc(*args)", "func"),
[FunctionSig(name="func", args=[ArgSig(name="*args")], ret_type="Any")],
)

assert_equal(
infer_sig_from_docstring("\nfunc(**kwargs)", "func"),
[FunctionSig(name="func", args=[ArgSig(name="**kwargs")], ret_type="Any")],
)

assert_equal(
infer_sig_from_docstring("\nfunc(*args, **kwargs)", "func"),
[
FunctionSig(
name="func",
args=[ArgSig(name="*args"), ArgSig(name="**kwargs")],
ret_type="Any",
)
],
)

def test_infer_sig_from_docstring_duplicate_args(self) -> None:
assert_equal(
infer_sig_from_docstring("\nfunc(x, x) -> str\nfunc(x, y) -> int", "func"),
Expand All @@ -399,6 +420,77 @@ def test_infer_sig_from_docstring_bad_indentation(self) -> None:
None,
)

def test_infer_sig_from_docstring_invalid_signature(self) -> None:

assert_equal(infer_sig_from_docstring("\nfunc() --> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(name1 name2) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(name1, name2 name3) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(name2, name3) -> None, None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(invalid::name) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(invalid: [type]) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(invalid: (type)) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(invalid: -type) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(invalid<name) -> None", "func"), [])

assert_equal(infer_sig_from_docstring("\nfunc(cpp::type name) -> None", "func"), [])
assert_equal(infer_sig_from_docstring("\nfunc(name) -> cpp::type", "func"), [])
assert_equal(infer_sig_from_docstring("\nvoid func(int name) {", "func"), [])
assert_equal(infer_sig_from_docstring("\nvoid func(std::vector<int> name)", "func"), [])

def test_infer_sig_from_docstring_multiple_overloads(self) -> None:
input = """
func(*args, **kwargs)
Overloaded function.

1. func(self: class_type) -> None

2. func(self: class_type, a: float, b: int) -> None
"""
assert_equal(
infer_sig_from_docstring(input, "func"),
[
FunctionSig(
name="func", args=[ArgSig(name="self", type="class_type")], ret_type="None"
),
FunctionSig(
name="func",
args=[
ArgSig(name="self", type="class_type"),
ArgSig(name="a", type="float"),
ArgSig(name="b", type="int"),
],
ret_type="None",
),
FunctionSig(
name="func",
args=[ArgSig(name="*args"), ArgSig(name="**kwargs")],
ret_type="Any",
),
],
)

def test_infer_sig_from_docstring_deeply_nested_types(self) -> None:
assert_equal(
infer_sig_from_docstring(
"\nfunc(name: dict[str, dict[str, list[tuple[int, float]]]]) -> None", "func"
),
[
FunctionSig(
name="func",
args=[ArgSig(name="name", type="dict[str,dict[str,list[tuple[int,float]]]]")],
ret_type="None",
)
],
)

def test_infer_arg_sig_from_anon_docstring(self) -> None:
assert_equal(
infer_arg_sig_from_anon_docstring("(*args, **kwargs)"),
Expand Down
Loading