|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import inspect |
| 4 | +from typing import Any |
4 | 5 | from unittest.mock import MagicMock, create_autospec, patch |
5 | 6 |
|
| 7 | +import pytest |
6 | 8 | from conftest import make_docstring_app, make_sig_app |
7 | 9 | from sphinx.application import Sphinx |
8 | 10 | from sphinx.config import Config |
@@ -309,6 +311,52 @@ def func(fp: int) -> None: ... |
309 | 311 | assert "int" in type_lines[0] |
310 | 312 |
|
311 | 313 |
|
| 314 | +def _trailing_underscore_func(lambda_: float) -> None: ... |
| 315 | + |
| 316 | + |
| 317 | +def _trailing_underscore_starred_func(*args_: float) -> None: ... |
| 318 | + |
| 319 | + |
| 320 | +def _trailing_underscore_undocumented_func(x_: float) -> None: ... |
| 321 | + |
| 322 | + |
| 323 | +@pytest.mark.parametrize( |
| 324 | + ("func", "lines", "expected_param_line", "expected_type_name"), |
| 325 | + [ |
| 326 | + pytest.param( |
| 327 | + _trailing_underscore_func, |
| 328 | + [":param lambda_: description"], |
| 329 | + ":param lambda\\_: description", |
| 330 | + "lambda\\_", |
| 331 | + id="unescaped-line-rewritten", |
| 332 | + ), |
| 333 | + pytest.param( |
| 334 | + _trailing_underscore_starred_func, |
| 335 | + [":param \\*args_: description"], |
| 336 | + ":param \\*args_: description", |
| 337 | + "args_", |
| 338 | + id="starred-line-kept", |
| 339 | + ), |
| 340 | + pytest.param( |
| 341 | + _trailing_underscore_undocumented_func, |
| 342 | + [], |
| 343 | + ":param x\\_:", |
| 344 | + "x\\_", |
| 345 | + id="undocumented-escaped", |
| 346 | + ), |
| 347 | + ], |
| 348 | +) |
| 349 | +def test_process_docstring_trailing_underscore_param( |
| 350 | + func: Any, lines: list[str], expected_param_line: str, expected_type_name: str |
| 351 | +) -> None: |
| 352 | + """napoleon emits ``:param lambda_:`` unescaped by default; the param line must be rewritten to |
| 353 | + the escaped form and the type attached, except for forms that cannot be rewritten (issue #708).""" |
| 354 | + app = make_docstring_app(typehints_document_rtype=False, always_document_param_types=True) |
| 355 | + process_docstring(app, "function", "test.func", func, None, lines) |
| 356 | + assert expected_param_line in lines |
| 357 | + assert any(line.startswith(":type ") and expected_type_name in line and "float" in line for line in lines) |
| 358 | + |
| 359 | + |
312 | 360 | def test_process_signature_annotations_name_error() -> None: |
313 | 361 | """PEP 649 lazy annotations raising NameError must not propagate from process_signature.""" |
314 | 362 |
|
|
0 commit comments