File: test_integration_issue_347.py

package info (click to toggle)
sphinx-autodoc-typehints 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 684 kB
  • sloc: python: 5,081; makefile: 3
file content (145 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from __future__ import annotations

import sys
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
from conftest import normalize_sphinx_text

numpydoc = pytest.importorskip("numpydoc")

if TYPE_CHECKING:
    from io import StringIO

    from sphinx.testing.util import SphinxTestApp


def simple_func(x: int, y: str) -> str:
    """
    Do something simple.

    Parameters
    ----------
    x : int
        The x value.
    y : str
        The y value.

    Returns
    -------
    result : str
        The combined result.
    """
    return f"{x}{y}"  # pragma: no cover


def raises_func(x: int) -> None:
    """
    Raise on bad input.

    Parameters
    ----------
    x : int
        The input value.

    Raises
    ------
    ValueError
        If x is negative.
    TypeError
        If x is not an integer.
    """


def multi_return(x: int) -> tuple[str, int]:
    """
    Return multiple values.

    Parameters
    ----------
    x : int
        The input.

    Returns
    -------
    name : str
        The name.
    value : int
        The value.
    """
    return str(x), x  # pragma: no cover


def no_params() -> str:
    """
    Function with no parameters.

    Returns
    -------
    str
        A greeting.
    """
    return "hello"  # pragma: no cover


def _build_and_get_output(
    app: SphinxTestApp,
    status: StringIO,
    monkeypatch: pytest.MonkeyPatch,
    func_name: str,
) -> str:
    template = f"""\
.. autofunction:: mod_numpy.{func_name}
"""
    (Path(app.srcdir) / "index.rst").write_text(template)
    monkeypatch.setitem(sys.modules, "mod_numpy", sys.modules[__name__])
    app.build()
    assert "build succeeded" in status.getvalue()
    return normalize_sphinx_text((Path(app.srcdir) / "_build/text/index.txt").read_text())


@pytest.mark.sphinx("text", testroot="issue_347")
def test_simple_params_and_return(
    app: SphinxTestApp,
    status: StringIO,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    result = _build_and_get_output(app, status, monkeypatch, "simple_func")
    assert "Parameters:" in result
    assert "**x** (" in result
    assert "**y** (" in result
    assert "Return type:" in result


@pytest.mark.sphinx("text", testroot="issue_347")
def test_raises_section(
    app: SphinxTestApp,
    status: StringIO,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    result = _build_and_get_output(app, status, monkeypatch, "raises_func")
    assert "Parameters:" in result
    assert "Raises:" in result
    assert "ValueError" in result
    assert "TypeError" in result


@pytest.mark.sphinx("text", testroot="issue_347")
def test_multi_return(
    app: SphinxTestApp,
    status: StringIO,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    result = _build_and_get_output(app, status, monkeypatch, "multi_return")
    assert "Return type:" in result


@pytest.mark.sphinx("text", testroot="issue_347")
def test_no_params(
    app: SphinxTestApp,
    status: StringIO,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    result = _build_and_get_output(app, status, monkeypatch, "no_params")
    assert "Return type:" in result