mirror of
https://github.com/astral-sh/ruff.git
synced 2026-12-03 02:55:52 +08:00
[ty] Avoid emitting cascading diagnostics when parsing invalid type expressions (#24326)
## Summary In lots of places in our type-expression parsing, we continue to call `self.infer_type_expression()` on sub-expressions in the AST even after we've already determined that the type expression is invalid. I think that's generally a mistake; it often leads to us emitting many diagnostics on a single type expression when one would really be sufficient. This PR switches many callsites from `infer_type_expression` to `infer_expression`, to avoid this phenomenon of cascading diagnostics in error cases. ## Test Plan Mdtests and snapshots updated.
This commit is contained in:
@@ -102,7 +102,6 @@ async def outer_async(): # avoid unrelated syntax errors on `yield` and `await`
|
||||
# error: [unsupported-operator]
|
||||
# error: [invalid-type-form] "F-strings are not allowed in type expressions"
|
||||
p: int | f"foo",
|
||||
# error: [invalid-type-form] "Slices are not allowed in type expressions"
|
||||
# error: [invalid-type-form] "Invalid subscript"
|
||||
q: [1, 2, 3][1:2],
|
||||
):
|
||||
@@ -159,6 +158,37 @@ def invalid_binary_operators(
|
||||
reveal_type(l) # revealed: Unknown
|
||||
```
|
||||
|
||||
## Error recovery upon encountering invalid AST nodes
|
||||
|
||||
Upon encountering an invalid-in-type-expression AST node, we try to avoid cascading diagnostics. For
|
||||
example, in this snippet, we only report the the outer list literal is invalid, and ignore the fact
|
||||
that there is also an invalid list literal inside the outer list literal node:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-form]
|
||||
x: [[int]]
|
||||
```
|
||||
|
||||
However, runtime errors inside invalid AST nodes are still reported -- these errors are more serious
|
||||
than just "typing spec pedantry":
|
||||
|
||||
```py
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
|
||||
# error: [unresolved-reference] "Name `foo` used when not defined"
|
||||
x: [[foo]]
|
||||
```
|
||||
|
||||
But we avoid false-positive diagnostics regarding unresolved references inside string annotations if
|
||||
we detect that the string annotation is an invalid type form. These diagnostics would just add
|
||||
noise, since stringized annotations are never executed at runtime. The following snippet causes us
|
||||
to emit `invalid-type-form`, but we ignore that `foo` is an "unresolved reference" inside the string
|
||||
annotation:
|
||||
|
||||
```py
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
|
||||
x: "[[foo]]"
|
||||
```
|
||||
|
||||
## Multiple starred expressions in a `tuple` specialization
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
@@ -246,7 +276,6 @@ async def outer_async(): # avoid unrelated syntax errors on `yield` and `await`
|
||||
l: "(yield 1)", # error: [invalid-type-form] "`yield` expressions are not allowed in type expressions"
|
||||
m: "1 < 2", # error: [invalid-type-form] "Comparison expressions are not allowed in type expressions"
|
||||
n: "bar()", # error: [invalid-type-form] "Function calls are not allowed in type expressions"
|
||||
# error: [invalid-type-form] "Slices are not allowed in type expressions"
|
||||
# error: [invalid-type-form] "Invalid subscript"
|
||||
o: "[1, 2, 3][1:2]",
|
||||
):
|
||||
@@ -282,7 +311,7 @@ def _(
|
||||
d: [k for k in [1, 2]], # error: [invalid-type-form] "List comprehensions are not allowed in type expressions"
|
||||
e: {k for k in [1, 2]}, # error: [invalid-type-form] "Set comprehensions are not allowed in type expressions"
|
||||
f: (k for k in [1, 2]), # error: [invalid-type-form] "Generator expressions are not allowed in type expressions"
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?"
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
|
||||
g: [int, str],
|
||||
# error: [invalid-type-form] "Tuple literals are not allowed in this context in a type expression: Did you mean `tuple[int, str]`?"
|
||||
h: (int, str),
|
||||
@@ -303,7 +332,6 @@ class name_0[name_2: [int]]:
|
||||
pass
|
||||
|
||||
# error: [invalid-type-form] "List literals are not allowed in this context in a type expression"
|
||||
# error: [invalid-type-form] "Dict literals are not allowed in type expressions"
|
||||
class name_4[name_1: [{}]]:
|
||||
pass
|
||||
```
|
||||
@@ -340,16 +368,15 @@ from PIL import Image
|
||||
def g(x: Image): ... # error: [invalid-type-form]
|
||||
```
|
||||
|
||||
### List-literal used when you meant to use a list or tuple
|
||||
### List-literal used when you meant to use a list
|
||||
|
||||
```py
|
||||
def _(
|
||||
x: [int], # error: [invalid-type-form]
|
||||
) -> [int]: # error: [invalid-type-form]
|
||||
return x
|
||||
```
|
||||
|
||||
```py
|
||||
# No special hints for these: it's unclear what the user meant:
|
||||
def _(
|
||||
x: [int, str], # error: [invalid-type-form]
|
||||
) -> [int, str]: # error: [invalid-type-form]
|
||||
|
||||
@@ -329,7 +329,6 @@ from other import Literal
|
||||
#
|
||||
# ?
|
||||
#
|
||||
# error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
|
||||
# error: [invalid-type-form] "Invalid subscript of object of type `_SpecialForm` in type expression"
|
||||
a1: Literal[26]
|
||||
|
||||
|
||||
@@ -354,12 +354,8 @@ o: "1 < 2"
|
||||
# error: [invalid-type-form]
|
||||
p: "call()"
|
||||
# error: [invalid-type-form] "List literals are not allowed"
|
||||
# error: [invalid-type-form] "Int literals are not allowed"
|
||||
# error: [invalid-type-form] "Int literals are not allowed"
|
||||
r: "[1, 2]"
|
||||
# error: [invalid-type-form] "Tuple literals are not allowed"
|
||||
# error: [invalid-type-form] "Int literals are not allowed"
|
||||
# error: [invalid-type-form] "Int literals are not allowed"
|
||||
s: "(1, 2)"
|
||||
```
|
||||
|
||||
|
||||
@@ -109,7 +109,6 @@ from typing_extensions import Self, TypeAlias, TypeVar
|
||||
T = TypeVar("T")
|
||||
|
||||
# error: [invalid-type-form] "Special form `typing.TypeAlias` expected no type parameter"
|
||||
# error: [unbound-type-variable]
|
||||
X: TypeAlias[T] = int
|
||||
|
||||
class Foo[T]:
|
||||
|
||||
@@ -678,8 +678,15 @@ def _(doubly_specialized: DoublySpecialized):
|
||||
# error: [not-subscriptable] "Cannot subscript non-generic type `<class 'list[int]'>`"
|
||||
List = list[int][int]
|
||||
|
||||
def _(doubly_specialized: List):
|
||||
# TODO: one error would be enough here
|
||||
#
|
||||
# error: [not-subscriptable] "Cannot subscript non-generic type `<class 'list[int]'>`"
|
||||
# error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
|
||||
WorseList = list[int][0]
|
||||
|
||||
def _(doubly_specialized: List, doubly_specialized_2: WorseList):
|
||||
reveal_type(doubly_specialized) # revealed: Unknown
|
||||
reveal_type(doubly_specialized_2) # revealed: Unknown
|
||||
|
||||
Tuple = tuple[int, str]
|
||||
|
||||
|
||||
@@ -188,7 +188,6 @@ a = 123
|
||||
def f(_) -> TypeGuard[int, str]: ...
|
||||
|
||||
# error: [invalid-type-form] "Special form `typing.TypeIs` expected exactly one type parameter"
|
||||
# error: [invalid-type-form] "Variable of type `Literal[123]` is not allowed in a type expression"
|
||||
def g(_) -> TypeIs[a, str]: ...
|
||||
|
||||
reveal_type(f(0)) # revealed: Unknown
|
||||
|
||||
+28
-27
@@ -4,7 +4,7 @@ expression: snapshot
|
||||
---
|
||||
|
||||
---
|
||||
mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - List-literal used when you meant to use a list or tuple
|
||||
mdtest name: invalid.md - Tests for invalid types in type expressions - Diagnostics for common errors - List-literal used when you meant to use a list
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
|
||||
---
|
||||
|
||||
@@ -13,14 +13,16 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/annotations/invalid.md
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | def _(
|
||||
2 | x: [int], # error: [invalid-type-form]
|
||||
3 | ) -> [int]: # error: [invalid-type-form]
|
||||
4 | return x
|
||||
5 | def _(
|
||||
6 | x: [int, str], # error: [invalid-type-form]
|
||||
7 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
8 | return x
|
||||
1 | def _(
|
||||
2 | x: [int], # error: [invalid-type-form]
|
||||
3 | ) -> [int]: # error: [invalid-type-form]
|
||||
4 | return x
|
||||
5 |
|
||||
6 | # No special hints for these: it's unclear what the user meant:
|
||||
7 | def _(
|
||||
8 | x: [int, str], # error: [invalid-type-form]
|
||||
9 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
10 | return x
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
@@ -50,7 +52,6 @@ error[invalid-type-form]: List literals are not allowed in this context in a typ
|
||||
3 | ) -> [int]: # error: [invalid-type-form]
|
||||
| ^^^^^ Did you mean `list[int]`?
|
||||
4 | return x
|
||||
5 | def _(
|
||||
|
|
||||
info: See the following page for a reference on valid type expressions:
|
||||
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
|
||||
@@ -60,15 +61,15 @@ info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
error[invalid-type-form]: List literals are not allowed in this context in a type expression
|
||||
--> src/mdtest_snippet.py:6:8
|
||||
|
|
||||
4 | return x
|
||||
5 | def _(
|
||||
6 | x: [int, str], # error: [invalid-type-form]
|
||||
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
|
||||
7 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
8 | return x
|
||||
|
|
||||
--> src/mdtest_snippet.py:8:8
|
||||
|
|
||||
6 | # No special hints for these: it's unclear what the user meant:
|
||||
7 | def _(
|
||||
8 | x: [int, str], # error: [invalid-type-form]
|
||||
| ^^^^^^^^^^
|
||||
9 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
10 | return x
|
||||
|
|
||||
info: See the following page for a reference on valid type expressions:
|
||||
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
@@ -77,14 +78,14 @@ info: rule `invalid-type-form` is enabled by default
|
||||
|
||||
```
|
||||
error[invalid-type-form]: List literals are not allowed in this context in a type expression
|
||||
--> src/mdtest_snippet.py:7:6
|
||||
|
|
||||
5 | def _(
|
||||
6 | x: [int, str], # error: [invalid-type-form]
|
||||
7 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
| ^^^^^^^^^^ Did you mean `tuple[int, str]`?
|
||||
8 | return x
|
||||
|
|
||||
--> src/mdtest_snippet.py:9:6
|
||||
|
|
||||
7 | def _(
|
||||
8 | x: [int, str], # error: [invalid-type-form]
|
||||
9 | ) -> [int, str]: # error: [invalid-type-form]
|
||||
| ^^^^^^^^^^
|
||||
10 | return x
|
||||
|
|
||||
info: See the following page for a reference on valid type expressions:
|
||||
info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions
|
||||
info: rule `invalid-type-form` is enabled by default
|
||||
@@ -1705,6 +1705,72 @@ impl<'db> Type<'db> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return `true` if `self` is a type that is suitable for displaying
|
||||
/// in a "Did you mean...?" hint message in diagnostics
|
||||
fn is_hintable(&self, db: &'db dyn Db) -> bool {
|
||||
match self {
|
||||
Type::NominalInstance(_)
|
||||
| Type::NewTypeInstance(_)
|
||||
| Type::LiteralValue(_)
|
||||
| Type::TypeAlias(_) => true,
|
||||
|
||||
Type::Intersection(_)
|
||||
| Type::Divergent(_)
|
||||
| Type::SpecialForm(_)
|
||||
| Type::BoundSuper(_)
|
||||
| Type::BoundMethod(_)
|
||||
| Type::KnownBoundMethod(_)
|
||||
| Type::AlwaysTruthy
|
||||
| Type::AlwaysFalsy
|
||||
| Type::TypeIs(_)
|
||||
| Type::TypeGuard(_)
|
||||
| Type::PropertyInstance(_)
|
||||
| Type::FunctionLiteral(_)
|
||||
| Type::ModuleLiteral(_)
|
||||
| Type::WrapperDescriptor(_)
|
||||
| Type::DataclassDecorator(_)
|
||||
| Type::DataclassTransformer(_)
|
||||
| Type::ClassLiteral(_)
|
||||
| Type::GenericAlias(_)
|
||||
| Type::KnownInstance(_) => false,
|
||||
|
||||
// `Never` is spellable and could result from an explicit type annotation,
|
||||
// but also could just be the result of us inferring an unreachable region.
|
||||
// Best to avoid showing it in hints.
|
||||
Type::Never => false,
|
||||
|
||||
// All `Callable` types are spellable in some way,
|
||||
// but they're generally not spellable with the syntax we use by default
|
||||
// in our type display
|
||||
Type::Callable(_) => false,
|
||||
|
||||
Type::SubclassOf(subclass_of) => match subclass_of.subclass_of() {
|
||||
SubclassOfInner::Class(_) => true,
|
||||
SubclassOfInner::Dynamic(dynamic) => Type::Dynamic(dynamic).is_hintable(db),
|
||||
SubclassOfInner::TypeVar(tvar) => Type::TypeVar(tvar).is_hintable(db),
|
||||
},
|
||||
|
||||
Type::TypeVar(tvar) => tvar.typevar(db).definition(db).is_some(),
|
||||
|
||||
Type::Union(union) => union.elements(db).iter().all(|ty| ty.is_hintable(db)),
|
||||
|
||||
Type::TypedDict(td) => td.defining_class().is_some(),
|
||||
|
||||
Type::ProtocolInstance(ProtocolInstanceType { inner, .. }) => !inner.is_synthesized(),
|
||||
|
||||
Type::Dynamic(dynamic) => match dynamic {
|
||||
DynamicType::Any => true,
|
||||
DynamicType::Unknown
|
||||
| DynamicType::UnknownGeneric(_)
|
||||
| DynamicType::UnspecializedTypeVar
|
||||
| DynamicType::TodoUnpack
|
||||
| DynamicType::TodoTypeVarTuple
|
||||
| DynamicType::Todo(_)
|
||||
| DynamicType::TodoStarredExpression => false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// If the type is a union (or a type alias that resolves to a union), filters union elements
|
||||
/// based on the provided predicate.
|
||||
///
|
||||
|
||||
@@ -536,6 +536,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
self.context.in_stub()
|
||||
}
|
||||
|
||||
fn in_string_annotation(&self) -> bool {
|
||||
self.deferred_state.in_string_annotation()
|
||||
}
|
||||
|
||||
/// Returns `true` if `expr` is a call to a known diagnostic function
|
||||
/// (e.g., `reveal_type` or `assert_type`) whose return value should not
|
||||
/// trigger the `unused-awaitable` lint.
|
||||
@@ -7866,7 +7870,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
place_from_bindings(db, use_def.reachable_bindings(place_id)).place
|
||||
} else {
|
||||
assert!(
|
||||
self.deferred_state.in_string_annotation(),
|
||||
self.in_string_annotation(),
|
||||
"Expected the place table to create a place for every valid PlaceExpr node"
|
||||
);
|
||||
Place::Undefined
|
||||
@@ -9330,7 +9334,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
|
||||
///
|
||||
/// The inference results can be merged into the current inference region using
|
||||
/// [`TypeInferenceBuilder::extend`].
|
||||
fn speculate(&mut self) -> Self {
|
||||
fn speculate(&self) -> Self {
|
||||
let Self {
|
||||
region,
|
||||
index,
|
||||
|
||||
@@ -172,6 +172,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
{
|
||||
builder.into_diagnostic("Type expressions cannot use bytes literal");
|
||||
}
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_bytes_literal_expression(bytes);
|
||||
}
|
||||
TypeAndQualifiers::declared(Type::unknown())
|
||||
}
|
||||
|
||||
@@ -179,7 +182,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
if let Some(builder) = self.context.report_lint(&FSTRING_TYPE_ANNOTATION, fstring) {
|
||||
builder.into_diagnostic("Type expressions cannot use f-strings");
|
||||
}
|
||||
self.infer_fstring_expression(fstring);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_fstring_expression(fstring);
|
||||
}
|
||||
TypeAndQualifiers::declared(Type::unknown())
|
||||
}
|
||||
|
||||
|
||||
@@ -282,7 +282,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
// Avoid inferring the types of invalid binary expressions that have been
|
||||
// parsed from a string annotation, as they are not present in the semantic
|
||||
// index.
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_binary_expression(binary, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -364,28 +364,23 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
ast::Expr::List(list) => {
|
||||
let db = self.db();
|
||||
|
||||
let inner_types: Vec<Type<'db>> = list
|
||||
.iter()
|
||||
.map(|element| self.infer_type_expression(element))
|
||||
.collect();
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_list_expression(list, TypeContext::default());
|
||||
}
|
||||
|
||||
if let Some(mut diagnostic) = self.report_invalid_type_expression(
|
||||
expression,
|
||||
format_args!(
|
||||
"List literals are not allowed in this context in a type expression"
|
||||
),
|
||||
) {
|
||||
if !inner_types.iter().any(|ty| {
|
||||
matches!(
|
||||
ty,
|
||||
Type::Dynamic(DynamicType::Todo(_) | DynamicType::Unknown)
|
||||
)
|
||||
}) {
|
||||
let hinted_type = if list.len() == 1 {
|
||||
KnownClass::List.to_specialized_instance(db, inner_types)
|
||||
} else {
|
||||
Type::heterogeneous_tuple(db, inner_types)
|
||||
};
|
||||
) && let [single_element] = &*list.elts
|
||||
{
|
||||
let mut speculative_builder = self.speculate();
|
||||
let inner_type = speculative_builder.infer_type_expression(single_element);
|
||||
|
||||
if inner_type.is_hintable(self.db()) {
|
||||
let hinted_type =
|
||||
KnownClass::List.to_specialized_instance(db, &[inner_type]);
|
||||
|
||||
diagnostic.set_primary_message(format_args!(
|
||||
"Did you mean `{}`?",
|
||||
@@ -397,25 +392,27 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Tuple(tuple) => {
|
||||
let inner_types: Vec<Type<'db>> = tuple
|
||||
.elts
|
||||
.iter()
|
||||
.map(|expr| self.infer_type_expression(expr))
|
||||
.collect();
|
||||
|
||||
if tuple.parenthesized {
|
||||
if !self.in_string_annotation() {
|
||||
for element in tuple {
|
||||
self.infer_expression(element, TypeContext::default());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(mut diagnostic) = self.report_invalid_type_expression(
|
||||
expression,
|
||||
format_args!(
|
||||
"Tuple literals are not allowed in this context in a type expression"
|
||||
),
|
||||
) {
|
||||
if !inner_types.iter().any(|ty| {
|
||||
matches!(
|
||||
ty,
|
||||
Type::Dynamic(DynamicType::Todo(_) | DynamicType::Unknown)
|
||||
)
|
||||
}) {
|
||||
let mut speculative = self.speculate();
|
||||
let inner_types: Vec<Type<'db>> = tuple
|
||||
.elts
|
||||
.iter()
|
||||
.map(|element| speculative.infer_type_expression(element))
|
||||
.collect();
|
||||
|
||||
if inner_types.iter().all(|ty| ty.is_hintable(self.db())) {
|
||||
let hinted_type = Type::heterogeneous_tuple(self.db(), inner_types);
|
||||
diagnostic.set_primary_message(format_args!(
|
||||
"Did you mean `{}`?",
|
||||
@@ -423,12 +420,17 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for element in tuple {
|
||||
self.infer_type_expression(element);
|
||||
}
|
||||
}
|
||||
|
||||
Type::unknown()
|
||||
}
|
||||
|
||||
ast::Expr::BoolOp(bool_op) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_boolean_expression(bool_op);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -439,7 +441,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Named(named) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_named_expression(named);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -450,7 +452,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::UnaryOp(unary) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_unary_expression(unary);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -461,7 +463,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Lambda(lambda_expression) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_lambda_expression(lambda_expression, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -472,7 +474,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::If(if_expression) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_if_expression(if_expression, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -483,7 +485,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Dict(dict) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_dict_expression(dict, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -494,7 +496,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Set(set) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_set_expression(set, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -505,7 +507,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::DictComp(dictcomp) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_dict_comprehension_expression(dictcomp, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -516,7 +518,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::ListComp(listcomp) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_list_comprehension_expression(listcomp, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -527,7 +529,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::SetComp(setcomp) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_set_comprehension_expression(setcomp, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -538,7 +540,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Generator(generator) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_generator_expression(generator);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -549,7 +551,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Await(await_expression) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_await_expression(await_expression, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -560,7 +562,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Yield(yield_expression) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_yield_expression(yield_expression);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -571,7 +573,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::YieldFrom(yield_from) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_yield_from_expression(yield_from);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -582,7 +584,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Compare(compare) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_compare_expression(compare);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -593,7 +595,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Call(call_expr) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_call_expression(call_expr, TypeContext::default());
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -604,7 +606,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::FString(fstring) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_fstring_expression(fstring);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -615,7 +617,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::TString(tstring) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_tstring_expression(tstring);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -626,7 +628,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
|
||||
ast::Expr::Slice(slice) => {
|
||||
if !self.deferred_state.in_string_annotation() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_slice_expression(slice);
|
||||
}
|
||||
self.report_invalid_type_expression(
|
||||
@@ -938,7 +940,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
union_ty
|
||||
}
|
||||
ast::Expr::Tuple(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, slice) {
|
||||
builder.into_diagnostic("type[...] must have exactly one type argument");
|
||||
}
|
||||
@@ -1003,7 +1007,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
None => {
|
||||
// TODO: emit a diagnostic if you try to specialize a non-generic class.
|
||||
self.infer_type_expression(parameters);
|
||||
self.infer_expression(parameters, TypeContext::default());
|
||||
todo_type!("specialized non-generic class")
|
||||
}
|
||||
}
|
||||
@@ -1017,7 +1021,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
invalid_type_argument(self, slice)
|
||||
}
|
||||
_ => {
|
||||
self.infer_type_expression(parameters);
|
||||
self.infer_expression(parameters, TypeContext::default());
|
||||
todo_type!("unsupported nested subscript in type[X]")
|
||||
}
|
||||
};
|
||||
@@ -1026,7 +1030,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
// TODO: subscripts, etc.
|
||||
_ => {
|
||||
self.infer_type_expression(slice);
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
todo_type!("unsupported type[X] special form")
|
||||
}
|
||||
}
|
||||
@@ -1141,7 +1145,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
// `infer_expression` (instead of `infer_type_expression`) here to avoid
|
||||
// false-positive `invalid-type-form` diagnostics (`1` is not a valid type
|
||||
// expression).
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
Type::unknown()
|
||||
}
|
||||
Type::SpecialForm(special_form) => {
|
||||
@@ -1149,7 +1155,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
Type::KnownInstance(known_instance) => match known_instance {
|
||||
KnownInstanceType::SubscriptedProtocol(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`typing.Protocol` is not allowed in type expressions",
|
||||
@@ -1158,7 +1166,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::SubscriptedGeneric(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`typing.Generic` is not allowed in type expressions",
|
||||
@@ -1167,7 +1177,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::Deprecated(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`warnings.deprecated` is not allowed in type expressions",
|
||||
@@ -1176,7 +1188,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::Field(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`dataclasses.Field` is not allowed in type expressions",
|
||||
@@ -1185,7 +1199,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::ConstraintSet(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`ty_extensions.ConstraintSet` is not allowed in type expressions",
|
||||
@@ -1194,7 +1210,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::GenericContext(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`ty_extensions.GenericContext` is not allowed in type expressions",
|
||||
@@ -1203,7 +1221,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::Specialization(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`ty_extensions.Specialization` is not allowed in type expressions",
|
||||
@@ -1213,6 +1233,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
KnownInstanceType::TypeAliasType(type_alias @ TypeAliasType::PEP695(_)) => {
|
||||
if type_alias.specialization(self.db()).is_some() {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) =
|
||||
self.context.report_lint(&NOT_SUBSCRIPTABLE, subscript)
|
||||
{
|
||||
@@ -1242,8 +1265,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
.unwrap_or(Type::unknown())
|
||||
}
|
||||
None => {
|
||||
self.infer_type_expression(slice);
|
||||
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) =
|
||||
self.context.report_lint(&NOT_SUBSCRIPTABLE, subscript)
|
||||
{
|
||||
@@ -1284,11 +1308,13 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::Dynamic(DynamicType::UnknownGeneric(generic_context))
|
||||
}
|
||||
KnownInstanceType::LiteralStringAlias(_) => {
|
||||
self.infer_type_expression(slice);
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
todo_type!("Generic stringified PEP-613 type alias")
|
||||
}
|
||||
KnownInstanceType::Literal(ty) => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`{ty}` is not a generic class",
|
||||
@@ -1308,6 +1334,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
if typevar.identity(self.db()).kind(self.db()) == TypeVarKind::Pep613Alias {
|
||||
self.infer_explicit_type_alias_specialization(subscript, value_ty, false)
|
||||
} else {
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) =
|
||||
self.context.report_lint(&INVALID_TYPE_FORM, subscript)
|
||||
{
|
||||
@@ -1326,7 +1355,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
self.infer_explicit_type_alias_specialization(subscript, value_ty, true)
|
||||
}
|
||||
KnownInstanceType::NewType(newtype) => {
|
||||
self.infer_type_expression(&subscript.slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(&subscript.slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`{}` is a `NewType` and cannot be specialized",
|
||||
@@ -1336,7 +1367,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
Type::unknown()
|
||||
}
|
||||
KnownInstanceType::NamedTupleSpec(_) => {
|
||||
self.infer_type_expression(&subscript.slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(&subscript.slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`NamedTuple` specs cannot be specialized",
|
||||
@@ -1352,7 +1385,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
// Infer slice as a value expression to avoid false-positive
|
||||
// `invalid-type-form` diagnostics, when we have e.g.
|
||||
// `MyCallable[[int, str], None]` but `MyCallable` is dynamic.
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
value_ty
|
||||
}
|
||||
Type::ClassLiteral(class) => {
|
||||
@@ -1376,7 +1411,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
_ => {
|
||||
// TODO: emit a diagnostic if you try to specialize a non-generic class.
|
||||
self.infer_type_expression(slice);
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
todo_type!("specialized non-generic class")
|
||||
}
|
||||
}
|
||||
@@ -1385,7 +1420,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
self.infer_explicit_type_alias_specialization(subscript, value_ty, true)
|
||||
}
|
||||
Type::LiteralValue(literal) if literal.is_string() => {
|
||||
self.infer_type_expression(slice);
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
// For stringified TypeAlias; remove once properly supported
|
||||
todo_type!("string literal subscripted in type expression")
|
||||
}
|
||||
@@ -1400,7 +1435,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
})
|
||||
}
|
||||
_ => {
|
||||
self.infer_type_expression(slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Invalid subscript of object of type `{}` in type expression",
|
||||
@@ -1616,8 +1653,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
let negated_type = if num_arguments == 1 {
|
||||
self.infer_type_expression(&arguments[0]).negate(db)
|
||||
} else {
|
||||
for argument in arguments {
|
||||
self.infer_type_expression(argument);
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
report_invalid_argument_number_to_special_form(
|
||||
&self.context,
|
||||
@@ -1660,8 +1699,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
let arg = if num_arguments == 1 {
|
||||
self.infer_type_expression(&arguments[0])
|
||||
} else {
|
||||
for argument in arguments {
|
||||
self.infer_type_expression(argument);
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
report_invalid_argument_number_to_special_form(
|
||||
&self.context,
|
||||
@@ -1684,8 +1725,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
let arg = if num_arguments == 1 {
|
||||
self.infer_type_expression(&arguments[0])
|
||||
} else {
|
||||
for argument in arguments {
|
||||
self.infer_type_expression(argument);
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
report_invalid_argument_number_to_special_form(
|
||||
&self.context,
|
||||
@@ -1709,8 +1752,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
// N.B. This uses `infer_expression` rather than `infer_type_expression`
|
||||
self.infer_expression(&arguments[0], TypeContext::default())
|
||||
} else {
|
||||
for argument in arguments {
|
||||
self.infer_type_expression(argument);
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
report_invalid_argument_number_to_special_form(
|
||||
&self.context,
|
||||
@@ -1736,8 +1781,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
let num_arguments = arguments.len();
|
||||
|
||||
if num_arguments != 1 {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
report_invalid_argument_number_to_special_form(
|
||||
&self.context,
|
||||
@@ -1802,7 +1849,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
}
|
||||
SpecialFormType::TypeIs => match arguments_slice {
|
||||
ast::Expr::Tuple(_) => {
|
||||
self.infer_type_expression(arguments_slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
}
|
||||
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
let diag = builder.into_diagnostic(
|
||||
@@ -1835,7 +1884,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
},
|
||||
SpecialFormType::TypeGuard => match arguments_slice {
|
||||
ast::Expr::Tuple(_) => {
|
||||
self.infer_type_expression(arguments_slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
}
|
||||
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
let diag = builder.into_diagnostic(
|
||||
@@ -1915,7 +1966,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
| SpecialFormType::Never
|
||||
| SpecialFormType::AlwaysTruthy
|
||||
| SpecialFormType::AlwaysFalsy => {
|
||||
self.infer_type_expression(arguments_slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
}
|
||||
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
@@ -1930,7 +1983,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
| SpecialFormType::Unknown
|
||||
| SpecialFormType::Any
|
||||
| SpecialFormType::NamedTuple => {
|
||||
self.infer_type_expression(arguments_slice);
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
}
|
||||
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
@@ -1989,7 +2044,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
SpecialFormType::Type => self.infer_subclass_of_type_expression(arguments_slice),
|
||||
SpecialFormType::Tuple => Type::tuple(self.infer_tuple_type_expression(subscript)),
|
||||
SpecialFormType::Generic | SpecialFormType::Protocol => {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(arguments_slice, TypeContext::default());
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"`{special_form}` is not allowed in type expressions",
|
||||
@@ -2099,7 +2156,9 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
return Err(vec![parameters]);
|
||||
}
|
||||
_ => {
|
||||
self.infer_expression(parameters, TypeContext::default());
|
||||
if !self.in_string_annotation() {
|
||||
self.infer_expression(parameters, TypeContext::default());
|
||||
}
|
||||
return Err(vec![parameters]);
|
||||
}
|
||||
};
|
||||
@@ -2244,8 +2303,10 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
let (last_arg, prefix_args) = match arguments.split_last() {
|
||||
Some((last_arg, prefix_args)) if !prefix_args.is_empty() => (last_arg, prefix_args),
|
||||
_ => {
|
||||
for argument in arguments {
|
||||
self.infer_type_expression(argument);
|
||||
if !self.in_string_annotation() {
|
||||
for argument in arguments {
|
||||
self.infer_expression(argument, TypeContext::default());
|
||||
}
|
||||
}
|
||||
if let Some(builder) = self.context.report_lint(&INVALID_TYPE_FORM, subscript) {
|
||||
builder.into_diagnostic(format_args!(
|
||||
|
||||
@@ -829,6 +829,10 @@ impl<'db> Protocol<'db> {
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) const fn is_synthesized(self) -> bool {
|
||||
matches!(self, Self::Synthesized(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'db> VarianceInferable<'db> for Protocol<'db> {
|
||||
|
||||
Reference in New Issue
Block a user