From 786ffe440c6f47c151b587900f054730a48302bd Mon Sep 17 00:00:00 2001 From: Andrew Haigh Date: Thu, 20 Oct 2022 23:47:53 +1300 Subject: [PATCH] Fix uncaught ValueError in infer_getitem with zero-step slices Fixes #1843 --- astroid/inference.py | 2 ++ astroid/nodes/node_classes.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/astroid/inference.py b/astroid/inference.py index a71005540b..502e11b1df 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -27,6 +27,7 @@ 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 2f515dbe90..06fad5c97e 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -22,6 +22,7 @@ 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",