Skip to content

Commit

Permalink
refactor: Use keyword only arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 29, 2021
1 parent 1b25670 commit e853fe9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Argument:
def __init__(
self,
name: str,
*,
annotation: str | None = None,
kind: ParameterKind | None = None,
default: str | None = None,
Expand Down
6 changes: 3 additions & 3 deletions src/griffe/docstrings/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DocstringElement:
description: The element description.
"""

def __init__(self, description: str, annotation: str | None = None) -> None:
def __init__(self, *, description: str, annotation: str | None = None) -> None:
"""Initialize the element.
Arguments:
Expand Down Expand Up @@ -101,7 +101,7 @@ class DocstringNamedElement(DocstringElement):
value: The element value, as a string, if any.
"""

def __init__(self, name: str, description: str, annotation: str | None = None, value: str | None = None) -> None:
def __init__(self, name: str, *, description: str, annotation: str | None = None, value: str | None = None) -> None:
"""Initialize the element.
Arguments:
Expand All @@ -110,7 +110,7 @@ def __init__(self, name: str, description: str, annotation: str | None = None, v
annotation: The element annotation, if any.
value: The element value, as a string.
"""
super().__init__(description, annotation)
super().__init__(description=description, annotation=annotation)
self.name: str = name
self.value: str | None = value

Expand Down
12 changes: 9 additions & 3 deletions src/griffe/docstrings/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def read_raises_section(docstring: Docstring, offset: int) -> tuple[DocstringSec
except ValueError:
warn(docstring, index, f"Failed to get 'exception: description' pair from '{exception_line}'")
else:
exceptions.append(DocstringException(annotation, description.lstrip(" ")))
exceptions.append(DocstringException(annotation=annotation, description=description.lstrip(" ")))

if exceptions:
return DocstringSection(DocstringSectionKind.raises, exceptions), index
Expand Down Expand Up @@ -361,7 +361,10 @@ def read_returns_section(docstring: Docstring, offset: int) -> tuple[DocstringSe
if annotation is None:
warn(docstring, index, "No return type/annotation in docstring/signature")

return DocstringSection(DocstringSectionKind.returns, DocstringReturn(annotation, description)), index
return (
DocstringSection(DocstringSectionKind.returns, DocstringReturn(annotation=annotation, description=description)),
index,
)


def read_yields_section(docstring: Docstring, offset: int) -> tuple[DocstringSection | None, int]:
Expand Down Expand Up @@ -401,7 +404,10 @@ def read_yields_section(docstring: Docstring, offset: int) -> tuple[DocstringSec
if annotation is None:
warn(docstring, index, "No yield type/annotation in docstring/signature")

return DocstringSection(DocstringSectionKind.yields, DocstringYield(annotation, description)), index
return (
DocstringSection(DocstringSectionKind.yields, DocstringYield(annotation=annotation, description=description)),
index,
)


def read_examples_section(docstring: Docstring, offset: int) -> tuple[DocstringSection | None, int]: # noqa: WPS231
Expand Down
4 changes: 2 additions & 2 deletions src/griffe/docstrings/rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def _read_exception(docstring: Docstring, offset: int, parsed_values: ParsedValu

if len(parsed_directive.directive_parts) == 2:
ex_type = parsed_directive.directive_parts[1]
parsed_values.exceptions.append(DocstringException(ex_type, parsed_directive.value))
parsed_values.exceptions.append(DocstringException(annotation=ex_type, description=parsed_directive.value))
else:
warn(docstring, 0, f"Failed to parse exception directive from '{parsed_directive.line}'")

Expand Down Expand Up @@ -353,7 +353,7 @@ def _read_return(docstring: Docstring, offset: int, parsed_values: ParsedValues)
warn(docstring, 0, f"No return type or annotation at '{parsed_directive.line}'")
annotation = None

parsed_values.return_value = DocstringReturn(annotation, parsed_directive.value)
parsed_values.return_value = DocstringReturn(annotation=annotation, description=parsed_directive.value)

return parsed_directive.next_index

Expand Down
21 changes: 17 additions & 4 deletions src/griffe/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,18 @@ def visit_FunctionDef(self, node) -> None: # noqa: WPS231
for (arg, kind), default in args_kinds_defaults:
annotation = _get_annotation(arg.annotation)
default = _get_argument_default(default, self.filepath)
arguments.add(Argument(arg.arg, annotation, kind, default))
arguments.add(Argument(arg.arg, annotation=annotation, kind=kind, default=default))

if node.args.vararg:
annotation = _get_annotation(node.args.vararg.annotation)
arguments.add(Argument(f"*{node.args.vararg.arg}", annotation, inspect.Parameter.VAR_POSITIONAL, None))
arguments.add(
Argument(
f"*{node.args.vararg.arg}",
annotation=annotation,
kind=inspect.Parameter.VAR_POSITIONAL,
default=None,
)
)

# TODO: probably some optimisations to do here
kwargs_defaults = reversed(
Expand All @@ -223,11 +230,17 @@ def visit_FunctionDef(self, node) -> None: # noqa: WPS231
for kwarg, default in kwargs_defaults: # noqa: WPS440
annotation = _get_annotation(kwarg.annotation)
default = _get_argument_default(default, self.filepath)
arguments.add(Argument(kwarg.arg, annotation, inspect.Parameter.KEYWORD_ONLY, default))
arguments.add(
Argument(kwarg.arg, annotation=annotation, kind=inspect.Parameter.KEYWORD_ONLY, default=default)
)

if node.args.kwarg:
annotation = _get_annotation(node.args.kwarg.annotation)
arguments.add(Argument(f"**{node.args.kwarg.arg}", annotation, inspect.Parameter.VAR_KEYWORD, None))
arguments.add(
Argument(
f"**{node.args.kwarg.arg}", annotation=annotation, kind=inspect.Parameter.VAR_KEYWORD, default=None
)
)

function = Function(
name=node.name,
Expand Down

0 comments on commit e853fe9

Please sign in to comment.