Skip to content
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

Output distinct types when type names are ambiguous #15183

Closed
wants to merge 6 commits into from
Closed
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 @@ -1435,7 +1435,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 @@ -1445,7 +1447,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
1 change: 0 additions & 1 deletion mypy/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ def __str__(self) -> str:
)
UNUSED_IGNORE: Final = ErrorCode(
"unused-ignore", "Ensure that all type ignores are used", "General", default_enabled=False
)


# Syntax errors are often blocking.
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
6 changes: 2 additions & 4 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,11 +1656,9 @@ def redundant_cast(self, typ: Type, context: Context) -> None:
)

def assert_type_fail(self, source_type: Type, target_type: Type, context: Context) -> None:
(source, target) = format_type_distinctly(source_type, target_type, options=self.options)
self.fail(
f"Expression is of type {format_type(source_type, self.options)}, "
f"not {format_type(target_type, self.options)}",
context,
code=codes.ASSERT_TYPE,
f"Expression is of type {source}, " f"not {target}", context, code=codes.ASSERT_TYPE
)

def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> None:
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-assert-type-fail.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[case testAssertTypeFail1]
import typing
import array as arr
class array:
pass
def f(si: arr.array[int]):
typing.assert_type(si, array) # E: Expression is of type "array.array[int]", not "__main__.array"
[builtins fixtures/tuple.pyi]

[case testAssertTypeFail2]
import typing
import array as arr
class array:
class array:
i = 1
def f(si: arr.array[int]):
typing.assert_type(si, array.array) # E: Expression is of type "array.array[int]", not "__main__.array.array"
[builtins fixtures/tuple.pyi]

[case testAssertTypeFail3]
import typing
import array as arr
class array:
class array:
i = 1
def f(si: arr.array[int]):
typing.assert_type(si, int) # E: Expression is of type "array[int]", not "int"
[builtins fixtures/tuple.pyi]
9 changes: 9 additions & 0 deletions test-data/unit/check-errorcodes.test
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,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