summaryrefslogtreecommitdiff
path: root/numpydoc/tests/test_xref.py
blob: b4b3125273dd55704b96afbdaa34875bd2d395a9 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import pytest
from numpydoc.xref import make_xref, DEFAULT_LINKS

# Use the default numpydoc link mapping
xref_aliases = DEFAULT_LINKS


# Comes mainly from numpy
data = r"""
(...) array_like, float, optional
(...) :term:`numpy:array_like`, :class:`python:float`, optional

(2,) ndarray
(2,) :obj:`ndarray <numpy.ndarray>`

(...,M,N) array_like
(...,M,N) :term:`numpy:array_like`

(..., M, N) array_like
(..., :obj:`M`, :obj:`N`) :term:`numpy:array_like`

(float, float), optional
(:class:`python:float`, :class:`python:float`), optional

1-D array or sequence
1-D :obj:`array <numpy.ndarray>` or :term:`python:sequence`

array of str or unicode-like
:obj:`array <numpy.ndarray>` of :class:`python:str` or unicode-like

array_like of float
:term:`numpy:array_like` of :class:`python:float`

bool or callable
:ref:`bool <python:bltin-boolean-values>` or :func:`python:callable`

int in [0, 255]
:class:`python:int` in [0, 255]

int or None, optional
:class:`python:int` or :data:`python:None`, optional

list of str or array_like
:class:`python:list` of :class:`python:str` or :term:`numpy:array_like`

sequence of array_like
:term:`python:sequence` of :term:`numpy:array_like`

str or pathlib.Path
:class:`python:str` or :obj:`pathlib.Path`

{'', string}, optional
{'', :class:`python:str`}, optional

{'C', 'F', 'A', or 'K'}, optional
{'C', 'F', 'A', or 'K'}, optional

{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
{'linear', 'lower', 'higher', 'midpoint', 'nearest'}

{False, True, 'greedy', 'optimal'}
{:data:`python:False`, :data:`python:True`, 'greedy', 'optimal'}

{{'begin', 1}, {'end', 0}}, {string, int}
{{'begin', 1}, {'end', 0}}, {:class:`python:str`, :class:`python:int`}

callable f'(x,*args)
:func:`python:callable` f'(x,*args)

callable ``fhess(x, *args)``, optional
:func:`python:callable` ``fhess(x, *args)``, optional

spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``)
:obj:`spmatrix` (format: ``csr``, ``bsr``, ``dia`` or coo``)

:ref:`strftime <strftime-strptime-behavior>`
:ref:`strftime <strftime-strptime-behavior>`

callable or :ref:`strftime <strftime-strptime-behavior>`
:func:`python:callable` or :ref:`strftime <strftime-strptime-behavior>`

callable or :ref:`strftime behavior <strftime-strptime-behavior>`
:func:`python:callable` or :ref:`strftime behavior <strftime-strptime-behavior>`

list(int)
:class:`python:list`\(:class:`python:int`)

list[int]
:class:`python:list`\[:class:`python:int`]

dict(str, int)
:class:`python:dict`\(:class:`python:str`, :class:`python:int`)

dict[str,  int]
:class:`python:dict`\[:class:`python:str`,  :class:`python:int`]

tuple(float, float)
:class:`python:tuple`\(:class:`python:float`, :class:`python:float`)

dict[tuple(str, str), int]
:class:`python:dict`\[:class:`python:tuple`\(:class:`python:str`, :class:`python:str`), :class:`python:int`]
"""  # noqa: E501

data_ignore_obj = r"""
(...) array_like, float, optional
(...) :term:`numpy:array_like`, :class:`python:float`, optional

(2,) ndarray
(2,) :obj:`ndarray <numpy.ndarray>`

(...,M,N) array_like
(...,M,N) :term:`numpy:array_like`

(..., M, N) array_like
(..., M, N) :term:`numpy:array_like`

(float, float), optional
(:class:`python:float`, :class:`python:float`), optional

1-D array or sequence
1-D :obj:`array <numpy.ndarray>` or :term:`python:sequence`

array of str or unicode-like
:obj:`array <numpy.ndarray>` of :class:`python:str` or unicode-like

array_like of float
:term:`numpy:array_like` of :class:`python:float`

bool or callable
:ref:`bool <python:bltin-boolean-values>` or :func:`python:callable`

int in [0, 255]
:class:`python:int` in [0, 255]

int or None, optional
:class:`python:int` or :data:`python:None`, optional

list of str or array_like
:class:`python:list` of :class:`python:str` or :term:`numpy:array_like`

sequence of array_like
:term:`python:sequence` of :term:`numpy:array_like`

str or pathlib.Path
:class:`python:str` or pathlib.Path

{'', string}, optional
{'', :class:`python:str`}, optional

{'C', 'F', 'A', or 'K'}, optional
{'C', 'F', 'A', or 'K'}, optional

{'linear', 'lower', 'higher', 'midpoint', 'nearest'}
{'linear', 'lower', 'higher', 'midpoint', 'nearest'}

{False, True, 'greedy', 'optimal'}
{:data:`python:False`, :data:`python:True`, 'greedy', 'optimal'}

{{'begin', 1}, {'end', 0}}, {string, int}
{{'begin', 1}, {'end', 0}}, {:class:`python:str`, :class:`python:int`}

callable f'(x,*args)
:func:`python:callable` f'(x,*args)

callable ``fhess(x, *args)``, optional
:func:`python:callable` ``fhess(x, *args)``, optional

spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``)
spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``)

:ref:`strftime <strftime-strptime-behavior>`
:ref:`strftime <strftime-strptime-behavior>`

callable or :ref:`strftime <strftime-strptime-behavior>`
:func:`python:callable` or :ref:`strftime <strftime-strptime-behavior>`

callable or :ref:`strftime behavior <strftime-strptime-behavior>`
:func:`python:callable` or :ref:`strftime behavior <strftime-strptime-behavior>`

list(int)
:class:`python:list`\(:class:`python:int`)

list[int]
:class:`python:list`\[:class:`python:int`]

dict(str, int)
:class:`python:dict`\(:class:`python:str`, :class:`python:int`)

dict[str,  int]
:class:`python:dict`\[:class:`python:str`,  :class:`python:int`]

tuple(float, float)
:class:`python:tuple`\(:class:`python:float`, :class:`python:float`)

dict[tuple(str, str), int]
:class:`python:dict`\[:class:`python:tuple`\(:class:`python:str`, :class:`python:str`), :class:`python:int`]
"""  # noqa: E501

xref_ignore = {"or", "in", "of", "default", "optional"}


@pytest.mark.parametrize(
    ("param_type", "expected_result"),
    [tuple(s.split("\n")) for s in data.strip().split("\n\n")],
)
def test_make_xref(param_type, expected_result):
    assert make_xref(param_type, xref_aliases, xref_ignore) == expected_result


@pytest.mark.parametrize(
    ("param_type", "expected_result"),
    [tuple(s.split("\n")) for s in data_ignore_obj.strip().split("\n\n")],
)
def test_make_xref_ignore_unknown(param_type, expected_result):
    assert make_xref(param_type, xref_aliases, xref_ignore="all") == expected_result


def test_xref_ignore_is_all():
    with pytest.raises(TypeError, match="must be a set or 'all'"):
        make_xref("array_like", xref_aliases, xref_ignore="foo")