Skip to content

Commit 26dc8b8

Browse files
Adding optional error flag missing return type
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 <[email protected]>
1 parent 19c5d5f commit 26dc8b8

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

mypy/checker.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,8 +1420,15 @@ def is_unannotated_any(t: Type) -> bool:
14201420
not is_unannotated_any(t) for t in fdef.type.arg_types + [fdef.type.ret_type]
14211421
)
14221422

1423+
has_explicit_arg_annotation = isinstance(fdef.type, CallableType) and any(
1424+
not is_unannotated_any(t) for t in fdef.type.arg_types
1425+
)
1426+
14231427
show_untyped = not self.is_typeshed_stub or self.options.warn_incomplete_stub
14241428
check_incomplete_defs = self.options.disallow_incomplete_defs and has_explicit_annotation
1429+
check_incomplete_defs_arg = (
1430+
self.options.disallow_incomplete_defs and has_explicit_arg_annotation
1431+
)
14251432
if show_untyped and (self.options.disallow_untyped_defs or check_incomplete_defs):
14261433
if fdef.type is None and self.options.disallow_untyped_defs:
14271434
if not fdef.arguments or (
@@ -1439,7 +1446,9 @@ def is_unannotated_any(t: Type) -> bool:
14391446
self.fail(message_registry.FUNCTION_TYPE_EXPECTED, fdef)
14401447
elif isinstance(fdef.type, CallableType):
14411448
ret_type = get_proper_type(fdef.type.ret_type)
1442-
if is_unannotated_any(ret_type):
1449+
if check_incomplete_defs_arg and is_unannotated_any(ret_type):
1450+
self.fail(message_registry.RETURN_TYPE_EXPECTED_TYPED_ARG, fdef)
1451+
elif is_unannotated_any(ret_type):
14431452
self.fail(message_registry.RETURN_TYPE_EXPECTED, fdef)
14441453
elif fdef.is_generator:
14451454
if is_unannotated_any(

mypy/errorcodes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ def __hash__(self) -> int:
151151
NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
152152
"no-untyped-def", "Check that every function has an annotation", "General"
153153
)
154+
NO_UNTYPED_RET: Final[ErrorCode] = ErrorCode(
155+
"no-untyped-ret",
156+
"Check that every function with at least one typed argument has a typed return",
157+
"General",
158+
)
154159
NO_UNTYPED_CALL: Final = ErrorCode(
155160
"no-untyped-call",
156161
"Disallow calling functions without type annotations from annotated functions",

mypy/message_registry.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
119119
RETURN_TYPE_EXPECTED: Final = ErrorMessage(
120120
"Function is missing a return type annotation", codes.NO_UNTYPED_DEF
121121
)
122+
RETURN_TYPE_EXPECTED_TYPED_ARG: Final = ErrorMessage(
123+
"Function is missing a return type annotation", codes.NO_UNTYPED_RET
124+
)
122125
ARGUMENT_TYPE_EXPECTED: Final = ErrorMessage(
123126
"Function is missing a type annotation for one or more arguments", codes.NO_UNTYPED_DEF
124127
)

0 commit comments

Comments
 (0)