diff --git a/pydocstringformatter/_formatting/base.py b/pydocstringformatter/_formatting/base.py index a00cbaa0..de0fdabc 100644 --- a/pydocstringformatter/_formatting/base.py +++ b/pydocstringformatter/_formatting/base.py @@ -278,7 +278,7 @@ def treat_string( if first_section: section[0] = section[0].lstrip() first_section = False - elif not section[0][0].isspace(): + elif section[0] and not section[0][0].isspace(): section[0] = f"{' ' * indent_length:s}{section[0]:s}" # Rejoin sections diff --git a/pydocstringformatter/_formatting/formatters_numpydoc.py b/pydocstringformatter/_formatting/formatters_numpydoc.py index 7f063734..81f8e776 100644 --- a/pydocstringformatter/_formatting/formatters_numpydoc.py +++ b/pydocstringformatter/_formatting/formatters_numpydoc.py @@ -121,7 +121,10 @@ def treat_sections( # summary. They have neither a section header nor the # line of hyphens after it. indent_length = section_lines[1].index("-") - section_lines[1] = " " * indent_length + "-" * len(section_name) + + if section_name: + section_lines[1] = " " * indent_length + "-" * len(section_name) + if first_section: # If the second line were not hyphens, the section name # would be summary. This assumes triple quotes, but that diff --git a/tests/data/format/multi_style/numpydoc_parameter_spacing.args b/tests/data/format/multi_style/numpydoc_parameter_spacing.args new file mode 100644 index 00000000..ca785ac8 --- /dev/null +++ b/tests/data/format/multi_style/numpydoc_parameter_spacing.args @@ -0,0 +1,2 @@ +--style=numpydoc +--style=pep257 diff --git a/tests/data/format/multi_style/numpydoc_parameter_spacing.py b/tests/data/format/multi_style/numpydoc_parameter_spacing.py new file mode 100644 index 00000000..619d27cf --- /dev/null +++ b/tests/data/format/multi_style/numpydoc_parameter_spacing.py @@ -0,0 +1,63 @@ +"""Example module for numpydoc docstring style. +References +----- +NumPy docstring style guide: +https://numpydoc.readthedocs.io/en/latest/format.html#documenting-modules""" +import math + +EULER_NUMBER = math.e +"""Euler's number. + +Not related to Euler's constant (sometimes called the Euler-Mascheroni +constant. +References +----- +E (mathematical constant) +https://en.wikipedia.org/wiki/E_(mathematical_constant) +Notes +--- +It is the limit of ``(1 + 1/n)**n`` as n approaches infinity, so it +is used in the equation for continuously-compouned interest. + +It is also the sum of the reciprocals of the whole numbers starting +with zero, which is related to some calculus-related properties +mathemeticians find elegant. +""" + + +def sincos(theta): + """Returns + ---- + sin: float + the sine of theta + cos: float + the cosine of theta + Raises + --- + TypeError + If `theta` is not a float. + Parameters + ----- + theta: float + the angle at which to calculate the sine and cosine. +""" + return math.sin(theta), math.cos(theta) + + +def fibbonacci(): + """Generate the Fibonacci sequence. + + Each term is the sum of the two previous; conventionally starts + with two ones. + References + ----- + Fibonacci numbers + https://en.wikipedia.org/wiki/Fibonacci_number + Yields + --- + int""" + curr = 1 + last = 0 + while True: + yield curr + curr, last = curr + last, curr diff --git a/tests/data/format/multi_style/numpydoc_parameter_spacing.py.out b/tests/data/format/multi_style/numpydoc_parameter_spacing.py.out new file mode 100644 index 00000000..802978c1 --- /dev/null +++ b/tests/data/format/multi_style/numpydoc_parameter_spacing.py.out @@ -0,0 +1,74 @@ +"""Example module for numpydoc docstring style. + +References +---------- +NumPy docstring style guide: +https://numpydoc.readthedocs.io/en/latest/format.html#documenting-modules +""" +import math + +EULER_NUMBER = math.e +"""Euler's number. + +Not related to Euler's constant (sometimes called the Euler-Mascheroni +constant. + +Notes +----- +It is the limit of ``(1 + 1/n)**n`` as n approaches infinity, so it +is used in the equation for continuously-compouned interest. + +It is also the sum of the reciprocals of the whole numbers starting +with zero, which is related to some calculus-related properties +mathemeticians find elegant. + +References +---------- +E (mathematical constant) +https://en.wikipedia.org/wiki/E_(mathematical_constant) +""" + + +def sincos(theta): + """Returns. + + Parameters + ---------- + theta : float + the angle at which to calculate the sine and cosine. + + Raises + ------ + TypeError + If `theta` is not a float. + + + ---- + sin: float + the sine of theta + cos: float + the cosine of theta + """ + return math.sin(theta), math.cos(theta) + + +def fibbonacci(): + """Generate the Fibonacci sequence. + + Each term is the sum of the two previous; conventionally starts + with two ones. + + Yields + ------ + int + + References + ---------- + Fibonacci numbers + https://en.wikipedia.org/wiki/Fibonacci_number + """ + curr = 1 + last = 0 + while True: + yield curr + curr, last = curr + last, curr