|
56 | 56 | from typing_extensions import Final, TypeAlias as _TypeAlias
|
57 | 57 |
|
58 | 58 | from mypy.nodes import (
|
59 |
| - MypyFile, TypeInfo, Node, AssignmentStmt, FuncDef, OverloadedFuncDef, |
| 59 | + AssertTypeExpr, MypyFile, TypeInfo, Node, AssignmentStmt, FuncDef, OverloadedFuncDef, |
60 | 60 | ClassDef, Var, GDEF, FuncItem, Import, Expression, Lvalue,
|
61 | 61 | ImportFrom, ImportAll, Block, LDEF, NameExpr, MemberExpr,
|
62 | 62 | IndexExpr, TupleExpr, ListExpr, ExpressionStmt, ReturnStmt,
|
|
99 | 99 | TypeTranslator, TypeOfAny, TypeType, NoneType, PlaceholderType, TPDICT_NAMES, ProperType,
|
100 | 100 | get_proper_type, get_proper_types, TypeAliasType, TypeVarLikeType, Parameters, ParamSpecType,
|
101 | 101 | PROTOCOL_NAMES, TYPE_ALIAS_NAMES, FINAL_TYPE_NAMES, FINAL_DECORATOR_NAMES, REVEAL_TYPE_NAMES,
|
102 |
| - is_named_instance, |
| 102 | + ASSERT_TYPE_NAMES, is_named_instance, |
103 | 103 | )
|
104 | 104 | from mypy.typeops import function_type, get_type_vars
|
105 | 105 | from mypy.type_visitor import TypeQuery
|
@@ -3898,6 +3898,19 @@ def visit_call_expr(self, expr: CallExpr) -> None:
|
3898 | 3898 | expr.analyzed.line = expr.line
|
3899 | 3899 | expr.analyzed.column = expr.column
|
3900 | 3900 | expr.analyzed.accept(self)
|
| 3901 | + elif refers_to_fullname(expr.callee, ASSERT_TYPE_NAMES): |
| 3902 | + if not self.check_fixed_args(expr, 2, 'assert_type'): |
| 3903 | + return |
| 3904 | + # Translate second argument to an unanalyzed type. |
| 3905 | + try: |
| 3906 | + target = self.expr_to_unanalyzed_type(expr.args[1]) |
| 3907 | + except TypeTranslationError: |
| 3908 | + self.fail('assert_type() type is not a type', expr) |
| 3909 | + return |
| 3910 | + expr.analyzed = AssertTypeExpr(expr.args[0], target) |
| 3911 | + expr.analyzed.line = expr.line |
| 3912 | + expr.analyzed.column = expr.column |
| 3913 | + expr.analyzed.accept(self) |
3901 | 3914 | elif refers_to_fullname(expr.callee, REVEAL_TYPE_NAMES):
|
3902 | 3915 | if not self.check_fixed_args(expr, 1, 'reveal_type'):
|
3903 | 3916 | return
|
@@ -4201,6 +4214,12 @@ def visit_cast_expr(self, expr: CastExpr) -> None:
|
4201 | 4214 | if analyzed is not None:
|
4202 | 4215 | expr.type = analyzed
|
4203 | 4216 |
|
| 4217 | + def visit_assert_type_expr(self, expr: AssertTypeExpr) -> None: |
| 4218 | + expr.expr.accept(self) |
| 4219 | + analyzed = self.anal_type(expr.type) |
| 4220 | + if analyzed is not None: |
| 4221 | + expr.type = analyzed |
| 4222 | + |
4204 | 4223 | def visit_reveal_expr(self, expr: RevealExpr) -> None:
|
4205 | 4224 | if expr.kind == REVEAL_TYPE:
|
4206 | 4225 | if expr.expr is not None:
|
|
0 commit comments