Skip to content

Commit

Permalink
Fix unhandled ValueError when indexing strings
Browse files Browse the repository at this point in the history
  • Loading branch information
nelfin committed Nov 12, 2022
1 parent 92ba74e commit a20c34b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions astroid/nodes/node_classes.py
Expand Up @@ -2038,6 +2038,10 @@ def getitem(self, index, context=None):
try:
if isinstance(self.value, (str, bytes)):
return Const(self.value[index_value])
except ValueError as exc:
raise AstroidValueError(
f"Could not index {self.value!r} with {index_value!r}"
) from exc
except IndexError as exc:
raise AstroidIndexError(
message="Index {index!r} out of range",
Expand Down
6 changes: 6 additions & 0 deletions tests/unittest_inference.py
Expand Up @@ -5321,6 +5321,12 @@ def test_slice_zero_step_does_not_raise_ValueError() -> None:
assert next(node.infer()) == util.Uninferable


def test_slice_zero_step_on_str_does_not_raise_ValueError() -> None:
# TODO: move these to SliceTest?
node = extract_node('x = ""[::0]; x')
assert next(node.infer()) == util.Uninferable


def test_unpacking_starred_and_dicts_in_assignment() -> None:
node = extract_node(
"""
Expand Down

0 comments on commit a20c34b

Please sign in to comment.