fixes parse_keyed_attributes

This commit is contained in:
Mose Müller 2024-04-22 19:31:29 +02:00
parent 2df1a673ac
commit 216368571a
2 changed files with 13 additions and 2 deletions

View File

@ -125,7 +125,9 @@ def parse_keyed_attribute(attr_string: str) -> tuple[str, str | float | int | No
>>> parse_keyed_attribute('attr_name') >>> parse_keyed_attribute('attr_name')
("attr_name", None) ("attr_name", None)
>>> parse_keyed_attribute('dict_attr["key"]') >>> parse_keyed_attribute('dict_attr["key"]')
("dict_attr", 'key') ("dict_attr", "key")
>>> parse_keyed_attribute("dict_attr['key']")
("dict_attr", "key")
>>> parse_keyed_attribute("dict_attr["0"]") >>> parse_keyed_attribute("dict_attr["0"]")
("dict_attr", "0") ("dict_attr", "0")
>>> parse_keyed_attribute("dict_attr[0]") >>> parse_keyed_attribute("dict_attr[0]")
@ -139,7 +141,7 @@ def parse_keyed_attribute(attr_string: str) -> tuple[str, str | float | int | No
attr_name, key_part = attr_string.split("[", 1) attr_name, key_part = attr_string.split("[", 1)
key_part = key_part.rstrip("]") key_part = key_part.rstrip("]")
# Remove quotes if present (supports both single and double quotes) # Remove quotes if present (supports both single and double quotes)
if key_part.startswith('"') and key_part.endswith('"'): if key_part.startswith(('"', "'")) and key_part.endswith(('"', "'")):
key = key_part[1:-1] key = key_part[1:-1]
elif "." in key_part: elif "." in key_part:
key = float(key_part) key = float(key_part)

View File

@ -40,9 +40,18 @@ def test_is_property_attribute(attr_name: str, expected: bool) -> None:
("list_attr[2]", ("list_attr", 2)), ("list_attr[2]", ("list_attr", 2)),
('dict_attr["2"]', ("dict_attr", "2")), ('dict_attr["2"]', ("dict_attr", "2")),
('dict_attr["some_key"]', ("dict_attr", "some_key")), ('dict_attr["some_key"]', ("dict_attr", "some_key")),
("dict_attr['some_key']", ("dict_attr", "some_key")),
("dict_attr[2]", ("dict_attr", 2)), ("dict_attr[2]", ("dict_attr", 2)),
("dict_attr[2.1]", ("dict_attr", 2.1)), ("dict_attr[2.1]", ("dict_attr", 2.1)),
], ],
) )
def test_parse_keyed_attributes(attr_name: str, expected: tuple[str, Any]) -> None: def test_parse_keyed_attributes(attr_name: str, expected: tuple[str, Any]) -> None:
assert parse_keyed_attribute(attr_name) == expected assert parse_keyed_attribute(attr_name) == expected
# def test_get_nested_dict_by_path() -> None:
# obj = {"2.1": "foo", 2.1: "bar"}
# serialized_object = {
# "dict_attr": dump(obj=obj),
# }
# assert get_nested_dict_by_path(serialized_object, 'dict_attr["2.1"]') == {}