Files
ruff/crates/red_knot_python_semantic/resources/mdtest/subscript/instance.md
David Peter c2f7c39987 [red-knot] mdtest suite: formatting and cleanup (#13806)
Minor cleanup and consistent formatting of the Markdown-based tests.

- Removed lots of unnecessary `a`, `b`, `c`, … variables.
- Moved test assertions (`# revealed:` comments) closer to the tested
object.
- Always separate `# revealed` and `# error` comments from the code by
two spaces, according to the discussion
[here](https://github.com/astral-sh/ruff/pull/13746/files#r1799385758).
This trades readability for consistency in some cases.
- Fixed some headings
2024-10-18 11:07:53 +02:00

854 B

Instance subscript

Getitem unbound

class NotSubscriptable: pass
a = NotSubscriptable()[0]  # error: "Cannot subscript object of type `NotSubscriptable` with no `__getitem__` method"

Getitem not callable

class NotSubscriptable:
    __getitem__ = None

a = NotSubscriptable()[0]  # error: "Method `__getitem__` of type `None` is not callable on object of type `NotSubscriptable`"

Valid getitem

class Identity:
    def __getitem__(self, index: int) -> int:
        return index

reveal_type(Identity()[0])  # revealed: int

Getitem union

flag = True

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