Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 44 additions & 7 deletions mypy/stubdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def __init__(self, function_name: str) -> None:
self.ret_type = "Any"
self.found = False
self.args: list[ArgSig] = []
self.pos_only: int | None = None
self.keyword_only: int | None = None
# Valid signatures found so far.
self.signatures: list[FunctionSig] = []

Expand Down Expand Up @@ -252,15 +254,32 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.arg_type = self.accumulator
self.state.pop()
elif self.state[-1] == STATE_ARGUMENT_LIST:
self.arg_name = self.accumulator
if not (
token.string == ")" and self.accumulator.strip() == ""
) and not _ARG_NAME_RE.match(self.arg_name):
# Invalid argument name.
self.reset()
return
if self.accumulator == "*":
if self.keyword_only is not None:
# Error condition: cannot have * twice
self.reset()
return
self.keyword_only = len(self.args)
self.accumulator = ""
else:
self.arg_name = self.accumulator
if not (
token.string == ")" and self.accumulator.strip() == ""
) and not _ARG_NAME_RE.match(self.arg_name):
# Invalid argument name.
self.reset()
return

if token.string == ")":
if (
self.state[-1] == STATE_ARGUMENT_LIST
and self.keyword_only is not None
and self.keyword_only == len(self.args)
and not self.arg_name
):
# Error condition: * must be followed by arguments
self.reset()
return
self.state.pop()

# arg_name is empty when there are no args. e.g. func()
Expand All @@ -280,6 +299,24 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
self.arg_type = None
self.arg_default = None
self.accumulator = ""
elif (
token.type == tokenize.OP
and (token.string in {"*", "/"})
and self.state[-1] == STATE_ARGUMENT_LIST
):
if token.string == "/":
if self.pos_only is not None or self.keyword_only is not None or not self.args:
# Error cases:
# - / shows up more than once
# - / shows up after *
# - / shows up before any arguments
self.reset()
return
self.pos_only = len(self.args)
self.state.append(STATE_ARGUMENT_TYPE)
self.accumulator = ""
else:
self.accumulator = "*"

elif token.type == tokenize.OP and token.string == "->" and self.state[-1] == STATE_INIT:
self.accumulator = ""
Expand Down
95 changes: 95 additions & 0 deletions mypy/test/teststubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,101 @@ def test_infer_sig_from_docstring_bad_indentation(self) -> None:
None,
)

def test_infer_sig_from_docstring_args_kwargs(self) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context: This behavior is not actually changing, but an intermediate version of the code would have failed these tests, and I only noticed this because the pybind11 stubgen tests started failing, so I figured I would add tests for this proactively.

assert_equal(
infer_sig_from_docstring("func(*args, **kwargs) -> int", "func"),
[
FunctionSig(
name="func",
args=[ArgSig(name="*args"), ArgSig(name="**kwargs")],
ret_type="int",
)
],
)

assert_equal(
infer_sig_from_docstring("func(*args) -> int", "func"),
[FunctionSig(name="func", args=[ArgSig(name="*args")], ret_type="int")],
)

assert_equal(
infer_sig_from_docstring("func(**kwargs) -> int", "func"),
[FunctionSig(name="func", args=[ArgSig(name="**kwargs")], ret_type="int")],
)

def test_infer_sig_from_docstring_positional_only_arguments(self) -> None:
assert_equal(
infer_sig_from_docstring("func(self, /) -> str", "func"),
[FunctionSig(name="func", args=[ArgSig(name="self")], ret_type="str")],
)

assert_equal(
infer_sig_from_docstring("func(self, x, /) -> str", "func"),
[
FunctionSig(
name="func", args=[ArgSig(name="self"), ArgSig(name="x")], ret_type="str"
)
],
)

assert_equal(
infer_sig_from_docstring("func(x, /, y) -> int", "func"),
[FunctionSig(name="func", args=[ArgSig(name="x"), ArgSig(name="y")], ret_type="int")],
)

def test_infer_sig_from_docstring_keyword_only_arguments(self) -> None:
assert_equal(
infer_sig_from_docstring("func(*, x) -> str", "func"),
[FunctionSig(name="func", args=[ArgSig(name="x")], ret_type="str")],
)

assert_equal(
infer_sig_from_docstring("func(x, *, y) -> str", "func"),
[FunctionSig(name="func", args=[ArgSig(name="x"), ArgSig(name="y")], ret_type="str")],
)

assert_equal(
infer_sig_from_docstring("func(*, x, y) -> str", "func"),
[FunctionSig(name="func", args=[ArgSig(name="x"), ArgSig(name="y")], ret_type="str")],
)

def test_infer_sig_from_docstring_pos_only_and_keyword_only_arguments(self) -> None:
assert_equal(
infer_sig_from_docstring("func(x, /, *, y) -> str", "func"),
[FunctionSig(name="func", args=[ArgSig(name="x"), ArgSig(name="y")], ret_type="str")],
)

assert_equal(
infer_sig_from_docstring("func(x, /, y, *, z) -> str", "func"),
[
FunctionSig(
name="func",
args=[ArgSig(name="x"), ArgSig(name="y"), ArgSig(name="z")],
ret_type="str",
)
],
)

def test_infer_sig_from_docstring_pos_only_and_keyword_only_arguments_errors(self) -> None:
# / as first argument
assert_equal(infer_sig_from_docstring("func(/, x) -> str", "func"), [])

# * as last argument
assert_equal(infer_sig_from_docstring("func(x, *) -> str", "func"), [])

# / after *
assert_equal(infer_sig_from_docstring("func(x, *, /, y) -> str", "func"), [])

# Two /
assert_equal(infer_sig_from_docstring("func(x, /, /, *, y) -> str", "func"), [])

assert_equal(infer_sig_from_docstring("func(x, /, y, /, *, z) -> str", "func"), [])

# Two *
assert_equal(infer_sig_from_docstring("func(x, /, *, *, y) -> str", "func"), [])

assert_equal(infer_sig_from_docstring("func(x, /, *, y, *, z) -> str", "func"), [])

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