From 4423ac1250e43f7af85330a2e0dfdd434c6632a4 Mon Sep 17 00:00:00 2001 From: Yuchen Jin Date: Sun, 20 Oct 2024 09:20:52 +0000 Subject: [PATCH] 0.3.4 - fix get_data_by_route Fix: Add extra rules to `DashJsonGrid.get_data_by_route(...)` to raise an `KeyError` if a column cannot export any value. --- Changelog.md | 3 ++- dash_json_grid/mixins.py | 15 +++++++++++++-- tests/test_init_from.py | 7 +++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index bd5dde0..4b3199c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,7 +10,8 @@ 1. Fix: The modification in `0.3.3` incorrectly allows `DashJsonGrid.pop_item_of_object(...)` to export a table row/cell even if the data fails to be routed. Now this situation is disallowed. 2. Fix: A bug of `react-json-grid<=0.9.0` causes the selection may return an incorrect route containing `null`. Now the codes will handle this case. When routing the data, the routing will stop by the parent of the place where the `null` index is applied to. When modifying the data, using a route containing `null` will do nothing. -3. Fix: Correct typos in the docstrings of `mixins`. +3. Fix: Correct typos in the docstrings of `mixins` and `pytest`. +4. Fix: Add extra rules to `DashJsonGrid.get_data_by_route(...)` to raise an `KeyError` if a column cannot export any value. #### :floppy_disk: Change diff --git a/dash_json_grid/mixins.py b/dash_json_grid/mixins.py index f688a29..d9f5a38 100644 --- a/dash_json_grid/mixins.py +++ b/dash_json_grid/mixins.py @@ -70,7 +70,18 @@ def get_item_of_object(data: Any, index: Any) -> Any: return data[index[0]] elif isinstance(data, collections.abc.Sequence): index_key = index[0] - return tuple(d_item.get(index_key, None) for d_item in data) + _missing = object() + _res = tuple( + ( + d_item.get(index_key, _missing) + if isinstance(d_item, collections.abc.Mapping) + else d_item[index_key] + ) + for d_item in data + ) + if _res and any(val is not _missing for val in _res): + return tuple((None if val is _missing else val for val in _res)) + raise KeyError(index_key) else: if isinstance(data, collections.abc.Mapping): return data[index] @@ -426,7 +437,7 @@ def from_file( all_args = inspect.signature(cls).bind(*args, **kwargs).arguments.keys() if "data" in all_args: raise TypeError( - 'When using "json_file", it is not allowed to specify the argument ' + 'When using "from_file", it is not allowed to specify the argument ' '"data" because "data" is delegated to the argument "json_file".' ) if isinstance(json_file, (str, os.PathLike)): diff --git a/tests/test_init_from.py b/tests/test_init_from.py index 2a53c53..f018bd6 100644 --- a/tests/test_init_from.py +++ b/tests/test_init_from.py @@ -88,8 +88,11 @@ def test_init_from_file(self, file_path: str) -> None: ) log.info("Successfully initialize the component with a file-like object.") - with pytest.raises(TypeError, match='When using "from_str", it is not allowed'): + with pytest.raises( + TypeError, match='When using "from_file", it is not allowed' + ): dash_json_grid.DashJsonGrid.from_file(file_path, data={}) log.info( - 'Successfully validate the functionality of using "data" with file_file(...)' + 'Successfully validate the functionality of using "data" with ' + "from_file(...)" )