Skip to content
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
6 changes: 6 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3918,6 +3918,12 @@ def visit_assert_type_expr(self, expr: AssertTypeExpr) -> Type:
always_allow_any=True,
)
target_type = expr.type
proper_source_type = get_proper_type(source_type)
if (
isinstance(proper_source_type, mypy.types.Instance)
and proper_source_type.last_known_value is not None
):
source_type = proper_source_type.last_known_value
if not is_same_type(source_type, target_type):
if not self.chk.in_checked_function():
self.msg.note(
Expand Down
2 changes: 2 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,8 @@ reveal_type(returned) # N: Revealed type is "builtins.int"
assert_type(a, str) # E: Expression is of type "int", not "str"
assert_type(a, Any) # E: Expression is of type "int", not "Any"
assert_type(a, Literal[1]) # E: Expression is of type "int", not "Literal[1]"
assert_type(42, Literal[42])
assert_type(42, int) # E: Expression is of type "Literal[42]", not "int"
[builtins fixtures/tuple.pyi]

[case testAssertTypeGeneric]
Expand Down