Skip to content

Commit ffd6211

Browse files
committed
Temporary arg for jsonschema
1 parent 8fd286b commit ffd6211

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch adds a temporary hook for a downstream tool,
4+
which is not part of the public API.

hypothesis-python/src/hypothesis/strategies/_internal/regex.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def base_regex_strategy(regex, parsed=None):
213213
)
214214

215215

216-
def regex_strategy(regex, fullmatch):
216+
def regex_strategy(regex, fullmatch, *, _temp_jsonschema_hack_no_end_newline=False):
217217
if not hasattr(regex, "pattern"):
218218
regex = re.compile(regex)
219219

@@ -254,6 +254,15 @@ def regex_strategy(regex, fullmatch):
254254
)
255255
else:
256256
right_pad = st.one_of(empty, newline)
257+
258+
# This will be removed when a regex-syntax-translation library exists.
259+
# It's a pretty nasty hack, but means that we can match the semantics
260+
# of JSONschema's compatible subset of ECMA regex, which is important
261+
# for hypothesis-jsonschema and Schemathesis. See e.g.
262+
# https://github.com/schemathesis/schemathesis/issues/1241
263+
if _temp_jsonschema_hack_no_end_newline:
264+
right_pad = empty
265+
257266
if parsed[0][0] == sre.AT:
258267
if parsed[0][1] == sre.AT_BEGINNING_STRING:
259268
left_pad = empty

hypothesis-python/tests/cover/test_regex.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
UNICODE_SPACE_CHARS,
2626
UNICODE_WORD_CATEGORIES,
2727
base_regex_strategy,
28+
regex_strategy,
2829
)
2930

3031
from tests.common.debug import assert_all_examples, assert_no_examples, find_any
@@ -462,3 +463,12 @@ def test_sets_allow_multichar_output_in_ignorecase_mode():
462463
st.from_regex(re.compile("[\u0130_]", re.IGNORECASE)),
463464
lambda s: len(s) > 1,
464465
)
466+
467+
468+
def test_internals_can_disable_newline_from_dollar_for_jsonschema():
469+
pattern = "^abc$"
470+
find_any(st.from_regex(pattern), lambda s: s == "abc\n")
471+
assert_all_examples(
472+
regex_strategy(pattern, False, _temp_jsonschema_hack_no_end_newline=True),
473+
lambda s: s == "abc",
474+
)

0 commit comments

Comments
 (0)