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 #15184

Merged
merged 4 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 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
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
)

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]