Skip to content

Commit 6ad0532

Browse files
authored
Merge pull request #71 from jg-rp/fix-re-repr
Fix the string representation of regex literals. Fixes #70.
2 parents 00baf85 + c5fefee commit 6ad0532

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Python JSONPath Change Log
22

3+
## Version 1.2.1 (unreleased)
4+
5+
**Fixes**
6+
7+
- Fixed the string representation regex literals in filter expressions. See [issue #70](https://github.com/jg-rp/python-jsonpath/issues/70).
8+
39
## Version 1.2.0
410

511
**Fixes**

jsonpath/__about__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: 2023-present James Prior <[email protected]>
22
#
33
# SPDX-License-Identifier: MIT
4-
__version__ = "1.2.0"
4+
__version__ = "1.2.1"

jsonpath/filter.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ def __str__(self) -> str:
243243
if self.value.flags & flag:
244244
flags.append(ch)
245245

246-
pattern = re.sub(r"\\(.)", r"\1", self.value.pattern)
247-
return f"/{pattern}/{''.join(flags)}"
246+
return f"/{self.value.pattern}/{''.join(flags)}"
248247

249248

250249
class ListLiteral(FilterExpression):

tests/test_fluent_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from jsonpath import JSONPathMatch
66
from jsonpath import JSONPointer
7-
from jsonpath import compile
7+
from jsonpath import compile # noqa: A004
88
from jsonpath import query
99

1010

tests/test_parse.py

+15
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ class Case:
200200
path="$[?!(@.a && [email protected])]",
201201
want="$[?!(@['a'] && !@['b'])]",
202202
),
203+
Case(
204+
description="issue 70",
205+
path=r"$[?@ =~ /\d/]",
206+
want="$[?@ =~ /\\d/]",
207+
),
208+
Case(
209+
description="escaped slash in regex literal",
210+
path=r"$[?@ =~ /\\d/]",
211+
want="$[?@ =~ /\\\\d/]",
212+
),
213+
Case(
214+
description="match function",
215+
path=r"$[?match(@, '\\d')]",
216+
want='$[?match(@, "\\\\d")]',
217+
),
203218
]
204219

205220

tests/test_re.py

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class Case:
3939
data={"some": [{"thing": "foO"}]},
4040
want=[{"thing": "foO"}],
4141
),
42+
Case(
43+
description="escaped slash",
44+
path="$.some[?(@.thing =~ /fo\\\\[a-z]/)]",
45+
data={"some": [{"thing": "fo\\b"}]},
46+
want=[{"thing": "fo\\b"}],
47+
),
4248
]
4349

4450

0 commit comments

Comments
 (0)