Skip to content

Commit 6eb5932

Browse files
fixup! Support positional and keyword-only arguments
1 parent 80fb6d0 commit 6eb5932

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

mypy/stubdoc.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,21 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
254254
self.arg_type = self.accumulator
255255
self.state.pop()
256256
elif self.state[-1] == STATE_ARGUMENT_LIST:
257-
self.arg_name = self.accumulator
258-
if not (
259-
token.string == ")" and self.accumulator.strip() == ""
260-
) and not _ARG_NAME_RE.match(self.arg_name):
261-
# Invalid argument name.
262-
self.reset()
263-
return
257+
if self.accumulator == "*":
258+
if self.keyword_only is not None:
259+
# Error condition: cannot have * twice
260+
self.reset()
261+
return
262+
self.keyword_only = len(self.args)
263+
self.accumulator = ""
264+
else:
265+
self.arg_name = self.accumulator
266+
if not (
267+
token.string == ")" and self.accumulator.strip() == ""
268+
) and not _ARG_NAME_RE.match(self.arg_name):
269+
# Invalid argument name.
270+
self.reset()
271+
return
264272

265273
if token.string == ")":
266274
if (
@@ -305,13 +313,10 @@ def add_token(self, token: tokenize.TokenInfo) -> None:
305313
self.reset()
306314
return
307315
self.pos_only = len(self.args)
316+
self.state.append(STATE_ARGUMENT_TYPE)
317+
self.accumulator = ""
308318
else:
309-
if self.keyword_only is not None:
310-
# * is not allowed after *
311-
self.reset()
312-
return
313-
self.keyword_only = len(self.args)
314-
self.state.append(STATE_ARGUMENT_TYPE)
319+
self.accumulator = "*"
315320

316321
elif token.type == tokenize.OP and token.string == "->" and self.state[-1] == STATE_INIT:
317322
self.accumulator = ""

mypy/test/teststubgen.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,28 @@ def test_infer_sig_from_docstring_bad_indentation(self) -> None:
399399
None,
400400
)
401401

402+
def test_infer_sig_from_docstring_args_kwargs(self) -> None:
403+
assert_equal(
404+
infer_sig_from_docstring("func(*args, **kwargs) -> int", "func"),
405+
[
406+
FunctionSig(
407+
name="func",
408+
args=[ArgSig(name="*args"), ArgSig(name="**kwargs")],
409+
ret_type="int",
410+
)
411+
],
412+
)
413+
414+
assert_equal(
415+
infer_sig_from_docstring("func(*args) -> int", "func"),
416+
[FunctionSig(name="func", args=[ArgSig(name="*args")], ret_type="int")],
417+
)
418+
419+
assert_equal(
420+
infer_sig_from_docstring("func(**kwargs) -> int", "func"),
421+
[FunctionSig(name="func", args=[ArgSig(name="**kwargs")], ret_type="int")],
422+
)
423+
402424
def test_infer_sig_from_docstring_positional_only_arguments(self) -> None:
403425
assert_equal(
404426
infer_sig_from_docstring("func(self, /) -> str", "func"),

0 commit comments

Comments
 (0)