Skip to content

Commit

Permalink
Adding optional error flag missing return type
Browse files Browse the repository at this point in the history
Adding optional error flag for missing return type, when a function has at least one typed argument. This will address issue python#15127

Co-Authored-By: Leonardo Abreu Santos <31524775+saintleonar@users.noreply.github.com>
  • Loading branch information
joaopedro-s and SaintLeonar committed Jul 1, 2023
1 parent 19c5d5f commit 26dc8b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,8 +1420,15 @@ def is_unannotated_any(t: Type) -> bool:
not is_unannotated_any(t) for t in fdef.type.arg_types + [fdef.type.ret_type]
)

has_explicit_arg_annotation = isinstance(fdef.type, CallableType) and any(
not is_unannotated_any(t) for t in fdef.type.arg_types
)

show_untyped = not self.is_typeshed_stub or self.options.warn_incomplete_stub
check_incomplete_defs = self.options.disallow_incomplete_defs and has_explicit_annotation
check_incomplete_defs_arg = (
self.options.disallow_incomplete_defs and has_explicit_arg_annotation
)
if show_untyped and (self.options.disallow_untyped_defs or check_incomplete_defs):
if fdef.type is None and self.options.disallow_untyped_defs:
if not fdef.arguments or (
Expand All @@ -1439,7 +1446,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 check_incomplete_defs_arg and is_unannotated_any(ret_type):
self.fail(message_registry.RETURN_TYPE_EXPECTED_TYPED_ARG, fdef)
elif is_unannotated_any(ret_type):
self.fail(message_registry.RETURN_TYPE_EXPECTED, fdef)
elif fdef.is_generator:
if is_unannotated_any(
Expand Down
5 changes: 5 additions & 0 deletions mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def __hash__(self) -> int:
NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
"no-untyped-def", "Check that every function has an annotation", "General"
)
NO_UNTYPED_RET: Final[ErrorCode] = ErrorCode(
"no-untyped-ret",
"Check that every function with at least one typed argument has a typed return",
"General",
)
NO_UNTYPED_CALL: Final = ErrorCode(
"no-untyped-call",
"Disallow calling functions without type annotations from annotated functions",
Expand Down
3 changes: 3 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ 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_TYPED_ARG: Final = ErrorMessage(
"Function is missing a return type annotation", codes.NO_UNTYPED_RET
)
ARGUMENT_TYPE_EXPECTED: Final = ErrorMessage(
"Function is missing a type annotation for one or more arguments", codes.NO_UNTYPED_DEF
)
Expand Down

0 comments on commit 26dc8b8

Please sign in to comment.