Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/numpy/_typing/_add_docstring.py: 33%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

33 statements  

1"""A module for creating docstrings for sphinx ``data`` domains.""" 

2 

3import re 

4import textwrap 

5 

6from ._array_like import NDArray 

7 

8_docstrings_list = [] 

9 

10 

11def add_newdoc(name: str, value: str, doc: str) -> None: 

12 """Append ``_docstrings_list`` with a docstring for `name`. 

13 

14 Parameters 

15 ---------- 

16 name : str 

17 The name of the object. 

18 value : str 

19 A string-representation of the object. 

20 doc : str 

21 The docstring of the object. 

22 

23 """ 

24 _docstrings_list.append((name, value, doc)) 

25 

26 

27def _parse_docstrings() -> str: 

28 """Convert all docstrings in ``_docstrings_list`` into a single 

29 sphinx-legible text block. 

30 

31 """ 

32 type_list_ret = [] 

33 for name, value, doc in _docstrings_list: 

34 s = textwrap.dedent(doc).replace("\n", "\n ") 

35 

36 # Replace sections by rubrics 

37 lines = s.split("\n") 

38 new_lines = [] 

39 indent = "" 

40 for line in lines: 

41 m = re.match(r'^(\s+)[-=]+\s*$', line) 

42 if m and new_lines: 

43 prev = textwrap.dedent(new_lines.pop()) 

44 if prev == "Examples": 

45 indent = "" 

46 new_lines.append(f'{m.group(1)}.. rubric:: {prev}') 

47 else: 

48 indent = 4 * " " 

49 new_lines.append(f'{m.group(1)}.. admonition:: {prev}') 

50 new_lines.append("") 

51 else: 

52 new_lines.append(f"{indent}{line}") 

53 

54 s = "\n".join(new_lines) 

55 s_block = f""".. data:: {name}\n :value: {value}\n {s}""" 

56 type_list_ret.append(s_block) 

57 return "\n".join(type_list_ret) 

58 

59 

60add_newdoc('ArrayLike', 'typing.Union[...]', 

61 """ 

62 A `~typing.Union` representing objects that can be coerced 

63 into an `~numpy.ndarray`. 

64 

65 Among others this includes the likes of: 

66 

67 * Scalars. 

68 * (Nested) sequences. 

69 * Objects implementing the `~class.__array__` protocol. 

70 

71 .. versionadded:: 1.20 

72 

73 See Also 

74 -------- 

75 :term:`array_like`: 

76 Any scalar or sequence that can be interpreted as an ndarray. 

77 

78 Examples 

79 -------- 

80 .. code-block:: python 

81 

82 >>> import numpy as np 

83 >>> import numpy.typing as npt 

84 

85 >>> def as_array(a: npt.ArrayLike) -> np.ndarray: 

86 ... return np.array(a) 

87 

88 """) 

89 

90add_newdoc('DTypeLike', 'typing.Union[...]', 

91 """ 

92 A `~typing.Union` representing objects that can be coerced 

93 into a `~numpy.dtype`. 

94 

95 Among others this includes the likes of: 

96 

97 * :class:`type` objects. 

98 * Character codes or the names of :class:`type` objects. 

99 * Objects with the ``.dtype`` attribute. 

100 

101 .. versionadded:: 1.20 

102 

103 See Also 

104 -------- 

105 :ref:`Specifying and constructing data types <arrays.dtypes.constructing>` 

106 A comprehensive overview of all objects that can be coerced 

107 into data types. 

108 

109 Examples 

110 -------- 

111 .. code-block:: python 

112 

113 >>> import numpy as np 

114 >>> import numpy.typing as npt 

115 

116 >>> def as_dtype(d: npt.DTypeLike) -> np.dtype: 

117 ... return np.dtype(d) 

118 

119 """) 

120 

121add_newdoc('NDArray', repr(NDArray), 

122 """ 

123 A `np.ndarray[tuple[Any, ...], np.dtype[ScalarT]] <numpy.ndarray>` 

124 type alias :term:`generic <generic type>` w.r.t. its 

125 `dtype.type <numpy.dtype.type>`. 

126 

127 Can be used during runtime for typing arrays with a given dtype 

128 and unspecified shape. 

129 

130 .. versionadded:: 1.21 

131 

132 Examples 

133 -------- 

134 .. code-block:: python 

135 

136 >>> import numpy as np 

137 >>> import numpy.typing as npt 

138 

139 >>> print(npt.NDArray) 

140 numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[~_ScalarT]] 

141 

142 >>> print(npt.NDArray[np.float64]) 

143 numpy.ndarray[tuple[typing.Any, ...], numpy.dtype[numpy.float64]] 

144 

145 >>> NDArrayInt = npt.NDArray[np.int_] 

146 >>> a: NDArrayInt = np.arange(10) 

147 

148 >>> def func(a: npt.ArrayLike) -> npt.NDArray[Any]: 

149 ... return np.array(a) 

150 

151 """) 

152 

153_docstrings = _parse_docstrings()