mirror of
https://github.com/astral-sh/ruff
synced 2026-01-23 14:30:53 -05:00
[ty] Avoid double-analyzing tuple in Final subscript (#21828)
## Summary
As-is, a single-element tuple gets destructured via:
```rust
let arguments = if let ast::Expr::Tuple(tuple) = slice {
&*tuple.elts
} else {
std::slice::from_ref(slice)
};
```
But then, because it's a single element, we call
`infer_annotation_expression_impl`, passing in the tuple, rather than
the first element.
Closes https://github.com/astral-sh/ty/issues/1793.
Closes https://github.com/astral-sh/ty/issues/1768.
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -273,10 +273,11 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
} 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, PEP613Policy::Disallowed);
|
||||
let type_and_qualifiers = if let [argument] = arguments {
|
||||
let mut type_and_qualifiers = self.infer_annotation_expression_impl(
|
||||
argument,
|
||||
PEP613Policy::Disallowed,
|
||||
);
|
||||
|
||||
match type_qualifier {
|
||||
SpecialFormType::ClassVar => {
|
||||
@@ -307,6 +308,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
if let Some(builder) =
|
||||
self.context.report_lint(&INVALID_TYPE_FORM, subscript)
|
||||
{
|
||||
let num_arguments = arguments.len();
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Type qualifier `{type_qualifier}` expected exactly 1 argument, \
|
||||
got {num_arguments}",
|
||||
@@ -325,10 +327,11 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
} 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, PEP613Policy::Disallowed);
|
||||
let type_and_qualifiers = if let [argument] = arguments {
|
||||
let mut type_and_qualifiers = self.infer_annotation_expression_impl(
|
||||
argument,
|
||||
PEP613Policy::Disallowed,
|
||||
);
|
||||
type_and_qualifiers.add_qualifier(TypeQualifiers::INIT_VAR);
|
||||
type_and_qualifiers
|
||||
} else {
|
||||
@@ -341,6 +344,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
|
||||
if let Some(builder) =
|
||||
self.context.report_lint(&INVALID_TYPE_FORM, subscript)
|
||||
{
|
||||
let num_arguments = arguments.len();
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Type qualifier `InitVar` expected exactly 1 argument, \
|
||||
got {num_arguments}",
|
||||
|
||||
Reference in New Issue
Block a user