Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ d.a = 2

## Too many arguments

<!-- pull-types:skip -->

```py
from typing import ClassVar

class C:
# error: [invalid-type-form] "Type qualifier `typing.ClassVar` expects exactly one type parameter"
# error: [invalid-type-form] "Type qualifier `typing.ClassVar` expected exactly 1 argument, got 2"
x: ClassVar[int, str] = 1
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ reveal_type(FINAL_E) # revealed: int

## Too many arguments

<!-- pull-types:skip -->

```py
from typing import Final

class C:
# error: [invalid-type-form] "Type qualifier `typing.Final` expects exactly one type parameter"
# error: [invalid-type-form] "Type qualifier `typing.Final` expected exactly 1 argument, got 2"
x: Final[int, str] = 1
```

Expand Down
40 changes: 26 additions & 14 deletions crates/ty_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8659,19 +8659,14 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
}
Type::SpecialForm(
type_qualifier @ (SpecialFormType::ClassVar | SpecialFormType::Final),
) => match slice {
ast::Expr::Tuple(..) => {
if let Some(builder) =
self.context.report_lint(&INVALID_TYPE_FORM, subscript)
{
builder.into_diagnostic(format_args!(
"Type qualifier `{type_qualifier}` \
expects exactly one type parameter",
));
}
Type::unknown().into()
}
_ => {
) => {
let arguments = if let ast::Expr::Tuple(tuple) = slice {
&*tuple.elts
} else {
std::slice::from_ref(slice)
};
let num_arguments = arguments.len();
let type_and_qualifiers = if num_arguments == 1 {
let mut type_and_qualifiers =
self.infer_annotation_expression_impl(slice);
match type_qualifier {
Expand All @@ -8684,8 +8679,25 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
_ => unreachable!(),
}
type_and_qualifiers
} else {
for element in arguments {
self.infer_annotation_expression_impl(element);
}
if let Some(builder) =
self.context.report_lint(&INVALID_TYPE_FORM, subscript)
{
builder.into_diagnostic(format_args!(
"Type qualifier `{type_qualifier}` expected exactly 1 argument, \
got {num_arguments}",
));
}
Type::unknown().into()
};
if slice.is_tuple_expr() {
self.store_expression_type(slice, type_and_qualifiers.inner_type());
}
},
type_and_qualifiers
}
_ => self
.infer_subscript_type_expression_no_store(subscript, slice, value_ty)
.into(),
Expand Down
Loading