Download tests/test_basic_ops_list.py from soiz1/omegaconf: direct link, hf CLI and curl.
- Browser
- Download file 16 kB
-
https://huggingface.co/datasets/soiz1/omegaconf/resolve/main/tests/test_basic_ops_list.py
- Command line
-
hf download hf://datasets/soiz1/omegaconf/tests/test_basic_ops_list.py
-
curl -L -o test_basic_ops_list.py https://huggingface.co/datasets/soiz1/omegaconf/resolve/main/tests/test_basic_ops_list.py
16 kB
| # -*- coding: utf-8 -*- | |
| import re | |
| from typing import Any, List, Optional | |
| import pytest | |
| from omegaconf import AnyNode, ListConfig, OmegaConf | |
| from omegaconf.errors import ( | |
| ConfigKeyError, | |
| ConfigTypeError, | |
| KeyValidationError, | |
| MissingMandatoryValue, | |
| UnsupportedValueType, | |
| ValidationError, | |
| ) | |
| from omegaconf.nodes import IntegerNode, StringNode | |
| from . import Color, IllegalType, User, does_not_raise | |
| def test_list_value() -> None: | |
| c = OmegaConf.create("a: [1,2]") | |
| assert {"a": [1, 2]} == c | |
| def test_list_of_dicts() -> None: | |
| v = [dict(key1="value1"), dict(key2="value2")] | |
| c = OmegaConf.create(v) | |
| assert c[0].key1 == "value1" | |
| assert c[1].key2 == "value2" | |
| def test_list_get_with_default() -> None: | |
| c = OmegaConf.create([None, "???", "found"]) | |
| assert c.get(0, "default_value") == "default_value" | |
| assert c.get(1, "default_value") == "default_value" | |
| assert c.get(2, "default_value") == "found" | |
| def test_iterate_list(input_: Any, expected: Any, list_key: str) -> None: | |
| c = OmegaConf.create(input_) | |
| if list_key is not None: | |
| lst = c.get(list_key) | |
| else: | |
| lst = c | |
| items = [x for x in lst] | |
| assert items == expected | |
| def test_iterate_list_with_missing_interpolation() -> None: | |
| c = OmegaConf.create([1, "${10}"]) | |
| itr = iter(c) | |
| assert 1 == next(itr) | |
| with pytest.raises(ConfigKeyError): | |
| next(itr) | |
| def test_iterate_list_with_missing() -> None: | |
| c = OmegaConf.create([1, "???"]) | |
| itr = iter(c) | |
| assert 1 == next(itr) | |
| with pytest.raises(MissingMandatoryValue): | |
| next(itr) | |
| def test_items_with_interpolation() -> None: | |
| c = OmegaConf.create(["foo", "${0}"]) | |
| assert c == ["foo", "foo"] | |
| def test_list_pop() -> None: | |
| c = OmegaConf.create([1, 2, 3, 4]) | |
| assert c.pop(0) == 1 | |
| assert c.pop() == 4 | |
| assert c == [2, 3] | |
| with pytest.raises(IndexError): | |
| c.pop(100) | |
| validate_list_keys(c) | |
| def test_list_pop_on_unexpected_exception_not_modifying() -> None: | |
| src = [1, 2, 3, 4] | |
| c = OmegaConf.create(src) | |
| with pytest.raises(ConfigTypeError): | |
| c.pop("foo") # type: ignore | |
| assert c == src | |
| def test_in_list() -> None: | |
| c = OmegaConf.create([10, 11, dict(a=12)]) | |
| assert 10 in c | |
| assert 11 in c | |
| assert dict(a=12) in c | |
| assert "blah" not in c | |
| def test_list_config_with_list() -> None: | |
| c = OmegaConf.create([]) | |
| assert isinstance(c, ListConfig) | |
| def test_list_config_with_tuple() -> None: | |
| c = OmegaConf.create(()) | |
| assert isinstance(c, ListConfig) | |
| def test_items_on_list() -> None: | |
| c = OmegaConf.create([1, 2]) | |
| with pytest.raises(AttributeError): | |
| c.items() | |
| def test_list_enumerate() -> None: | |
| src: List[Optional[str]] = ["a", "b", "c", "d"] | |
| c = OmegaConf.create(src) | |
| for i, v in enumerate(c): | |
| assert src[i] == v | |
| assert v is not None | |
| src[i] = None | |
| for v in src: | |
| assert v is None | |
| def test_list_delitem() -> None: | |
| c = OmegaConf.create([1, 2, 3]) | |
| assert c == [1, 2, 3] | |
| del c[0] | |
| assert c == [2, 3] | |
| with pytest.raises(IndexError): | |
| del c[100] | |
| validate_list_keys(c) | |
| def test_list_len(lst: Any, expected: Any) -> None: | |
| assert len(lst) == expected | |
| def test_nested_list_assign_illegal_value() -> None: | |
| c = OmegaConf.create({"a": [None]}) | |
| with pytest.raises( | |
| UnsupportedValueType, | |
| match=re.escape( | |
| "Value 'IllegalType' is not a supported primitive type\n\tfull_key: a[0]" | |
| ), | |
| ): | |
| c.a[0] = IllegalType() | |
| def test_list_append() -> None: | |
| c = OmegaConf.create([]) | |
| c.append(1) | |
| c.append(2) | |
| c.append({}) | |
| c.append([]) | |
| assert c == [1, 2, {}, []] | |
| validate_list_keys(c) | |
| def test_append_invalid_element_type( | |
| lc: ListConfig, element: Any, expected: Any | |
| ) -> None: | |
| with expected: | |
| lc.append(element) | |
| def test_append_convert(lc: ListConfig, element: Any, expected: Any) -> None: | |
| lc.append(element) | |
| value = lc[-1] | |
| assert value == expected | |
| assert type(value) == type(expected) | |
| def test_list_index(index: Any, expected: Any) -> None: | |
| c = OmegaConf.create([10, 11, 12, 13]) | |
| assert c[index] == expected | |
| def test_list_dir(cfg: Any, expected: Any) -> None: | |
| assert dir(cfg) == expected | |
| def validate_list_keys(c: Any) -> None: | |
| # validate keys are maintained | |
| for i in range(len(c)): | |
| assert c._get_node(i)._metadata.key == i | |
| def test_insert( | |
| input_: List[str], | |
| index: int, | |
| value: Any, | |
| expected: Any, | |
| expected_node_type: type, | |
| expectation: Any, | |
| ) -> None: | |
| c = OmegaConf.create(input_) | |
| if expectation is None: | |
| c.insert(index, value) | |
| assert c == expected | |
| assert type(c._get_node(index)) == expected_node_type | |
| else: | |
| with pytest.raises(expectation): | |
| c.insert(index, value) | |
| validate_list_keys(c) | |
| def test_insert_special_list(lst: Any, idx: Any, value: Any, expectation: Any) -> None: | |
| with expectation: | |
| lst.insert(idx, value) | |
| def test_extend(src: List[Any], append: List[Any], result: List[Any]) -> None: | |
| lst = OmegaConf.create(src) | |
| lst.extend(append) | |
| assert lst == result | |
| def test_remove(src: List[Any], remove: Any, result: Any, expectation: Any) -> None: | |
| with expectation: | |
| lst = OmegaConf.create(src) | |
| assert isinstance(lst, ListConfig) | |
| lst.remove(remove) | |
| assert lst == result | |
| # type: ignore | |
| # type: ignore | |
| def test_clear(src: List[Any], num_clears: int) -> None: | |
| lst = OmegaConf.create(src) | |
| for i in range(num_clears): | |
| lst.clear() | |
| assert lst == [] | |
| def test_index( | |
| src: List[Any], item: Any, expected_index: int, expectation: Any | |
| ) -> None: | |
| with expectation: | |
| lst = OmegaConf.create(src) | |
| assert lst.index(item) == expected_index | |
| def test_index_with_range() -> None: | |
| lst = OmegaConf.create([10, 20, 30, 40, 50]) | |
| assert lst.index(x=30) == 2 | |
| assert lst.index(x=30, start=1) == 2 | |
| assert lst.index(x=30, start=1, end=3) == 2 | |
| with pytest.raises(ValueError): | |
| lst.index(x=30, start=3) | |
| with pytest.raises(ValueError): | |
| lst.index(x=30, end=2) | |
| def test_count(src: List[Any], item: Any, count: int) -> None: | |
| lst = OmegaConf.create(src) | |
| assert lst.count(item) == count | |
| def test_sort() -> None: | |
| c = OmegaConf.create(["bbb", "aa", "c"]) | |
| c.sort() | |
| assert ["aa", "bbb", "c"] == c | |
| c.sort(reverse=True) | |
| assert ["c", "bbb", "aa"] == c | |
| c.sort(key=len) | |
| assert ["c", "aa", "bbb"] == c | |
| c.sort(key=len, reverse=True) | |
| assert ["bbb", "aa", "c"] == c | |
| def test_insert_throws_not_changing_list() -> None: | |
| c = OmegaConf.create([]) | |
| with pytest.raises(ValueError): | |
| c.insert(0, IllegalType()) | |
| assert len(c) == 0 | |
| assert c == [] | |
| def test_append_throws_not_changing_list() -> None: | |
| c = OmegaConf.create([]) | |
| v = IllegalType() | |
| with pytest.raises(ValueError): | |
| c.append(v) | |
| assert len(c) == 0 | |
| assert c == [] | |
| validate_list_keys(c) | |
| def test_hash() -> None: | |
| c1 = OmegaConf.create([10]) | |
| c2 = OmegaConf.create([10]) | |
| assert hash(c1) == hash(c2) | |
| c2[0] = 20 | |
| assert hash(c1) != hash(c2) | |
| class TestListAdd: | |
| def test_list_plus( | |
| self, in_list1: List[Any], in_list2: List[Any], in_expected: List[Any] | |
| ) -> None: | |
| list1 = OmegaConf.create(in_list1) | |
| list2 = OmegaConf.create(in_list2) | |
| expected = OmegaConf.create(in_expected) | |
| ret = list1 + list2 | |
| assert ret == expected | |
| def test_list_plus_eq( | |
| self, in_list1: List[Any], in_list2: List[Any], in_expected: List[Any] | |
| ) -> None: | |
| list1 = OmegaConf.create(in_list1) | |
| list2 = OmegaConf.create(in_list2) | |
| expected = OmegaConf.create(in_expected) | |
| list1 += list2 | |
| assert list1 == expected | |
| def test_deep_add() -> None: | |
| cfg = OmegaConf.create({"foo": [1, 2, "${bar}"], "bar": "xx"}) | |
| lst = cfg.foo + [10, 20] | |
| assert lst == [1, 2, "xx", 10, 20] | |
| def test_set_with_invalid_key() -> None: | |
| cfg = OmegaConf.create([1, 2, 3]) | |
| with pytest.raises(KeyValidationError): | |
| cfg["foo"] = 4 # type: ignore | |
| def test_getitem(lst: Any, idx: Any, expected: Any) -> None: | |
| if isinstance(expected, type): | |
| with pytest.raises(expected): | |
| lst.__getitem__(idx) | |
| else: | |
| assert lst.__getitem__(idx) == expected | |
| def test_getitem_slice(sli: slice) -> None: | |
| lst = [1, 2, 3] | |
| olst = OmegaConf.create([1, 2, 3]) | |
| expected = lst[sli.start : sli.stop : sli.step] | |
| assert olst.__getitem__(sli) == expected | |
| def test_get(lst: Any, idx: Any, expected: Any) -> None: | |
| if isinstance(expected, type): | |
| with pytest.raises(expected): | |
| lst.get(idx) | |
| else: | |
| lst.__getitem__(idx) == expected | |
| def test_getattr() -> None: | |
| src = ["a", "b", "c"] | |
| cfg = OmegaConf.create(src) | |
| with pytest.raises(AttributeError): | |
| getattr(cfg, "foo") | |
| assert getattr(cfg, "0") == src[0] | |
| assert getattr(cfg, "1") == src[1] | |
| assert getattr(cfg, "2") == src[2] | |