diff --git a/killswitch.py b/killswitch.py index 3a17f022..346d4099 100644 --- a/killswitch.py +++ b/killswitch.py @@ -81,8 +81,9 @@ def _apply(target: UPDATABLE_DATA, key: str, to_set: Any = None, delete: bool = if idx is None: raise ValueError(f'Cannot use string {key!r} as int for index into Sequence') - if delete: - if len(target) > idx: + if delete and len(target) > 0: + length = len(target) + if (idx > 0 and length > idx) or idx >= -length: target.pop(idx) elif len(target) == idx: diff --git a/tests/killswitch.py/test_apply.py b/tests/killswitch.py/test_apply.py index 0e10849d..29517203 100644 --- a/tests/killswitch.py/test_apply.py +++ b/tests/killswitch.py/test_apply.py @@ -15,7 +15,10 @@ from killswitch import UPDATABLE_DATA (['this', 'is', 'a', 'test'], '1', '', None, ['this', None, 'a', 'test']), ({'now': 'with', 'a': 'dict'}, 'now', 'delete', None, {'a': 'dict'}), ({'now': 'with', 'a': 'dict'}, 'now', '', None, {'now': None, 'a': 'dict'}), - (['test append'], '1', '', 'yay', ['test append', 'yay']) + (['test append'], '1', '', 'yay', ['test append', 'yay']), + (['test neg del'], '-1', 'delete', None, []), + (['test neg del'], '-1337', 'delete', None, ['test neg del']), + (['test neg del'], '-2', 'delete', None, ['test neg del']), ] ) def test_apply(source: UPDATABLE_DATA, key: str, action: str, to_set: Any, result: UPDATABLE_DATA) -> None: