ruff/crates/red_knot_python_semantic/resources/mdtest/subscript/string.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

753 B

Subscript on strings

Simple

s = 'abcde'

a = s[0]
b = s[1]
c = s[-1]
d = s[-2]
e = s[8]        # error: [index-out-of-bounds] "Index 8 is out of bounds for string `Literal["abcde"]` with length 5"
f = s[-8]       # error: [index-out-of-bounds] "Index -8 is out of bounds for string `Literal["abcde"]` with length 5"

reveal_type(a)  # revealed: Literal["a"]
reveal_type(b)  # revealed: Literal["b"]
reveal_type(c)  # revealed: Literal["e"]
reveal_type(d)  # revealed: Literal["d"]
reveal_type(e)  # revealed: Unknown
reveal_type(f)  # revealed: Unknown

Function return

def add(x: int, y: int) -> int:
    return x + y

a = 'abcde'[add(0, 1)]
# TODO: Support overloads... Should be `str`
reveal_type(a)  # revealed: @Todo