crates/ty_python_semantic/resources/mdtest/subscript/instance.md
__getitem__ unboundclass NotSubscriptable: ...
# snapshot: not-subscriptable
a = NotSubscriptable()[0]
error[not-subscriptable]: Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method
--> src/mdtest_snippet.py:4:5
|
4 | a = NotSubscriptable()[0]
| ^^^^^^^^^^^^^^^^^^^^^
|
__getitem__ not callableclass NotSubscriptable:
__getitem__ = None
# TODO: this would be more user-friendly if the `call-non-callable` diagnostic was
# transformed into a `not-subscriptable` diagnostic with a subdiagnostic explaining
# that this was because `__getitem__` was possibly not callable
#
# error: [call-non-callable] "Method `__getitem__` of type `None | Unknown` may not be callable on object of type `NotSubscriptable`"
a = NotSubscriptable()[0]
__getitem__class Identity:
def __getitem__(self, index: int) -> int:
return index
reveal_type(Identity()[0]) # revealed: int
__getitem__ uniondef _(flag: bool):
class Identity:
if flag:
def __getitem__(self, index: int) -> int:
return index
else:
def __getitem__(self, index: int) -> str:
return str(index)
reveal_type(Identity()[0]) # revealed: int | str
__getitem__ with invalid index argumentclass Identity:
def __getitem__(self, index: int) -> int:
return index
a = Identity()
# error: [invalid-argument-type] "Method `__getitem__` of type `bound method Identity.__getitem__(index: int) -> int` cannot be called with key of type `Literal["a"]` on object of type `Identity`"
a["a"]
__setitem__ with no __getitem__class NoGetitem:
def __setitem__(self, index: int, value: int) -> None:
pass
a = NoGetitem()
a[0] = 0
__setitem__class NoSetitem: ...
a = NoSetitem()
a[0] = 0 # error: "Cannot assign to a subscript on an object of type `NoSetitem`"
__setitem__ not callableclass NoSetitem:
__setitem__ = None
a = NoSetitem()
a[0] = 0 # error: "Method `__setitem__` of type `None | Unknown` may not be callable on object of type `NoSetitem`"
__setitem__ methodclass Identity:
def __setitem__(self, index: int, value: int) -> None:
pass
a = Identity()
a[0] = 0
__setitem__ with invalid index argumentclass Identity:
def __setitem__(self, index: int, value: int) -> None:
pass
a = Identity()
# error: [invalid-assignment] "Invalid subscript assignment with key of type `Literal["a"]` and value of type `Literal[0]` on object of type `Identity`"
a["a"] = 0