Skip to content
Open
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
15 changes: 15 additions & 0 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ Example:
def __init__(self) -> None:
self.value = 0

Check that every function has a return annotation [no-incomplete-def]
---------------------------------------------------------------------

If you use :option:`--disallow-incomplete-defs <mypy --disallow-incomplete-defs>`, mypy requires that all functions
fully annotated.

Example:

.. code-block:: python

# mypy: disallow-incomplete-defs

def example(x: int): # Error: Function is missing a return type annotation [no-incomplete-def]
pass

Check that cast is not redundant [redundant-cast]
-------------------------------------------------

Expand Down
11 changes: 9 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,9 @@ def is_unannotated_any(t: Type) -> bool:
self.fail(message_registry.FUNCTION_TYPE_EXPECTED, fdef)
elif isinstance(fdef.type, CallableType):
ret_type = get_proper_type(fdef.type.ret_type)
if is_unannotated_any(ret_type):
if is_unannotated_any(ret_type) and check_incomplete_defs:
self.fail(message_registry.RETURN_TYPE_EXPECTED_INCOMPLETE_DEF, fdef)
elif is_unannotated_any(ret_type):
self.fail(message_registry.RETURN_TYPE_EXPECTED, fdef)
elif fdef.is_generator:
if is_unannotated_any(
Expand All @@ -1411,7 +1413,12 @@ def is_unannotated_any(t: Type) -> bool:
elif fdef.is_coroutine and isinstance(ret_type, Instance):
if is_unannotated_any(self.get_coroutine_return_type(ret_type)):
self.fail(message_registry.RETURN_TYPE_EXPECTED, fdef)
if any(is_unannotated_any(t) for t in fdef.type.arg_types):
if (
any(is_unannotated_any(t) for t in fdef.type.arg_types)
and check_incomplete_defs
):
self.fail(message_registry.ARGUMENT_TYPE_EXPECTED_INCOMPLETE_DEF, fdef)
elif any(is_unannotated_any(t) for t in fdef.type.arg_types):
self.fail(message_registry.ARGUMENT_TYPE_EXPECTED, fdef)

def check___new___signature(self, fdef: FuncDef, typ: CallableType) -> None:
Expand Down
5 changes: 5 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def __str__(self) -> str:
)


NO_INCOMPLETE_DEF: Final[ErrorCode] = ErrorCode(
"no-incomplete-def", "Function is missing a return type annotation", "General"
)


# Syntax errors are often blocking.
SYNTAX: Final[ErrorCode] = ErrorCode("syntax", "Report syntax errors", "General")

Expand Down
6 changes: 6 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
RETURN_TYPE_EXPECTED: Final = ErrorMessage(
"Function is missing a return type annotation", codes.NO_UNTYPED_DEF
)
RETURN_TYPE_EXPECTED_INCOMPLETE_DEF: Final = ErrorMessage(
"Function is missing a return type annotation", codes.NO_INCOMPLETE_DEF
)
ARGUMENT_TYPE_EXPECTED: Final = ErrorMessage(
"Function is missing a type annotation for one or more arguments", codes.NO_UNTYPED_DEF
)
ARGUMENT_TYPE_EXPECTED_INCOMPLETE_DEF: Final = ErrorMessage(
"Function is missing a type annotation for one or more arguments", codes.NO_INCOMPLETE_DEF
)
KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE: Final = ErrorMessage(
'Keyword argument only valid with "str" key type in call to "dict"'
)
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ def f() -> None:
def g():
pass

[case testErrorCodeNoReturnAnnotation]
# flags: --disallow-incomplete-defs

def f(i: int): # E: Function is missing a return type annotation [no-incomplete-def]
pass

def g(i) -> None: # E: Function is missing a type annotation for one or more arguments [no-incomplete-def]
pass

[case testErrorCodeIndexing]
from typing import Dict
x: Dict[int, int]
Expand Down