Skip to content

Commit

Permalink
Use source_type.last_known_value when Instance. Fixes python#12923
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlFK committed Apr 25, 2023
1 parent 15fbd53 commit 06c1a61
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3918,6 +3918,9 @@ def visit_assert_type_expr(self, expr: AssertTypeExpr) -> Type:
always_allow_any=True,
)
target_type = expr.type
if isinstance( source_type, mypy.types.Instance ) \
and source_type.last_known_value is not None:
source_type = 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

0 comments on commit 06c1a61

Please sign in to comment.