Skip to content

Commit 0ee47f9

Browse files
committed
chore: change the default "extra context" identifier to _, see #5
1 parent 9288dc1 commit 0ee47f9

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Version 0.6.0
44

5+
**Breaking changes**
6+
7+
- The "extra context" identifier now defaults to `_`. Previously it defaulted to `#`, but it has been decided that `#` is better suited as a "keys" or "properties" identifier.
8+
59
**Features**
610

711
- Added a non-standard keys/properties selector (`~`).
@@ -25,7 +29,7 @@
2529

2630
**Fixes**
2731

28-
- Fixed a bug where the current object selector (`@`) would evaluate to `undefined` when a filter is applied to an array of strings.
32+
- Fixed a bug where the current object identifier (`@`) would evaluate to `undefined` when a filter is applied to an array of strings.
2933
- Compound paths that have a trailing `|` or `&` now raise a `JSONPathSyntaxError`.
3034

3135
**IETF JSONPath Draft compliance**

docs/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,5 +187,5 @@ And this is a list of features that are uncommon or unique to Python JSONPath.
187187

188188
- `|` is a union operator, where matches from two or more JSONPaths are combined. This is not part of the Python API, but built-in to the JSONPath syntax.
189189
- `&` is an intersection operator, where we exclude matches that don't exist in both left and right paths. This is not part of the Python API, but built-in to the JSONPath syntax.
190-
- `#` is a filter context selector. With usage similar to `$` and `@`, `#` exposes arbitrary data from the `filter_context` argument to `findall()` and `finditer()`.
190+
- `_` is a filter context selector. With usage similar to `$` and `@`, `_` exposes arbitrary data from the `filter_context` argument to `findall()` and `finditer()`.
191191
- `~` is a "keys" or "properties" selector.

jsonpath/env.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class JSONPathEnvironment:
5555
and overriding class attributes and/or methods. Some of these
5656
customizations include:
5757
58-
- Changing the root (`$`), self (`@`) or filter context (`#`) token with
58+
- Changing the root (`$`), self (`@`) or filter context (`_`) token with
5959
class attributes `root_token`, `self_token` and `filter_context_token`.
6060
- Registering a custom lexer or parser with the class attributes
6161
`lexer_class` or `parser_class`. `lexer_class` must be a subclass of
@@ -69,7 +69,7 @@ class attributes `root_token`, `self_token` and `filter_context_token`.
6969
7070
Attributes:
7171
filter_context_token (str): The pattern used to select extra filter context
72-
data. Defaults to `"#"`.
72+
data. Defaults to `"_"`.
7373
intersection_token (str): The pattern used as the intersection operator.
7474
Defaults to `"$"`.
7575
keys_token (str): The pattern used as the "keys" selector. Defaults to `"~"`.
@@ -93,7 +93,7 @@ class attributes `root_token`, `self_token` and `filter_context_token`.
9393
root_token = "$"
9494
self_token = "@"
9595
union_token = "|"
96-
filter_context_token = "#"
96+
filter_context_token = "_"
9797

9898
max_int_index = (2**53) - 1
9999
min_int_index = -(2**53) + 1

jsonpath/filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class FilterContextPath(Path):
402402

403403
def __str__(self) -> str:
404404
path_repr = str(self.path)
405-
return "#" + path_repr[1:]
405+
return "_" + path_repr[1:]
406406

407407
def evaluate(self, context: FilterContext) -> object:
408408
matches = self.path.findall(context.extra_context)

tests/test_env.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_find_all_from_json_string(env: JSONPathEnvironment) -> None:
2727
def test_find_all_with_extra_filter_context(env: JSONPathEnvironment) -> None:
2828
"""Test that we can pass extra filter context to findall."""
2929
rv = env.findall(
30-
"$[?(@.some == #.other)]",
30+
"$[?(@.some == _.other)]",
3131
{"foo": {"some": 1, "thing": 2}},
3232
filter_context={"other": 1},
3333
)
@@ -49,7 +49,7 @@ def test_find_iter_from_json_string(env: JSONPathEnvironment) -> None:
4949
def test_find_iter_with_extra_filter_context(env: JSONPathEnvironment) -> None:
5050
"""Test that we can pass extra filter context to finditer."""
5151
matches = env.finditer(
52-
"$[?(@.some == #.other)]",
52+
"$[?(@.some == _.other)]",
5353
{"foo": {"some": 1, "thing": 2}},
5454
filter_context={"other": 1},
5555
)
@@ -79,7 +79,7 @@ def test_find_all_async_with_extra_filter_context(env: JSONPathEnvironment) -> N
7979

8080
async def coro() -> List[object]:
8181
return await env.findall_async(
82-
"$[?(@.some == #.other)]",
82+
"$[?(@.some == _.other)]",
8383
{"foo": {"some": 1, "thing": 2}},
8484
filter_context={"other": 1},
8585
)
@@ -112,7 +112,7 @@ def test_find_iter_async_with_extra_filter_context(env: JSONPathEnvironment) ->
112112

113113
async def coro() -> List[object]:
114114
matches = await env.finditer_async(
115-
"$[?(@.some == #.other)]",
115+
"$[?(@.some == _.other)]",
116116
{"foo": {"some": 1, "thing": 2}},
117117
filter_context={"other": 1},
118118
)

0 commit comments

Comments
 (0)