|
136 | 136 | ListExpr, |
137 | 137 | Lvalue, |
138 | 138 | MatchStmt, |
| 139 | + MaybeTypeExpression, |
139 | 140 | MemberExpr, |
140 | 141 | MypyFile, |
141 | 142 | NamedTupleExpr, |
@@ -3541,7 +3542,7 @@ def analyze_lvalues(self, s: AssignmentStmt) -> None: |
3541 | 3542 |
|
3542 | 3543 | def analyze_rvalue_as_type_form(self, s: AssignmentStmt) -> None: |
3543 | 3544 | if TYPE_FORM in self.options.enable_incomplete_feature: |
3544 | | - s.rvalue.as_type = self.try_parse_as_type_expression(s.rvalue) |
| 3545 | + self.try_parse_as_type_expression(s.rvalue) |
3545 | 3546 |
|
3546 | 3547 | def apply_dynamic_class_hook(self, s: AssignmentStmt) -> None: |
3547 | 3548 | if not isinstance(s.rvalue, CallExpr): |
@@ -5278,7 +5279,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> None: |
5278 | 5279 | if s.expr: |
5279 | 5280 | s.expr.accept(self) |
5280 | 5281 | if TYPE_FORM in self.options.enable_incomplete_feature: |
5281 | | - s.expr.as_type = self.try_parse_as_type_expression(s.expr) |
| 5282 | + self.try_parse_as_type_expression(s.expr) |
5282 | 5283 |
|
5283 | 5284 | def visit_raise_stmt(self, s: RaiseStmt) -> None: |
5284 | 5285 | self.statement = s |
@@ -5823,7 +5824,7 @@ def visit_call_expr(self, expr: CallExpr) -> None: |
5823 | 5824 | for a in expr.args: |
5824 | 5825 | a.accept(self) |
5825 | 5826 | if calculate_type_forms: |
5826 | | - a.as_type = self.try_parse_as_type_expression(a) |
| 5827 | + self.try_parse_as_type_expression(a) |
5827 | 5828 |
|
5828 | 5829 | if ( |
5829 | 5830 | isinstance(expr.callee, MemberExpr) |
@@ -7618,9 +7619,16 @@ def visit_pass_stmt(self, o: PassStmt, /) -> None: |
7618 | 7619 | def visit_singleton_pattern(self, o: SingletonPattern, /) -> None: |
7619 | 7620 | return None |
7620 | 7621 |
|
7621 | | - def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> Type | None: |
7622 | | - """Try to parse maybe_type_expr as a type expression. |
7623 | | - If parsing fails return None and emit no errors.""" |
| 7622 | + def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> None: |
| 7623 | + """Try to parse a value Expression as a type expression. |
| 7624 | + If success then annotate the Expression with the type that it spells. |
| 7625 | + If fails then emit no errors and take no further action. |
| 7626 | +
|
| 7627 | + A value expression that is parsable as a type expression may be used |
| 7628 | + where a TypeForm is expected to represent the spelled type.""" |
| 7629 | + if not isinstance(maybe_type_expr, MaybeTypeExpression): |
| 7630 | + return |
| 7631 | + |
7624 | 7632 | # Save SemanticAnalyzer state |
7625 | 7633 | original_errors = self.errors # altered by fail() |
7626 | 7634 | original_num_incomplete_refs = ( |
@@ -7649,7 +7657,8 @@ def try_parse_as_type_expression(self, maybe_type_expr: Expression) -> Type | No |
7649 | 7657 | self.progress = original_progress |
7650 | 7658 | self.deferred = original_deferred |
7651 | 7659 | del self.deferral_debug_context[original_deferral_debug_context_len:] |
7652 | | - return t |
| 7660 | + |
| 7661 | + maybe_type_expr.as_type = t |
7653 | 7662 |
|
7654 | 7663 |
|
7655 | 7664 | def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike: |
|
0 commit comments