Skip to content

Commit

Permalink
conforming to pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tikuma-lsuhsc committed Jun 22, 2024
1 parent c0c174b commit 2085f69
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 47 deletions.
33 changes: 22 additions & 11 deletions src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,20 +750,31 @@ def _inject_signature( # noqa: C901
if app.config.typehints_defaults:
formatted_default = format_default(app, default, annotation is not None)
if formatted_default:
if app.config.typehints_defaults.endswith("after"):
type_annotation = _append_default(app, lines, insert_index, type_annotation, formatted_default)

# advance index to the end of the :param: paragraph
nlines = len(lines)
i = insert_index
while i < nlines and (lines[i + 1] == "" or lines[i + 1].startswith(" ")):
i = i + 1

lines[i] += formatted_default
lines.insert(insert_index, type_annotation)

else: # add to last param doc line
type_annotation += formatted_default

lines.insert(insert_index, type_annotation)
def _append_default(
app: Sphinx, lines: list[str], insert_index: int, type_annotation: str, formatted_default: str
) -> str:
if app.config.typehints_defaults.endswith("after"):
# advance the index to the end of the :param: paragraphs
# (terminated by a line with no indentation)
# append default to the last nonempty line
nlines = len(lines)
i = insert_index + 1
j = insert_index # last nonempty line
while i < nlines and (not lines[i] or lines[i].startswith(" ")):
if lines[i]:
j = i
i += 1
lines[j] += formatted_default

else: # add to last param doc line
type_annotation += formatted_default

return type_annotation


@dataclass
Expand Down
53 changes: 17 additions & 36 deletions tests/test_integration_issue_384.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from pathlib import Path
from textwrap import dedent, indent
from typing import TYPE_CHECKING, Any, Callable, Literal, NewType, TypeVar # no type comments
from typing import TYPE_CHECKING, Any, Callable, NewType, TypeVar # no type comments

import pytest

Expand Down Expand Up @@ -36,61 +36,42 @@ def dec(val: T) -> T:

@expected(
"""\
mod.function(x=5)
mod.function(x=5, y=10, z=15)
Function docstring.
Parameters:
**x** ("int") -- optional specifier for how to handle complex
data types. See "ivy.func_wrapper.handle_complex_input" for more
detail.
Returns:
something
* **x** ("int") -- optional specifier line 2 (default: "5")
Return type:
bytes
""",
)
def function1(x: int, y: int, z: int) -> str: # noqa: ARG001
"""
Function docstring.
* **y** ("int") --
:param x: optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
:param y: another optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
another optional line 4
:param z: yet another optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
:return: something
:rtype: bytes
"""
second paragraph for y (default: "10")
@expected(
"""\
mod.function(x=5)
Function docstring.
* **z** ("int") -- yet another optional s line 6 (default: "15")
Parameters:
**x** ("int") -- optional specifier for how to handle complex
data types. See "ivy.func_wrapper.handle_complex_input" for more
detail.
Returns:
something
Return type:
bytes
""",
)
def function(x: int = 5, y: int = 10, z: int = 5) -> str: # noqa: ARG001
def function(x: int = 5, y: int = 10, z: int = 15) -> str: # noqa: ARG001
"""
Function docstring.
:param x: optional specifier
:param x: optional specifier
line 2
:param y: another optional
line 4
second paragraph for y
:param z: yet another optional s
line 6
:return: something
:rtype: bytes
Expand Down

0 comments on commit 2085f69

Please sign in to comment.