Skip to content

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

Merged
AlexWaygood merged 1 commit intomainfrom
alex-brent/subscript-diagnostic-range
Nov 14, 2025
Merged

[ty] Improve diagnostic range for non-subscriptable diagnostics#21461
AlexWaygood merged 1 commit intomainfrom
alex-brent/subscript-diagnostic-range

Conversation

@AlexWaygood
Copy link
Member

Summary

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

image

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

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Nov 14, 2025
@AlexWaygood AlexWaygood added the diagnostics Related to reporting of diagnostics. label Nov 14, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 14, 2025

mypy_primer results

Changes were detected when running on open source projects
pandas (https://github.com/pandas-dev/pandas)
- pandas/tests/series/test_arithmetic.py:646:17: error[non-subscriptable] Cannot subscript object of type `bool` with no `__getitem__` method
+ pandas/tests/series/test_arithmetic.py:646:16: error[non-subscriptable] Cannot subscript object of type `bool` with no `__getitem__` method
- pandas/tests/series/test_arithmetic.py:647:21: error[non-subscriptable] Cannot subscript object of type `bool` with no `__getitem__` method
+ pandas/tests/series/test_arithmetic.py:647:20: error[non-subscriptable] Cannot subscript object of type `bool` with no `__getitem__` method

sympy (https://github.com/sympy/sympy)
- sympy/parsing/autolev/test-examples/ruletest6.py:31:6: error[non-subscriptable] Cannot subscript object of type `<module 'math'>` with no `__getitem__` method
+ sympy/parsing/autolev/test-examples/ruletest6.py:31:5: error[non-subscriptable] Cannot subscript object of type `<module 'math'>` with no `__getitem__` method

No memory usage changes detected ✅

@AlexWaygood AlexWaygood force-pushed the alex-brent/subscript-diagnostic-range branch from de9cc2a to 6b45718 Compare November 14, 2025 18:21
@carljm
Copy link
Contributor

carljm commented Nov 14, 2025

I think highlighting the whole expression (as you do here) is best. It is both the type of the base object (which is the type that is not subscriptable), and the fact that you are subscripting it, that's "at fault" for the error. (In the case of not-subscriptable, the actual key is arguably not involved at all! But it still makes sense to highlight the entire expression IMO.)

@MichaReiser
Copy link
Member

MichaReiser commented Nov 14, 2025

Similar to attribute expressions, I think it's undesired to highlight the entire object because it can get very noisy if that expression is long (think a call expression)

I think the ideal is to highlight .attribute or [value].

Related discussion astral-sh/ty#169

I don't think this should block this PR because it at least makes it consistent but we should update said issue to also mention subscript

@AlexWaygood
Copy link
Member Author

Heh, well, I disagree for the same reasons I gave in astral-sh/ty#169 ;) but I'll add a note in that issue to mention that the same discussion also applies to subscript diagnostics!

@AlexWaygood AlexWaygood merged commit d63b4b0 into main Nov 14, 2025
41 checks passed
@AlexWaygood AlexWaygood deleted the alex-brent/subscript-diagnostic-range branch November 14, 2025 21:15
dcreager added a commit that referenced this pull request Nov 14, 2025
* origin/main: (59 commits)
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  [ty] Support legacy `typing` special forms in implicit type aliases (#21433)
  Bump 0.14.5 (#21435)
  [ty] Support `type[…]` and `Type[…]` in implicit type aliases (#21421)
  [ty] Respect notebook cell boundaries when adding an auto import (#21322)
  Update PyCharm setup instructions (#21409)
  ...
dcreager added a commit that referenced this pull request Nov 14, 2025
* dcreager/deep-comparison: (64 commits)
  assuming
  SubtypingAssuming
  implies_subtype_of
  name tweak
  Apply suggestions from code review
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

diagnostics Related to reporting of diagnostics. ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants