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 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
8 changes: 2 additions & 6 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,12 +1656,8 @@ def redundant_cast(self, typ: Type, context: Context) -> None:
)

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

def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> None:
self.fail(
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]