Skip to content

Commit 9d546a8

Browse files
authored
Remove skimage specific array expression syntax (#77)
* Remove skimage specific array expression syntax Which was somewhat ambiguious and more complicated than necessary. Instead let's go with the convention used by scikit-learn. * Remove skimage array forms from example package * Replace "e.g." with "for example" The latter is clearer and easier to understand language.
1 parent b878555 commit 9d546a8

File tree

5 files changed

+28
-34
lines changed

5 files changed

+28
-34
lines changed

docs/introduction.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ ski = "skimage"
110110
"sklearn.tree" = "sklearn.tree"
111111
```
112112

113-
which will enable any type that is prefixed with `ski.` or `sklearn.tree.`, e.g. `ski.transform.AffineTransform` or `sklearn.tree.DecisionTreeClassifier`.
113+
which will enable any type that is prefixed with `ski.` or `sklearn.tree.`, for example `ski.transform.AffineTransform` or `sklearn.tree.DecisionTreeClassifier`.
114114

115115
:::{important}
116116
Docstub doesn't check that types actually exist or if a symbol is a valid type.
@@ -162,7 +162,7 @@ In those cases, you docstub provides a few approaches to dealing with this.
162162
Docstub will always preserve inline type annotations, regardless of what the docstring contains.
163163
This is useful for example, if you want to express something that isn't yet supported by Python's type system.
164164

165-
E.g., consider the docstring type of `ord` parameter in [`numpy.linalg.matrix_norm`](https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_norm.html)
165+
For example, consider the docstring type of `ord` parameter in [`numpy.linalg.matrix_norm`](https://numpy.org/doc/stable/reference/generated/numpy.linalg.matrix_norm.html)
166166
```rst
167167
ord : {1, -1, 2, -2, inf, -inf, ‘fro’, ‘nuc’}, optional
168168
```
@@ -181,7 +181,7 @@ At its heart, docstub transforms Python source files into stub files.
181181
You can tell docstub to temporarily stop that transformation for a specific area with a comment directive.
182182
Wrapping lines of code with `docstub: off` and `docstub: on` comments will preserve these lines completely.
183183

184-
E.g., consider the following example:
184+
For example, consider the following example:
185185
```python
186186
class Foo:
187187
# docstub: off

docs/typing_syntax.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This extends the basic subscription syntax for [generics](https://typing.python.
4545
| `CONTAINER of (X or Y)` | `CONTAINER[X \| Y]` |
4646

4747
For the simple case `CONTAINER of X`, where `X` is a name, you can append `(s)` to indicate the plural form.
48-
E.g., `list of float(s)`.
48+
For example, `list of float(s)`.
4949

5050
Variants of for [**tuples**](https://typing.python.org/en/latest/spec/tuples.html)
5151

@@ -73,28 +73,30 @@ In the future, docstub may warn against or disallow nesting these natural langua
7373

7474
This expression allows adding shape and datatype information for data structures like [NumPy arrays](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html).
7575

76-
`array` and `ndarray`, and `array-like` and `array_like` can be used interchange-ably.
76+
`array` and `ndarray`, and `array-like` and `array_like` can be used interchange-ably for the variable `ARRAY` below.
77+
78+
| Docstring type | Python type annotation |
79+
|----------------------------------------|------------------------|
80+
| `ARRAY of dtype DTYPE` | `ARRAY[DTYPE]` |
81+
| `ARRAY of dtype DTYPE and shape SHAPE` | `ARRAY[DTYPE]` |
82+
| `ARRAY of shape SHAPE` | `ARRAY[DTYPE]` |
83+
| `ARRAY of shape SHAPE and dtype DTYPE` | `ARRAY[DTYPE]` |
84+
85+
For example
86+
87+
| Docstring type | Python type annotation |
88+
|------------------------------------------|------------------------|
89+
| `array of dtype int` | `ndarray[int]` |
90+
| `ndarray of dtype bool and shape (4, 4)` | `ndarray[bool]` |
91+
| `array-like of dtype float` | `ArrayLike[float]` |
92+
| `array_like of shape (M, 2)` | `ArrayLike` |
7793

78-
| Docstring type | Python type annotation |
79-
|-----------------------------|------------------------|
80-
| `array of DTYPE` | `ndarray[DTYPE]` |
81-
| `ndarray of dtype DTYPE` | `ndarray[DTYPE]` |
82-
| `array-like of DTYPE` | `ArrayLike[DTYPE]` |
83-
| `array_like of dtype DTYPE` | `ArrayLike[DTYPE]` |
8494

8595
:::{note}
8696
Noting the **shape** of an array in the docstring is supported.
87-
However, Python's typing system is not yet able to express this information.
88-
It is therefore not included in the resulting type annotation.
97+
However, [support for including shapes in generated stubs](https://github.com/scientific-python/docstub/issues/76) is not yet included in docstub.
8998
:::
9099

91-
| Docstring type | Python type annotation |
92-
|--------------------------|------------------------|
93-
| `(3,) array of DTYPE` | `ndarray[DTYPE]` |
94-
| `(X, Y) array of DTYPE` | `ndarray[DTYPE]` |
95-
| `([P,] M, N) array-like` | `ArrayLike` |
96-
| `(M, ...) ndarray` | `ArrayLike` |
97-
98100

99101
## Literals
100102

@@ -113,7 +115,7 @@ However, `Literal[X]` is more explicit.
113115

114116
:::{warning}
115117
Python's `typing.Literal` only supports a restricted set of parameters.
116-
E.g., `float` literals are not yet supported by the type system but are allowed by docstub.
118+
For example, `float` literals are not yet supported by the type system but are allowed by docstub.
117119
Addressing this use case is on the roadmap.
118120
See [issue 47](https://github.com/scientific-python/docstub/issues/47) for more details.
119121
:::

examples/example_pkg/_numpy.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def func_ndarray(a1, a2, a3, a4=None):
2020
----------
2121
a1 : ndarray
2222
a2 : np.NDArray
23-
a3 : (N, 3) ndarray of float
23+
a3 : ndarray of dtype float and shape (N, 3)
2424
a4 : ndarray of shape (1,) and dtype uint8
2525
2626
Returns
2727
-------
28-
r1 : uint8 array
28+
r1 : array of dtype uint8
2929
r2 : array of dtype complex and shape (1, ..., 3)
3030
"""
3131

@@ -37,11 +37,11 @@ def func_array_like(a1, a2, a3, a4):
3737
----------
3838
a1 : array-like
3939
a2 : array_like
40-
a3 : (N, 3) array-like of float
40+
a3 : array-like of dtype float and shape (N, 3)
4141
a4 : array-like of shape (1,) and dtype uint8
4242
4343
Returns
4444
-------
45-
r1 : uint8 array-like
45+
r1 : array-like of dtype uint8
4646
r2 : array_like of dtype complex and shape (1, ..., 3)
4747
"""

src/docstub/doctype.lark

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ _natlang_mapping: qualname "of" "{" type ":" type "}"
8989
// A natural language alternative to describe arrays with a dtype and shape.
9090
natlang_array: array_name "of dtype" dtype ("and shape" shape)?
9191
| array_name "of shape" shape ("and dtype" dtype)?
92-
| shape array_name ("of" dtype)?
93-
| shape? array_name "of" dtype
94-
| shape dtype array_name
95-
| dtype array_name
9692

9793

9894
// Currently a bit of a hack. Since the `array_expression` is ambiguous, we

tests/test_docstrings.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,9 @@ def test_rst_role(self, doctype, expected):
228228
@pytest.mark.parametrize(
229229
("fmt", "expected_fmt"),
230230
[
231-
("{shape} {name}", "{name}"),
232-
("{shape} {name} of {dtype}", "{name}[{dtype}]"),
233-
("{shape} {dtype} {name}", "{name}[{dtype}]"),
234-
("{dtype} {name}", "{name}[{dtype}]"),
235231
("{name} of shape {shape} and dtype {dtype}", "{name}[{dtype}]"),
236232
("{name} of dtype {dtype} and shape {shape}", "{name}[{dtype}]"),
233+
("{name} of {dtype}", "{name}[{dtype}]"),
237234
],
238235
)
239236
@pytest.mark.parametrize("name", ["array", "ndarray", "array-like", "array_like"])
@@ -261,7 +258,6 @@ def escape(name: str) -> str:
261258
("doctype", "expected"),
262259
[
263260
("ndarray of dtype (int or float)", "ndarray[int | float]"),
264-
("([P,] M, N) (int or float) array", "array[int | float]"),
265261
],
266262
)
267263
def test_natlang_array_specific(self, doctype, expected):

0 commit comments

Comments
 (0)