[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:
@@ -0,0 +1,33 @@
|
||||
---
|
||||
source: crates/ty_test/src/lib.rs
|
||||
expression: snapshot
|
||||
---
|
||||
---
|
||||
mdtest name: instance.md - Instance subscript - `__getitem__` unbound
|
||||
mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/instance.md
|
||||
---
|
||||
|
||||
# Python source files
|
||||
|
||||
## mdtest_snippet.py
|
||||
|
||||
```
|
||||
1 | class NotSubscriptable: ...
|
||||
2 |
|
||||
3 | a = NotSubscriptable()[0] # error: [non-subscriptable]
|
||||
```
|
||||
|
||||
# Diagnostics
|
||||
|
||||
```
|
||||
error[non-subscriptable]: Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method
|
||||
--> src/mdtest_snippet.py:3:5
|
||||
|
|
||||
1 | class NotSubscriptable: ...
|
||||
2 |
|
||||
3 | a = NotSubscriptable()[0] # error: [non-subscriptable]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
info: rule `non-subscriptable` is enabled by default
|
||||
|
||||
```
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
## `__getitem__` unbound
|
||||
|
||||
<!-- snapshot-diagnostics -->
|
||||
|
||||
```py
|
||||
class NotSubscriptable: ...
|
||||
|
||||
a = NotSubscriptable()[0] # error: "Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method"
|
||||
a = NotSubscriptable()[0] # error: [non-subscriptable]
|
||||
```
|
||||
|
||||
## `__getitem__` not callable
|
||||
|
||||
@@ -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,
|
||||
) {
|
||||
|
||||
@@ -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__");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user