|
| 1 | +# -*- encoding:utf-8 -*- |
| 2 | +from __future__ import division, absolute_import, print_function |
| 3 | + |
| 4 | +from numpydoc.xref import make_xref_param_type |
| 5 | + |
| 6 | +xref_aliases = { |
| 7 | + # python |
| 8 | + 'sequence': ':term:`python:sequence`', |
| 9 | + 'iterable': ':term:`python:iterable`', |
| 10 | + 'string': 'str', |
| 11 | + # numpy |
| 12 | + 'array': '~numpy.array', |
| 13 | + 'dtype': 'numpy.dtype', |
| 14 | + 'ndarray': '~numpy.ndarray', |
| 15 | + 'matrix': 'numpy.matrix', |
| 16 | + 'array-like': ':term:`numpy:array_like`', |
| 17 | + 'array_like': ':term:`numpy:array_like`', |
| 18 | +} |
| 19 | + |
| 20 | +# Comes mainly from numpy |
| 21 | +data = """ |
| 22 | +(...) array_like, float, optional |
| 23 | +(...) :term:`numpy:array_like`, :xref_param_type:`float`, optional |
| 24 | +
|
| 25 | +(2,) ndarray |
| 26 | +(2,) :xref_param_type:`~numpy.ndarray` |
| 27 | +
|
| 28 | +(...,M,N) array_like |
| 29 | +(...,M,N) :term:`numpy:array_like` |
| 30 | +
|
| 31 | +(float, float), optional |
| 32 | +(:xref_param_type:`float`, :xref_param_type:`float`), optional |
| 33 | +
|
| 34 | +1-D array or sequence |
| 35 | +1-D :xref_param_type:`~numpy.array` or :term:`python:sequence` |
| 36 | +
|
| 37 | +array of str or unicode-like |
| 38 | +:xref_param_type:`~numpy.array` of :xref_param_type:`str` or unicode-like |
| 39 | +
|
| 40 | +array_like of float |
| 41 | +:term:`numpy:array_like` of :xref_param_type:`float` |
| 42 | +
|
| 43 | +bool or callable |
| 44 | +:xref_param_type:`bool` or :xref_param_type:`callable` |
| 45 | +
|
| 46 | +int in [0, 255] |
| 47 | +:xref_param_type:`int` in [0, 255] |
| 48 | +
|
| 49 | +int or None, optional |
| 50 | +:xref_param_type:`int` or :xref_param_type:`None`, optional |
| 51 | +
|
| 52 | +list of str or array_like |
| 53 | +:xref_param_type:`list` of :xref_param_type:`str` or :term:`numpy:array_like` |
| 54 | +
|
| 55 | +sequence of array_like |
| 56 | +:term:`python:sequence` of :term:`numpy:array_like` |
| 57 | +
|
| 58 | +str or pathlib.Path |
| 59 | +:xref_param_type:`str` or :xref_param_type:`pathlib.Path` |
| 60 | +
|
| 61 | +{'', string}, optional |
| 62 | +{'', :xref_param_type:`str`}, optional |
| 63 | +
|
| 64 | +{'C', 'F', 'A', or 'K'}, optional |
| 65 | +{'C', 'F', 'A', or 'K'}, optional |
| 66 | +
|
| 67 | +{'linear', 'lower', 'higher', 'midpoint', 'nearest'} |
| 68 | +{'linear', 'lower', 'higher', 'midpoint', 'nearest'} |
| 69 | +
|
| 70 | +{False, True, 'greedy', 'optimal'} |
| 71 | +{:xref_param_type:`False`, :xref_param_type:`True`, 'greedy', 'optimal'} |
| 72 | +
|
| 73 | +{{'begin', 1}, {'end', 0}}, {string, int} |
| 74 | +{{'begin', 1}, {'end', 0}}, {string, :xref_param_type:`int`} |
| 75 | +
|
| 76 | +callable f'(x,*args) |
| 77 | +:xref_param_type:`callable` f'(x,*args) |
| 78 | +
|
| 79 | +callable ``fhess(x, *args)``, optional |
| 80 | +:xref_param_type:`callable` ``fhess(x, *args)``, optional |
| 81 | +
|
| 82 | +spmatrix (format: ``csr``, ``bsr``, ``dia`` or coo``) |
| 83 | +:xref_param_type:`spmatrix` (format: ``csr``, ``bsr``, ``dia`` or coo``) |
| 84 | +
|
| 85 | +list(int) |
| 86 | +:xref_param_type:`list`(:xref_param_type:`int`) |
| 87 | +
|
| 88 | +list[int] |
| 89 | +:xref_param_type:`list`[:xref_param_type:`int`] |
| 90 | +
|
| 91 | +dict(str, int) |
| 92 | +:xref_param_type:`dict`(:xref_param_type:`str`, :xref_param_type:`int`) |
| 93 | +
|
| 94 | +dict[str, int] |
| 95 | +:xref_param_type:`dict`[:xref_param_type:`str`, :xref_param_type:`int`] |
| 96 | +
|
| 97 | +tuple(float, float) |
| 98 | +:xref_param_type:`tuple`(:xref_param_type:`float`, :xref_param_type:`float`) |
| 99 | +
|
| 100 | +dict[tuple(str, str), int] |
| 101 | +:xref_param_type:`dict`[:xref_param_type:`tuple`(:xref_param_type:`str`, :xref_param_type:`str`), :xref_param_type:`int`] |
| 102 | +""" # noqa: E501 |
| 103 | + |
| 104 | + |
| 105 | +def test_make_xref_param_type(): |
| 106 | + for s in data.strip().split('\n\n'): |
| 107 | + param_type, expected_result = s.split('\n') |
| 108 | + result = make_xref_param_type(param_type, xref_aliases) |
| 109 | + assert result == expected_result |
0 commit comments