ruff/crates/red_knot_python_semantic/resources/mdtest/subscript/instance.md
Alex d77480768d
[red-knot] Port type inference tests to new test framework (#13719)
## Summary

Porting infer tests to new markdown tests framework.

Link to the corresponding issue: #13696

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
2024-10-15 11:23:46 -07:00

868 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

a = Identity()[0]  
reveal_type(a) # 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)

a = Identity()[0]  
reveal_type(a) # revealed: int | str