Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ml_collections/config_flags/config_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def set_value(
"""
holder, field = _get_holder_field(config_path, config)

if isinstance(field, int) and isinstance(holder, MutableSequence):
if isinstance(field, (int, slice)) and isinstance(holder, MutableSequence):
holder[field] = value
elif hasattr(holder, '__setitem__') and (
field in holder or accept_new_attributes
Expand Down
17 changes: 17 additions & 0 deletions ml_collections/config_flags/tests/config_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@

class ConfigPathTest(parameterized.TestCase):

@parameterized.parameters(
('data[1:3]', [8, 9], [0, 8, 9, 3]),
('data[:]', [8], [8]),
('data[::2]', [8, 9], [8, 1, 9, 3]),
('data[-2:]', [], [0, 1]),
)
def test_set_list_slice(self, path, replacement, expected):
config = {'data': [0, 1, 2, 3]}
config_path.set_value(path, config, replacement)
self.assertEqual(config['data'], expected)

def test_set_extended_slice_rejects_wrong_length(self):
config = {'data': [0, 1, 2, 3]}
with self.assertRaises(ValueError):
config_path.set_value('data[::2]', config, [8])
self.assertEqual(config['data'], [0, 1, 2, 3])

def test_list_extra_index(self):
"""Tries to index a non-indexable list element."""

Expand Down
Loading