Skip to content

Commit e7d4085

Browse files
committed
Strip * in parameter names of *args & **kwargs
1 parent d141a03 commit e7d4085

3 files changed

Lines changed: 55 additions & 16 deletions

File tree

src/docstub/_docstrings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ def parameters(self) -> dict[str, Annotation]:
534534
continue
535535

536536
annotation = self._doctype_to_annotation(param.type, ds_line=ds_line)
537-
annotated_params[param.name] = annotation
537+
name = param.name.lstrip("*") # normalize *args & **kwargs
538+
annotated_params[name] = annotation
538539

539540
return annotated_params
540541

tests/test_docstrings.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ def test_empty_docstring(self):
195195
def test_parameters(self, doctype, expected):
196196
docstring = dedent(
197197
f"""
198-
Parameters
199-
----------
200-
a : {doctype}
201-
b :
202-
"""
198+
Parameters
199+
----------
200+
a : {doctype}
201+
b :
202+
"""
203203
)
204204
transformer = DoctypeTransformer()
205205
annotations = DocstringAnnotations(docstring, transformer=transformer)
@@ -216,11 +216,11 @@ def test_parameters(self, doctype, expected):
216216
def test_returns(self, doctypes, expected):
217217
docstring = dedent(
218218
"""
219-
Returns
220-
-------
221-
a : {}
222-
b : {}
223-
"""
219+
Returns
220+
-------
221+
a : {}
222+
b : {}
223+
"""
224224
).format(*doctypes)
225225
transformer = DoctypeTransformer()
226226
annotations = DocstringAnnotations(docstring, transformer=transformer)
@@ -229,11 +229,11 @@ def test_returns(self, doctypes, expected):
229229
def test_duplicate_parameters(self, caplog):
230230
docstring = dedent(
231231
"""
232-
Parameters
233-
----------
234-
a : int
235-
a : str
236-
"""
232+
Parameters
233+
----------
234+
a : int
235+
a : str
236+
"""
237237
)
238238
transformer = DoctypeTransformer()
239239
annotations = DocstringAnnotations(docstring, transformer=transformer)
@@ -252,3 +252,19 @@ def test_duplicate_returns(self, caplog):
252252
transformer = DoctypeTransformer()
253253
annotations = DocstringAnnotations(docstring, transformer=transformer)
254254
assert annotations.returns.value == "int"
255+
256+
def test_args_kwargs(self):
257+
docstring = dedent(
258+
"""
259+
Parameters
260+
----------
261+
*args : int
262+
**kwargs : str
263+
"""
264+
)
265+
transformer = DoctypeTransformer()
266+
annotations = DocstringAnnotations(docstring, transformer=transformer)
267+
assert "args" in annotations.parameters
268+
assert "*args" not in annotations.parameters
269+
assert "kwargs" in annotations.parameters
270+
assert "**kargs" not in annotations.parameters

tests/test_stubs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,25 @@ class Foo:
430430
transformer = Py2StubTransformer()
431431
result = transformer.python_to_stub(source)
432432
assert expected == result
433+
434+
def test_args_kwargs(self):
435+
# Unpack and TypedDict (PEP 692) are not yet considered / supported
436+
source = dedent(
437+
"""
438+
def foo(*args, **kwargs):
439+
'''
440+
Parameters
441+
----------
442+
*args : str
443+
**kwargs : int
444+
'''
445+
"""
446+
)
447+
expected = dedent(
448+
"""
449+
def foo(*args: str, **kwargs: int) -> None: ...
450+
"""
451+
)
452+
transformer = Py2StubTransformer()
453+
result = transformer.python_to_stub(source)
454+
assert expected == result

0 commit comments

Comments
 (0)