Skip to content

Commit

Permalink
[WIP] Fixing edge cases in NestedData
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbueno committed Sep 20, 2024
1 parent 900072d commit 18a011b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
11 changes: 9 additions & 2 deletions extradict/nested_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def __len__(self):


def _extract_sequence(obj, default=_sentinel):
if isinstance(obj, Sequence):
return list(obj)
elements = []
if not obj:
# Normally not reachable: an empty mapping should be created as a mapping
Expand Down Expand Up @@ -382,13 +384,18 @@ def _should_be_a_sequence(obj, default=_sentinel, **kw):
return False
if isinstance(obj, Set):
return True
if isinstance(obj, Sequence):
return True

first_comp_keys = [key.split(".")[0] for key in obj.keys()]
if all(
isinstance(k, int) or (isinstance(k, _strings) and k.isdigit())
for k in obj.keys()
for k in first_comp_keys
):
if default:
return True
if len(set(map(int, obj.keys()))) == len(obj) and 0 in obj or "0" in obj:
# otherwise, only True if all numbers, counting from 0 are present:
if len(set(map(int, first_comp_keys))) == len(obj) and 0 in obj or "0" in obj:
return True
return False

Expand Down
45 changes: 44 additions & 1 deletion tests/test_nested_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from copy import deepcopy

from extradict import NestedData
from extradict.nested_data import _NestedDict, _NestedList
from extradict.nested_data import _NestedDict, _NestedList, _extract_sequence

import pytest

Expand Down Expand Up @@ -77,6 +77,31 @@ def test_nested_data_new_data_is_merged_with_path():
assert a["person.address.cep"] == "01311-902"


@pytest.mark.skip # WIP
def test_nested_data_new_data_is_merged_with_list_in_path():
address = {"city": "São Paulo", "street": "Av. Paulista"}
extra = {"number": 37, "cep": "01311-902"}
a = NestedData({"person.address": {}})
a["person.address"] = address
a.merge(extra, path="person.address")
assert a["person.address.street"] == "Av. Paulista"
assert a["person.address.city"] == "São Paulo"
assert a["person.address.number"] == 37
assert a["person.address.cep"] == "01311-902"


def test_nested_data_new_data_is_merged_with_path():
address = {"city": "São Paulo", "street": "Av. Paulista"}
extra = {"number": 37, "cep": "01311-902"}
a = NestedData({"person.address": {}})
a["person.address"] = address
a.merge(extra, path="person.address")
assert a["person.address.street"] == "Av. Paulista"
assert a["person.address.city"] == "São Paulo"
assert a["person.address.number"] == 37
assert a["person.address.cep"] == "01311-902"


def test_nested_data_distinct_blocks_can_be_assigned_and_contains_works_with_path():
address = {"city": "São Paulo", "street": "Av. Paulista"}
contacts = {"email": "tarsila@example.com"}
Expand Down Expand Up @@ -154,6 +179,15 @@ def test_nested_data_can_delete_deep_elements():
a["person.address"]


#######
def test__extract_sequence_works():
class MyList(list):
pass
a = MyList([1,2,3])
assert type(_extract_sequence(a)) == list and a == [1, 2, 3]



#########################


Expand All @@ -163,6 +197,15 @@ def test_nested_data_composite_key_creates_sequences_with_numeric_indexes():
assert isinstance(a, NestedData)
assert isinstance(a, Sequence)

def test_nested_data_composite_key_with_a_0_creates_sequence_at_top_level():
a = NestedData({"0": "Sáo Paulo"})
assert a.data == ["Sáo Paulo"]

a = NestedData({"0.city": "Sáo Paulo"})
assert a.data == [{"city": "São Paulo"}]




def test_nested_data_composite_key_creates_sequences_with_numeric_indexes_as_ints():
a = NestedData({0: "São Paulo", 1: "Rio de Janeiro"})
Expand Down

0 comments on commit 18a011b

Please sign in to comment.