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

Note that assert_type always matches against any in untyped function #13626

Merged
merged 5 commits into from
Sep 8, 2022
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
5 changes: 5 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3724,6 +3724,11 @@ def visit_assert_type_expr(self, expr: AssertTypeExpr) -> Type:
)
target_type = expr.type
if not is_same_type(source_type, target_type):
if not self.chk.in_checked_function():
self.msg.note(
'"assert_type" expects everything to be "Any" in unchecked functions',
expr.expr,
)
self.msg.assert_type_fail(source_type, target_type, expr)
return source_type

Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,16 @@ y: Gen[Literal[1]] = assert_type(Gen(1), Gen[Literal[1]])

[builtins fixtures/tuple.pyi]

[case testAssertTypeUncheckedFunction]
from typing_extensions import assert_type, Literal
rsokl marked this conversation as resolved.
Show resolved Hide resolved
def f():
rsokl marked this conversation as resolved.
Show resolved Hide resolved
x = 42
assert_type(x, Literal[42])
[out]
main:4 error: Expression is of type "Any", not "Literal[42]"
main:4 note: "assert_type" expects everything to be "Any" in unchecked functions
[builtins fixtures/tuple.pyi]

-- None return type
-- ----------------

Expand Down