GeezerVison is showing github.com — Improve docs for no-any-return error code (#22070) · python/mypy@25d86c6
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: docs/source/error_code_list2.rst +11-5Lines changed: 11 additions & 5 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -195,8 +195,8 @@ Check that function does not return Any value [no-any-return] |
195 | 195 | ------------------------------------------------------------- |
196 | 196 |
|
197 | 197 | If you use :option:`--warn-return-any <mypy --warn-return-any>`, mypy generates an error if you return a |
198 | | -value with an ``Any`` type in a function that is annotated to return a |
199 | | -non-``Any`` value. |
| 198 | +value with a plain ``Any`` type in a function that is annotated to return a |
| 199 | +non-``Any`` value. You can use this to enforce better boundaries around unsafe/dynamic code |
200 | 200 |
|
201 | 201 | Example: |
202 | 202 |
|
@@ -207,9 +207,15 @@ Example: |
207 | 207 | def fields(s): |
208 | 208 | return s.split(',') |
209 | 209 |
|
210 | | - def first_field(x: str) -> str: |
211 | | - # Error: Returning Any from function declared to return "str" [no-any-return] |
212 | | - return fields(x)[0] |
| 210 | + def reversed_fields_bad(x: str) -> list[str]: |
| 211 | + # Error: Returning Any from function declared to return "list[str]" [no-any-return] |
| 212 | + return fields(x)[::-1] |
| 213 | +
|
| 214 | + def reversed_fields_ok(x: str) -> list[str]: |
| 215 | + x_fields = fields(x) |
| 216 | + assert isinstance(x_fields, list) |
| 217 | + # OK, we can at least guarantee return is list[Any] |
| 218 | + return x_fields[::-1] |
213 | 219 |
|
214 | 220 | .. _code-no-any-unimported: |
215 | 221 |
|
|