Skip to content

Commit

Permalink
Fix missing signatures for docstrings in Markdown (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
staticf0x authored Oct 11, 2023
1 parent d339272 commit 4f6aa20
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
1 change: 0 additions & 1 deletion pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ def format_docstring(
if markup_kind == "markdown":
try:
value = docstring_to_markdown.convert(contents)
return {"kind": "markdown", "value": value}
except docstring_to_markdown.UnknownFormatError:
# try to escape the Markdown syntax instead:
value = escape_markdown(contents)
Expand Down
51 changes: 51 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest import mock

from flaky import flaky
from docstring_to_markdown import UnknownFormatError

from pylsp import _utils
from pylsp.python_lsp import PythonLSPServer, start_io_lang_server
Expand Down Expand Up @@ -154,3 +155,53 @@ def test_clip_column():
assert _utils.clip_column(2, ["123\n", "123"], 0) == 2
assert _utils.clip_column(3, ["123\n", "123"], 0) == 3
assert _utils.clip_column(4, ["123\n", "123"], 1) == 3


@mock.patch("docstring_to_markdown.convert")
def test_format_docstring_valid_rst_signature(mock_convert):
"""Test that a valid RST docstring includes the function signature."""
docstring = """A function docstring.
Parameters
----------
a : str, something
"""

# Mock the return value to avoid depedency on the real thing
mock_convert.return_value = """A function docstring.
#### Parameters
- `a`: str, something
"""

markdown = _utils.format_docstring(
docstring,
"markdown",
["something(a: str) -> str"],
)["value"]

assert markdown.startswith(
_utils.wrap_signature("something(a: str) -> str"),
)


@mock.patch("docstring_to_markdown.convert", side_effect=UnknownFormatError)
def test_format_docstring_invalid_rst_signature(_):
"""Test that an invalid RST docstring includes the function signature."""
docstring = """A function docstring.
Parameters
----------
a : str, something
"""

markdown = _utils.format_docstring(
docstring,
"markdown",
["something(a: str) -> str"],
)["value"]

assert markdown.startswith(
_utils.wrap_signature("something(a: str) -> str"),
)

0 comments on commit 4f6aa20

Please sign in to comment.