summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog3
-rw-r--r--astroid/inference.py2
-rw-r--r--astroid/nodes/node_classes.py12
-rw-r--r--tests/unittest_inference.py10
4 files changed, 27 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 89cd00f0..2a17699e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,9 @@ Release date: TBA
Refs PyCQA/pylint#7706
+* Catch ``ValueError`` when indexing some builtin containers and sequences during inference.
+
+ Closes #1843
What's New in astroid 2.12.12?
==============================
diff --git a/astroid/inference.py b/astroid/inference.py
index a7100554..502e11b1 100644
--- a/astroid/inference.py
+++ b/astroid/inference.py
@@ -27,6 +27,7 @@ from astroid.exceptions import (
AstroidError,
AstroidIndexError,
AstroidTypeError,
+ AstroidValueError,
AttributeInferenceError,
InferenceError,
NameInferenceError,
@@ -441,6 +442,7 @@ def infer_subscript(
except (
AstroidTypeError,
AstroidIndexError,
+ AstroidValueError,
AttributeInferenceError,
AttributeError,
) as exc:
diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py
index c9c79cc3..4414d9ce 100644
--- a/astroid/nodes/node_classes.py
+++ b/astroid/nodes/node_classes.py
@@ -22,6 +22,7 @@ from astroid.context import InferenceContext
from astroid.exceptions import (
AstroidIndexError,
AstroidTypeError,
+ AstroidValueError,
InferenceError,
NoDefault,
ParentMissingError,
@@ -234,6 +235,13 @@ def _container_getitem(instance, elts, index, context=None):
return new_cls
if isinstance(index, Const):
return elts[index.value]
+ except ValueError as exc:
+ raise AstroidValueError(
+ message="Slice {index!r} cannot index container",
+ node=instance,
+ index=index,
+ context=context,
+ ) from exc
except IndexError as exc:
raise AstroidIndexError(
message="Index {index!s} out of range",
@@ -2030,6 +2038,10 @@ class Const(_base_nodes.NoChildrenNode, Instance):
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",
diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py
index 8ca10ca6..12092be7 100644
--- a/tests/unittest_inference.py
+++ b/tests/unittest_inference.py
@@ -5316,6 +5316,16 @@ def test_slice_inference_in_for_loops_not_working() -> None:
assert inferred == util.Uninferable
+def test_slice_zero_step_does_not_raise_ValueError() -> None:
+ node = extract_node("x = [][::0]; x")
+ assert next(node.infer()) == util.Uninferable
+
+
+def test_slice_zero_step_on_str_does_not_raise_ValueError() -> None:
+ node = extract_node('x = ""[::0]; x')
+ assert next(node.infer()) == util.Uninferable
+
+
def test_unpacking_starred_and_dicts_in_assignment() -> None:
node = extract_node(
"""