Skip to content

Commit

Permalink
fix: fix parsing trailing comma after keyword remainder argument in f…
Browse files Browse the repository at this point in the history
…unction definition (before it would accidentally consider the comma as a positional argument)
  • Loading branch information
NiklasRosenstein committed Jul 18, 2022
1 parent 9a60937 commit 8dea126
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docspec-python/.changelog/_unreleased.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[entries]]
id = "9698224d-eca1-41c5-9cf5-c04d34a607fe"
type = "fix"
description = "fix parsing trailing comma after keyword remainder argument in function definition (before it would accidentally consider the comma as a positional argument)"
author = "@NiklasRosenstein"
2 changes: 2 additions & 0 deletions docspec-python/src/docspec_python/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ def parse_parameters(self, parameters):
elif node and node.type == token.DOUBLESTAR:
node = index.next()
result.append(self.parse_argument(node, Argument.Type.KEYWORD_REMAINDER, index))
argtype = Argument.Type.KEYWORD_ONLY
index.advance()

else:
result.append(self.parse_argument(node, argtype, index))
Expand Down
43 changes: 41 additions & 2 deletions docspec-python/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
loc = Location('<string>', 0, None)


def mkfunc(name: str, docstring: Optional[str], lineno: int, args: List[Argument], modifiers: Optional[List[str]] = None) -> Function:
def mkfunc(name: str, docstring: Optional[str], lineno: int, args: List[Argument], modifiers: Optional[List[str]] = None, return_type: Optional[str] = None) -> Function:
return Function(
name=name,
location=loc,
docstring=Docstring(Location(get_callsite().code_name, lineno), docstring) if docstring else None,
modifiers=modifiers,
args=args,
return_type=None,
return_type=return_type,
decorations=[],
)

Expand Down Expand Up @@ -433,3 +433,42 @@ def test_format_arglist():
Argument(loc, 'tqdm_kwargs', Argument.Type.KEYWORD_REMAINDER, None, None, None),
], ['async'])
assert format_arglist(func.args, True) == 'cls, *fs, loop=None, timeout=None, total=None, **tqdm_kwargs'



@docspec_test()
def test_funcdef_with_trailing_comma():
"""
def build_docker_image(
name: str = "buildDocker",
default: bool = False,
dockerfile: str = "docker/release.Dockerfile",
project: Project | None = None,
auth: dict[str, tuple[str, str]] | None = None,
secrets: dict[str, str] | None = None,
image_qualifier: str | None = None,
platforms: list[str] | None = None,
**kwargs: Any,
) -> Task:
pass
"""

return [
mkfunc(
"build_docker_image",
None,
0,
[
Argument(loc, "name", Argument.Type.POSITIONAL, None, "str", '"buildDocker"'),
Argument(loc, "default", Argument.Type.POSITIONAL, None, "bool", 'False'),
Argument(loc, "dockerfile", Argument.Type.POSITIONAL, None, "str", '"docker/release.Dockerfile"'),
Argument(loc, "project", Argument.Type.POSITIONAL, None, "Project | None", 'None'),
Argument(loc, "auth", Argument.Type.POSITIONAL, None, "dict[str, tuple[str, str]] | None", 'None'),
Argument(loc, "secrets", Argument.Type.POSITIONAL, None, "dict[str, str] | None", 'None'),
Argument(loc, "image_qualifier", Argument.Type.POSITIONAL, None, "str | None", 'None'),
Argument(loc, "platforms", Argument.Type.POSITIONAL, None, "list[str] | None", 'None'),
Argument(loc, "kwargs", Argument.Type.KEYWORD_REMAINDER, None, "Any", None),
],
return_type="Task"
),
]

0 comments on commit 8dea126

Please sign in to comment.