From 5ca55d4b178e4722ff9c354d7084031c0eb762f8 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Tue, 16 Apr 2024 20:41:30 -0500 Subject: [PATCH 1/3] fix namespace.to_dict --- .../bidsschematools/types/namespace.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tools/schemacode/bidsschematools/types/namespace.py b/tools/schemacode/bidsschematools/types/namespace.py index 39943ac3f0..e2c37eee40 100644 --- a/tools/schemacode/bidsschematools/types/namespace.py +++ b/tools/schemacode/bidsschematools/types/namespace.py @@ -168,12 +168,17 @@ def __init__(self, *args, **kwargs): self._properties = dict(*args, **kwargs) def to_dict(self) -> dict: - ret = {} - for key, val in self._properties.items(): - if isinstance(val, Namespace): - val = val.to_dict() - ret[key] = val - return ret + + def _to_dict(obj): + if isinstance(obj, Namespace): + return {k: _to_dict(v) for k, v in obj._properties.items()} + if isinstance(obj, list): + return [_to_dict(v) for v in obj] + if isinstance(obj, dict): + return {k: _to_dict(v) for k, v in obj.items()} + return obj + + return _to_dict(self) def __deepcopy__(self, memo): return self.build(self.to_dict()) From c21aaaf1ace03d33995f4443dc9c886eb910dd32 Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Wed, 17 Apr 2024 10:01:24 -0500 Subject: [PATCH 2/3] add test searching for namespaces --- .../bidsschematools/tests/test_schema.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tools/schemacode/bidsschematools/tests/test_schema.py b/tools/schemacode/bidsschematools/tests/test_schema.py index 5e7b0e3575..595401d943 100644 --- a/tools/schemacode/bidsschematools/tests/test_schema.py +++ b/tools/schemacode/bidsschematools/tests/test_schema.py @@ -343,3 +343,17 @@ def test_dereferencing(): }, }, } + + +def test_namespace_to_dict(): + + def check_for_namespaces(obj): + if isinstance(obj, dict): + [check_for_namespaces(val) for val in obj.values()] + elif isinstance(obj, list): + [check_for_namespaces(val) for val in obj] + elif isinstance(obj, types.Namespace): + raise ValueError("Namespace object found in dict") + + check_for_namespaces(schema.load_schema().to_dict()) + From be24b665ce68d3bdcb797bf51306bdfa0e1fc13d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:02:05 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tools/schemacode/bidsschematools/tests/test_schema.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/schemacode/bidsschematools/tests/test_schema.py b/tools/schemacode/bidsschematools/tests/test_schema.py index 595401d943..bd5fc28942 100644 --- a/tools/schemacode/bidsschematools/tests/test_schema.py +++ b/tools/schemacode/bidsschematools/tests/test_schema.py @@ -356,4 +356,3 @@ def check_for_namespaces(obj): raise ValueError("Namespace object found in dict") check_for_namespaces(schema.load_schema().to_dict()) -