Skip to content

Commit 25d86c6

Browse files
authored
Improve docs for no-any-return error code (#22070)
See discussion in #22050 for context.
1 parent 1648291 commit 25d86c6

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎docs/source/error_code_list2.rst‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ Check that function does not return Any value [no-any-return]
195195
-------------------------------------------------------------
196196

197197
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
200200

201201
Example:
202202

@@ -207,9 +207,15 @@ Example:
207207
def fields(s):
208208
return s.split(',')
209209
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]
213219
214220
.. _code-no-any-unimported:
215221

0 commit comments

Comments
 (0)