[ty] Improve diagnostic range for non-subscriptable diagnostics (#21461)

## Summary

Currently our diagnostic only covers the range of the thing being
subscripted:

<img width="1702" height="312" alt="image"
src="https://github.com/user-attachments/assets/7e630431-e846-46ca-93c1-139f11aaba11"
/>

But it should probably cover the _whole_ subscript expression (arguably
the more "incorrect" bit is the `["foo"]` part of this expression, not
the `x` part of this expression!)

## Test Plan

Added a snapshot

Co-authored-by: Brent Westbrook
<36778786+ntBre@users.noreply.github.com>
This commit is contained in:
Alex Waygood
2025-11-14 21:15:14 +00:00
committed by GitHub
parent c5d654bce8
commit d63b4b0383
6 changed files with 54 additions and 19 deletions

View File

@@ -2034,7 +2034,7 @@ pub(super) fn report_index_out_of_bounds(
/// Emit a diagnostic declaring that a type does not support subscripting.
pub(super) fn report_non_subscriptable(
context: &InferContext,
node: AnyNodeRef,
node: &ast::ExprSubscript,
non_subscriptable_ty: Type,
method: &str,
) {

View File

@@ -11369,11 +11369,11 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
.as_class_literal()
.is_some_and(|class| class.iter_mro(db, None).contains(&ClassBase::Generic))
{
report_non_subscriptable(context, value_node.into(), value_ty, "__class_getitem__");
report_non_subscriptable(context, subscript, value_ty, "__class_getitem__");
}
} else {
if expr_context != ExprContext::Store {
report_non_subscriptable(context, value_node.into(), value_ty, "__getitem__");
report_non_subscriptable(context, subscript, value_ty, "__getitem__");
}
}