diff --git a/tests/test_dsl.py b/tests/test_dsl.py index c56b8d859..46bf2f677 100644 --- a/tests/test_dsl.py +++ b/tests/test_dsl.py @@ -46,6 +46,7 @@ "neg": -1, "bool": True, "nesting": {"fib": [1, 1, 2, 3, 5], "foo": {"bar": "baz"}}, + "dot.key": 3.14, } @@ -105,8 +106,8 @@ def test_prop_or_prop() -> None: {}, ) assert str(query) == ( - '((json_extract(properties, "$.int") == 2) OR ' - '(json_extract(properties, "$.int") == 3))' + """((json_extract(properties, '$."int"') == 2) OR """ + """(json_extract(properties, '$."int"') == 3))""" ) @@ -571,3 +572,19 @@ def test_contains_str( eval_locals, ) assert bool(check(result)) is True + + @staticmethod + def test_key_with_period( + eval_globals: dict[str, object], + eval_locals: Mapping[str, object], + check: Callable, + ) -> None: + """Test key with period.""" + query = "props['dot.key']" + result = eval( # skipcq: PYL-W0123 # noqa: S307 + query, + eval_globals, + eval_locals, + ) + + assert check(result) == 3.14 diff --git a/tiatoolbox/annotation/dsl.py b/tiatoolbox/annotation/dsl.py index 4aa6536bd..6e3e00db7 100644 --- a/tiatoolbox/annotation/dsl.py +++ b/tiatoolbox/annotation/dsl.py @@ -265,14 +265,14 @@ def __init__(self: SQLJSONDictionary, acc: str | None = None) -> None: def __str__(self: SQLJSONDictionary) -> str: """Return a human-readable, or informal, string representation of an object.""" - return f"json_extract(properties, {json.dumps(f'$.{self.acc}')})" + return f"json_extract(properties, '$.{self.acc}')" def __getitem__(self: SQLJSONDictionary, key: str) -> SQLJSONDictionary: """Get an item from the dataset.""" - key_str = f"[{key}]" if isinstance(key, (int,)) else str(key) + key_str = f"[{key}]" if isinstance(key, (int,)) else f'"{key}"' joiner = "." if self.acc and not isinstance(key, int) else "" - return SQLJSONDictionary(acc=self.acc + joiner + f"{key_str}") + return SQLJSONDictionary(acc=self.acc + joiner + key_str) def get( self: SQLJSONDictionary,