Skip to content

Commit e1c569c

Browse files
JelleZijlstraJukkaL
authored andcommitted
With disallow_untyped_defs, warn for missing return types on async def (#4218)
Fixes #4209. Under disallow_untyped_defs, we were missing errors for missing return types on async def functions, because the return type of such functions is `Awaitable[Any]` instead of `Any`. This PR also removes a duplicate test case (apparently from the fast parser cleanup) and adds some missing annotations to stubs.
1 parent 51fe653 commit e1c569c

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

mypy/checker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,11 @@ def is_unannotated_any(t: Type) -> bool:
792792
if fdef.type is None and self.options.disallow_untyped_defs:
793793
self.fail(messages.FUNCTION_TYPE_EXPECTED, fdef)
794794
elif isinstance(fdef.type, CallableType):
795-
if is_unannotated_any(fdef.type.ret_type):
795+
ret_type = fdef.type.ret_type
796+
if is_unannotated_any(ret_type):
797+
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
798+
elif (fdef.is_coroutine and isinstance(ret_type, Instance) and
799+
is_unannotated_any(ret_type.args[0])):
796800
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
797801
if any(is_unannotated_any(t) for t in fdef.type.arg_types):
798802
self.fail(messages.ARGUMENT_TYPE_EXPECTED, fdef)

test-data/unit/check-flags.test

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ def f(x) -> int: pass
1010
[out]
1111
main:2: error: Function is missing a type annotation for one or more arguments
1212

13-
[case testUnannotatedArgumentWithFastParser]
14-
# flags: --disallow-untyped-defs
15-
def f(x) -> int: pass
16-
[out]
17-
main:2: error: Function is missing a type annotation for one or more arguments
18-
1913
[case testNoArgumentFunction]
2014
# flags: --disallow-untyped-defs
2115
def f() -> int: pass
@@ -45,6 +39,31 @@ def f():
4539
[out]
4640
main:2: error: Function is missing a type annotation
4741

42+
[case testUntypedAsyncDef]
43+
# flags: --disallow-untyped-defs
44+
async def f(): # E: Function is missing a type annotation
45+
pass
46+
[builtins fixtures/async_await.pyi]
47+
[typing fixtures/typing-full.pyi]
48+
49+
[case testAsyncUnannotatedArgument]
50+
# flags: --disallow-untyped-defs
51+
async def f(x) -> None: # E: Function is missing a type annotation for one or more arguments
52+
pass
53+
[builtins fixtures/async_await.pyi]
54+
[typing fixtures/typing-full.pyi]
55+
56+
[case testAsyncUnannotatedReturn]
57+
# flags: --disallow-untyped-defs
58+
from typing import Any
59+
async def f(x: int): # E: Function is missing a return type annotation
60+
pass
61+
# Make sure explicit Any is allowed.
62+
async def g(x: int) -> Any:
63+
pass
64+
[builtins fixtures/async_await.pyi]
65+
[typing fixtures/typing-full.pyi]
66+
4867
[case testDisallowUntypedDefsUntypedDecorator]
4968
# flags: --disallow-untyped-decorators
5069
def d(p):

test-data/unit/fixtures/async_await.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ U = typing.TypeVar('U')
55
class list(typing.Sequence[T]): pass
66

77
class object:
8-
def __init__(self): pass
8+
def __init__(self) -> None: pass
99
class type: pass
1010
class function: pass
1111
class int: pass
1212
class str: pass
13+
class bool: pass
1314
class dict(typing.Generic[T, U]): pass
1415
class set(typing.Generic[T]): pass
1516
class tuple(typing.Generic[T]): pass

test-data/unit/fixtures/typing-full.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def runtime(cls: T) -> T:
140140

141141
class ContextManager(Generic[T]):
142142
def __enter__(self) -> T: pass
143-
def __exit__(self, exc_type, exc_value, traceback): pass
143+
# Use Any because not all the precise types are in the fixtures.
144+
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> Any: pass
144145

145146
TYPE_CHECKING = 1

0 commit comments

Comments
 (0)