[ty] Fix incorrect diagnostic when calling __setitem__ (#19645)

## Summary

Resolves https://github.com/astral-sh/ty/issues/862 by not emitting a
diagnostic.

## Test Plan

Add test to show we don't emit the diagnostic
This commit is contained in:
Matthew Mckee 2025-07-30 19:34:52 +01:00 committed by GitHub
parent 7b4103bcb6
commit 4739bc8d14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 19 deletions

View file

@ -253,7 +253,6 @@ does["not"]["exist"] = 0
reveal_type(does["not"]["exist"]) # revealed: Unknown
non_subscriptable = 1
# error: [non-subscriptable]
non_subscriptable[0] = 0
# error: [non-subscriptable]
reveal_type(non_subscriptable[0]) # revealed: Unknown

View file

@ -1,6 +1,6 @@
# Instance subscript
## Getitem unbound
## `__getitem__` unbound
```py
class NotSubscriptable: ...
@ -8,7 +8,7 @@ class NotSubscriptable: ...
a = NotSubscriptable()[0] # error: "Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method"
```
## Getitem not callable
## `__getitem__` not callable
```py
class NotSubscriptable:
@ -18,7 +18,7 @@ class NotSubscriptable:
a = NotSubscriptable()[0]
```
## Valid getitem
## Valid `__getitem__`
```py
class Identity:
@ -28,7 +28,7 @@ class Identity:
reveal_type(Identity()[0]) # revealed: int
```
## Getitem union
## `__getitem__` union
```py
def _(flag: bool):
@ -42,3 +42,14 @@ def _(flag: bool):
reveal_type(Identity()[0]) # revealed: int | str
```
## `__setitem__` with no `__getitem__`
```py
class NoGetitem:
def __setitem__(self, index: int, value: int) -> None:
pass
a = NoGetitem()
a[0] = 0
```