omry / omegaconf

Flexible Python configuration system. The last one you will ever need.
BSD 3-Clause "New" or "Revised" License
1.91k stars 104 forks source link

ListConfig errors when is [:-1] applied to empty list #1086

Open YodaEmbedding opened 1 year ago

YodaEmbedding commented 1 year ago

Describe the bug ListConfig errors when is [:-1] applied to empty list.

To Reproduce

>>> from omegaconf import OmegaConf
>>> conf = OmegaConf.create({"xs": []})
>>> conf.xs[:-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/site-packages/omegaconf/listconfig.py", line 218, in __getitem__
    self._format_and_raise(key=index, value=None, cause=e)
  File "/usr/lib/python3.11/site-packages/omegaconf/base.py", line 231, in _format_and_raise
    format_and_raise(
  File "/usr/lib/python3.11/site-packages/omegaconf/_utils.py", line 899, in format_and_raise
    _raise(ex, cause)
  File "/usr/lib/python3.11/site-packages/omegaconf/_utils.py", line 797, in _raise
    raise ex.with_traceback(sys.exc_info()[2])  # set env var OC_CAUSE=1 for full trace
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/omegaconf/listconfig.py", line 203, in __getitem__
    for slice_idx in itertools.islice(
                     ^^^^^^^^^^^^^^^^^
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.
    full_key: xs[None:-1]
    object_type=list

Expected behavior

>>> xs = []
>>> xs[:-1]
[]

Additional context

Jasha10 commented 1 year ago

Thanks @YodaEmbedding.

For reference, here is comparison between current behavior for ListConfig and for python's built-in list type:

>>> [][:-1]
[]
>>> OmegaConf.create([])[:-1]
Traceback (most recent call last):  ... ValueError: ...

A PR to fix this would be welcome. It would probably involve a minor edit to the ListConfig.__getitem__ method and an extra test or two in the test_basic_ops_list.py file.