Skip to content

Commit

Permalink
refactor: Set default values for Argument arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 29, 2021
1 parent 90c5956 commit d5cccaa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
16 changes: 13 additions & 3 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ class Argument:
default: The argument default, if any.
"""

def __init__(self, name: str, annotation: str | None, kind: ParameterKind, default: str | None) -> None:
def __init__(
self,
name: str,
annotation: str | None = None,
kind: ParameterKind | None = None,
default: str | None = None,
) -> None:
"""Initialize the argument.
Arguments:
Expand All @@ -153,7 +159,7 @@ def __init__(self, name: str, annotation: str | None, kind: ParameterKind, defau
"""
self.name: str = name
self.annotation: str | None = annotation
self.kind: ParameterKind = kind
self.kind: ParameterKind | None = kind
self.default: str | None = default

def as_dict(self, **kwargs) -> dict[str, Any]:
Expand All @@ -180,7 +186,11 @@ class Arguments:
"""

def __init__(self, *arguments: Argument) -> None:
"""Initialize the arguments container."""
"""Initialize the arguments container.
Arguments:
*arguments: The initial arguments to add to the container.
"""
self._arguments_list: list[Argument] = []
self._arguments_dict: dict[str, Argument] = {}
for argument in arguments:
Expand Down
10 changes: 5 additions & 5 deletions src/griffe/docstrings/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class DocstringElement:
description: The element description.
"""

def __init__(self, annotation: str | None, description: str) -> None:
def __init__(self, description: str, annotation: str | None = None) -> None:
"""Initialize the element.
Arguments:
annotation: The element annotation, if any.
description: The element description.
"""
self.annotation: str | None = annotation
self.description: str = description
self.annotation: str | None = annotation

def as_dict(self, **kwargs) -> dict[str, Any]:
"""Return this element's data as a dictionary.
Expand Down Expand Up @@ -101,16 +101,16 @@ class DocstringNamedElement(DocstringElement):
value: The element value, as a string, if any.
"""

def __init__(self, name: str, annotation: str | None, description: str, 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:
name: The element name.
annotation: The element annotation, if any.
description: The element description.
annotation: The element annotation, if any.
value: The element value, as a string.
"""
super().__init__(annotation, description)
super().__init__(description, annotation)
self.name: str = name
self.value: str | None = value

Expand Down
28 changes: 14 additions & 14 deletions tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def test_parse_without_annotations():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument(name="y", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument("x", kind=inspect.Parameter.POSITIONAL_ONLY),
Argument("y", kind=inspect.Parameter.POSITIONAL_ONLY),
),
),
)
Expand Down Expand Up @@ -222,8 +222,8 @@ def test_parse_with_annotations():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument(name="y", annotation="int", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, default=None),
Argument("x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY),
Argument("y", annotation="int", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD),
),
returns="int",
),
Expand Down Expand Up @@ -307,8 +307,8 @@ def test_parse_examples_sections():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument(name="y", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument("x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY),
Argument("y", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY),
),
returns="int",
),
Expand Down Expand Up @@ -443,8 +443,8 @@ def test_parse_types_in_docstring():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument(name="y", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument("x", kind=inspect.Parameter.POSITIONAL_ONLY),
Argument("y", kind=inspect.Parameter.POSITIONAL_ONLY),
),
returns=None,
),
Expand Down Expand Up @@ -490,9 +490,9 @@ def test_parse_optional_type_in_docstring():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default="1"),
Argument(name="y", annotation=None, kind=inspect.Parameter.POSITIONAL_ONLY, default="None"),
Argument(name="z", annotation=None, kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, default="None"),
Argument("x", kind=inspect.Parameter.POSITIONAL_ONLY, default="1"),
Argument("y", kind=inspect.Parameter.POSITIONAL_ONLY, default="None"),
Argument("z", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, default="None"),
),
returns=None,
),
Expand Down Expand Up @@ -540,8 +540,8 @@ def test_prefer_docstring_types_over_annotations():
parent=Function(
"func",
arguments=Arguments(
Argument(name="x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument(name="y", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY, default=None),
Argument("x", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY),
Argument("y", annotation="int", kind=inspect.Parameter.POSITIONAL_ONLY),
),
returns="int",
),
Expand Down Expand Up @@ -633,7 +633,7 @@ def test_parse_yields_section_with_return_annotation():
Integers.
"""

function = Function(name="func", returns="Iterator[int]")
function = Function("func", returns="Iterator[int]")
sections, warnings = parse(docstring, function)
assert len(sections) == 1
annotated = sections[0].value
Expand Down
46 changes: 22 additions & 24 deletions tests/test_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_parse__param_field__param_section():
)
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(sections[1].value[0], DocstringArgument(SOME_NAME, annotation=None, description=SOME_TEXT))
assert_argument_equal(sections[1].value[0], DocstringArgument(SOME_NAME, description=SOME_TEXT))


def test_parse__only_param_field__empty_markdown():
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_parse__all_param_names__param_section(param_directive_name):
)
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(sections[1].value[0], DocstringArgument(SOME_NAME, annotation=None, description=SOME_TEXT))
assert_argument_equal(sections[1].value[0], DocstringArgument(SOME_NAME, description=SOME_TEXT))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_parse__param_field_multi_line__param_section(docstring):
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(
sections[1].value[0],
DocstringArgument(SOME_NAME, annotation=None, description=f"{SOME_TEXT} {SOME_EXTRA_TEXT}"),
DocstringArgument(SOME_NAME, description=f"{SOME_TEXT} {SOME_EXTRA_TEXT}"),
)


Expand All @@ -226,7 +226,7 @@ def test_parse__param_field_for_function__param_section_with_kind():
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(
sections[1].value[0],
DocstringArgument(SOME_NAME, annotation=None, description=SOME_TEXT),
DocstringArgument(SOME_NAME, description=SOME_TEXT),
)


Expand Down Expand Up @@ -316,7 +316,7 @@ def test_parse__param_field_annotate_type__param_section_with_type():

sections, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation="str", kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", annotation="str", kind=None))),
)
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.arguments
Expand All @@ -340,7 +340,7 @@ def test_parse__param_field_no_matching_param__result_from_docstring():
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(
sections[1].value[0],
DocstringArgument("other", annotation=None, description=SOME_TEXT),
DocstringArgument("other", description=SOME_TEXT),
)


Expand All @@ -354,15 +354,13 @@ def test_parse__param_field_with_default__result_from_docstring():

sections, warnings = parse(
docstring,
parent=Function(
name="func", arguments=Arguments(Argument("foo", annotation=None, kind=None, default=repr("")))
),
parent=Function("func", arguments=Arguments(Argument("foo", kind=None, default=repr("")))),
)
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.arguments
assert_argument_equal(
sections[1].value[0],
DocstringArgument("foo", annotation=None, description=SOME_TEXT, value=repr("")),
DocstringArgument("foo", description=SOME_TEXT, value=repr("")),
)
assert not warnings

Expand Down Expand Up @@ -414,7 +412,7 @@ def test_parse__param_twice__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation=None, kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", kind=None))),
)
assert "Duplicate parameter entry for 'foo'" in warnings[0]

Expand All @@ -430,7 +428,7 @@ def test_parse__param_type_twice_doc__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation=None, kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", kind=None))),
)
assert "Duplicate parameter information for 'foo'" in warnings[0]

Expand All @@ -446,7 +444,7 @@ def test_parse__param_type_twice_type_directive_first__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation=None, kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", kind=None))),
)
assert "Duplicate parameter information for 'foo'" in warnings[0]

Expand All @@ -462,7 +460,7 @@ def test_parse__param_type_twice_annotated__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation="str", kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", annotation="str", kind=None))),
)
assert "Duplicate parameter information for 'foo'" in warnings[0]

Expand All @@ -478,7 +476,7 @@ def test_parse__param_type_no_type__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation="str", kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", annotation="str", kind=None))),
)
assert "Failed to get ':directive: value' pair from" in warnings[0]

Expand All @@ -494,7 +492,7 @@ def test_parse__param_type_no_name__error_message():

_, warnings = parse(
docstring,
parent=Function(name="func", arguments=Arguments(Argument("foo", annotation="str", kind=None, default=None))),
parent=Function("func", arguments=Arguments(Argument("foo", annotation="str", kind=None))),
)
assert "Failed to get parameter name from" in warnings[0]

Expand Down Expand Up @@ -527,7 +525,7 @@ def test_parse__attribute_field_multi_line__param_section(docstring):
assert sections[1].kind is DocstringSectionKind.attributes
assert_attribute_equal(
sections[1].value[0],
DocstringAttribute(SOME_NAME, annotation=None, description=f"{SOME_TEXT} {SOME_EXTRA_TEXT}"),
DocstringAttribute(SOME_NAME, description=f"{SOME_TEXT} {SOME_EXTRA_TEXT}"),
)
assert not warnings

Expand Down Expand Up @@ -557,7 +555,7 @@ def test_parse__all_attribute_names__param_section(attribute_directive_name):
assert sections[1].kind is DocstringSectionKind.attributes
assert_attribute_equal(
sections[1].value[0],
DocstringAttribute(SOME_NAME, annotation=None, description=SOME_TEXT),
DocstringAttribute(SOME_NAME, description=SOME_TEXT),
)
assert not warnings

Expand All @@ -570,12 +568,12 @@ def test_parse__class_attributes__attributes_section():
:var foo: {SOME_TEXT}
"""

sections, _ = parse(docstring, parent=Class(name="klass"))
sections, _ = parse(docstring, parent=Class("klass"))
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.attributes
assert_attribute_equal(
sections[1].value[0],
DocstringAttribute(SOME_NAME, annotation=None, description=SOME_TEXT),
DocstringAttribute(SOME_NAME, description=SOME_TEXT),
)


Expand All @@ -588,7 +586,7 @@ def test_parse__class_attributes_with_type__annotation_in_attributes_section():
:var foo: {SOME_TEXT}
"""

sections, _ = parse(docstring, parent=Class(name="klass"))
sections, _ = parse(docstring, parent=Class("klass"))
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.attributes
assert_attribute_equal(
Expand Down Expand Up @@ -721,7 +719,7 @@ def test_parse__return_directive_annotation__return_section_with_type():
:return: {SOME_TEXT}
"""

sections, _ = parse(docstring, parent=Function(name="func", returns="str"))
sections, _ = parse(docstring, parent=Function("func", returns="str"))
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.returns
assert_element_equal(
Expand All @@ -739,7 +737,7 @@ def test_parse__return_directive_annotation__prefer_return_directive():
:rtype: str
"""

sections, _ = parse(docstring, parent=Function(name="func", returns="int"))
sections, _ = parse(docstring, parent=Function("func", returns="int"))
assert len(sections) == 2
assert sections[1].kind is DocstringSectionKind.returns
assert_element_equal(
Expand Down Expand Up @@ -880,7 +878,7 @@ def test_parse__raise_no_name__error():
# :vartype E: float
# """

# module = Module(name="mod", filepath=None)
# module = Module("mod", filepath=None)
# module["A"] = Data("A") # annotation="int", value="0"
# module["B"] = Data("B") # annotation="str", value=repr("ŧ")
# module["C"] = Data("C") # annotation="bool", value="True"
Expand Down

0 comments on commit d5cccaa

Please sign in to comment.