Skip to content

With disallow_untyped_defs, warn for missing return types on async def #4218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,11 @@ def is_unannotated_any(t: Type) -> bool:
if fdef.type is None and self.options.disallow_untyped_defs:
self.fail(messages.FUNCTION_TYPE_EXPECTED, fdef)
elif isinstance(fdef.type, CallableType):
if is_unannotated_any(fdef.type.ret_type):
ret_type = fdef.type.ret_type
if is_unannotated_any(ret_type):
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
elif (fdef.is_coroutine and isinstance(ret_type, Instance) and
is_unannotated_any(ret_type.args[0])):
self.fail(messages.RETURN_TYPE_EXPECTED, fdef)
if any(is_unannotated_any(t) for t in fdef.type.arg_types):
self.fail(messages.ARGUMENT_TYPE_EXPECTED, fdef)
Expand Down
31 changes: 25 additions & 6 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ def f(x) -> int: pass
[out]
main:2: error: Function is missing a type annotation for one or more arguments

[case testUnannotatedArgumentWithFastParser]
# flags: --disallow-untyped-defs
def f(x) -> int: pass
[out]
main:2: error: Function is missing a type annotation for one or more arguments

[case testNoArgumentFunction]
# flags: --disallow-untyped-defs
def f() -> int: pass
Expand Down Expand Up @@ -45,6 +39,31 @@ def f():
[out]
main:2: error: Function is missing a type annotation

[case testUntypedAsyncDef]
# flags: --disallow-untyped-defs
async def f(): # E: Function is missing a type annotation
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-full.pyi]

[case testAsyncUnannotatedArgument]
# flags: --disallow-untyped-defs
async def f(x) -> None: # E: Function is missing a type annotation for one or more arguments
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-full.pyi]

[case testAsyncUnannotatedReturn]
# flags: --disallow-untyped-defs
from typing import Any
async def f(x: int): # E: Function is missing a return type annotation
pass
# Make sure explicit Any is allowed.
async def g(x: int) -> Any:
pass
[builtins fixtures/async_await.pyi]
[typing fixtures/typing-full.pyi]

[case testDisallowUntypedDefsUntypedDecorator]
# flags: --disallow-untyped-decorators
def d(p):
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/async_await.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ U = typing.TypeVar('U')
class list(typing.Sequence[T]): pass

class object:
def __init__(self): pass
def __init__(self) -> None: pass
class type: pass
class function: pass
class int: pass
class str: pass
class bool: pass
class dict(typing.Generic[T, U]): pass
class set(typing.Generic[T]): pass
class tuple(typing.Generic[T]): pass
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/typing-full.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def runtime(cls: T) -> T:

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

TYPE_CHECKING = 1