Skip to content

Uncaught ValueError on inferring __getitem__ with nonsensical slices #1843

@nelfin

Description

@nelfin

Steps to reproduce

import astroid
node = astroid.extract_node("[][::0]")
print(next(node.infer()))

Current behavior

Traceback (most recent call last):
  File "repro.py", line 3, in <module>
    print(next(node.infer()))
  File ".../astroid/nodes/node_ng.py", line 169, in infer
    yield from self._infer(context=context, **kwargs)
  File ".../astroid/decorators.py", line 140, in raise_if_nothing_inferred
    yield next(generator)
  File ".../astroid/decorators.py", line 109, in wrapped
    for res in _func(node, context, **kwargs):
  File ".../astroid/inference.py", line 440, in infer_subscript
    assigned = value.getitem(index_value, context)
  File ".../astroid/nodes/node_classes.py", line 3499, in getitem
    return _container_getitem(self, self.elts, index, context=context)
  File ".../astroid/nodes/node_classes.py", line 232, in _container_getitem
    new_cls.elts = elts[index_slice]
ValueError: slice step cannot be zero

Originally spotted in pylint with a minimal reproducer like:

for _ in [][::0]:
    pass

Expected behavior

Uninferable

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

(venv) $ python -c "from astroid import __pkginfo__; print(__pkginfo__.version)"
2.13.0-dev0
(venv) $ git rev-parse HEAD
1a074cf66fc4fdb6714fac7afb14f9e7457cd704

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions