You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Keep inline annotations over docstring generated ones (#61)
* first pass at keeping inline annotation over docstring
* Make override_docsring tests more independent
Keeping parameters is already tested in `override_docstring_param`
above – no need to do so again.
* Remove duplication in error message
The `underline` method of the `ErrorReporter` includes the underlined
line already.
* Simplify logic when ignoring docstring annotation
Also makes sure that imports of a doctype are not unnecessarily
included an inline annotation already exists.
* Keep existing inline annotation for assignments
even if a different docstring annotation exists. This is the same
behavior as for parameters and return types.
* Add more tests
* Re-enable commented out tests
Probably forgot to do so during debugging.
* Document inline annotation as overwrite solution
---------
Co-authored-by: Lars Grüter <lagru@mailbox.org>
Copy file name to clipboardExpand all lines: docs/user_guide.md
+32-4Lines changed: 32 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -172,9 +172,35 @@ Two command line options can help addressing these errors gradually:
172
172
173
173
## Dealing with typing problems
174
174
175
-
Docstub may not fully or correctly implement a particular part of Python's typing system yet.
175
+
For various reasons – missing features in docstub, or limitations of Python's typing system – it may not always be possible to correctly type something in a docstring.
176
+
In those cases, you docstub provides a few approaches to dealing with this.
176
177
177
-
In some cases, you can use a comment directive to selectively disable docstub for a specific block of lines:
178
+
179
+
### Use inline type annotation
180
+
181
+
Docstub will always preserve inline type annotations, regardless of what the docstring contains.
182
+
This is useful for example, if you want to express something that isn't yet supported by Python's type system.
183
+
184
+
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)
[Python's type system currently can't express floats as literal types](https://typing.python.org/en/latest/spec/literal.html#:~:text=Floats%3A%20e.g.%20Literal%5B3.14%5D) – such as `inf`.
189
+
We don't want to make the type description here less specific to users, so instead, you could handle this with a less constrained inline type annotation like
190
+
```python
191
+
ord: Literal[1, -1, 2, -2, 'fro', 'nuc'] | float
192
+
```
193
+
Docstub will include the latter less constrained type in the stubs.
194
+
This allows you to keep the information in the docstring while still having valid – if a bit less constrained – stubs.
195
+
196
+
197
+
### Preserve code with comment directive
198
+
199
+
At its heart, docstub transforms Python source files into stub files.
200
+
You can tell docstub to temporarily stop that transformation for a specific area with a comment directive.
201
+
Wrapping lines of code with `docstub: off` and `docstub: on` comments will preserve these lines completely.
202
+
203
+
E.g., consider the following example:
178
204
```python
179
205
class Foo:
180
206
# docstub: off
@@ -184,7 +210,7 @@ class Foo:
184
210
c: int = None
185
211
d: str = ""
186
212
```
187
-
will leave the parameters within the `# docstub` guards untouched in the resulting stub file:
213
+
will leave the guarded parameters untouched in the resulting stub file:
188
214
```python
189
215
class Foo:
190
216
a: int = None
@@ -193,5 +219,7 @@ class Foo:
193
219
d: str
194
220
```
195
221
196
-
If that is not possible, you can – for now – fallback to writing a correct stub file by hand.
222
+
### Write a manual stub file
223
+
224
+
If all of the above does not solve your issue, you can fall back to writing a correct stub file by hand.
197
225
Docstub will preserve this file and integrated it with other automatically generated stubs.
0 commit comments