ruff/crates/red_knot_python_semantic/resources/mdtest/subscript/instance.md
David Peter a378ff38dc
[red-knot] Fix Boolean flags in mdtests (#14654)
## Summary

Similar to #14652, but now with conditions that are `Literal[True]`
(instead of `Literal[False]`), where we want them to be `bool`.
2024-11-28 14:29:35 +01:00

900 B

Instance subscript

Getitem unbound

class NotSubscriptable: ...

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

def bool_instance() -> bool:
    return True

class Identity:
    if bool_instance():

        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