Skip to content

Commit a8f0845

Browse files
committed
Use JS meaning of ^abc$
1 parent 376ddb8 commit a8f0845

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
#### 0.22.0 - 2021-12-15
4+
- never generate trailing newlines for regex patterns ending in `$`
5+
(allowed by Python, but not by JSON Schema)
6+
37
#### 0.21.0 - 2021-10-03
48
- reduced filtering for object keys (#88)
59
- updated to `jsonschema >= 4.0.0` (#89);

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def local_file(name: str) -> str:
3131
license="MPL 2.0",
3232
description="Generate test data from JSON schemata with Hypothesis",
3333
zip_safe=False,
34-
install_requires=["hypothesis>=5.3.0", "jsonschema>=4.0.0"],
34+
install_requires=["hypothesis>=6.31.6", "jsonschema>=4.0.0"],
3535
python_requires=">=3.7",
3636
classifiers=[
3737
"Development Status :: 4 - Beta",

src/hypothesis_jsonschema/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The only public API is `from_schema`; check the docstring for details.
44
"""
55

6-
__version__ = "0.21.0"
6+
__version__ = "0.22.0"
77
__all__ = ["from_schema"]
88

99
from ._from_schema import from_schema

src/hypothesis_jsonschema/_from_schema.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import warnings
88
from fractions import Fraction
99
from functools import partial
10+
from inspect import signature
1011
from typing import Any, Callable, Dict, List, NoReturn, Optional, Set, Union
1112

1213
import jsonschema
1314
from hypothesis import assume, provisional as prov, strategies as st
1415
from hypothesis.errors import HypothesisWarning, InvalidArgument
1516
from hypothesis.internal.conjecture import utils as cu
17+
from hypothesis.strategies._internal.regex import regex_strategy
1618

1719
from ._canonicalise import (
1820
FALSEY,
@@ -41,6 +43,17 @@
4143
)
4244
_FORMATS_TOKEN = object()
4345

46+
from_js_regex: Callable[[str], st.SearchStrategy[str]] = st.from_regex
47+
if len(signature(regex_strategy).parameters) == 3: # pragma: no branch
48+
# On Hypothesis >= 6.31.6, we can use this to get the ECMA semantics of "$".
49+
# Conditionally-defined so that we degrade relatively gracefully if you update
50+
# Hypothesis but not hypothesis-jsonschema once we have a more general fix.
51+
52+
def from_js_regex(pattern: str) -> st.SearchStrategy[str]:
53+
return regex_strategy(
54+
pattern, fullmatch=False, _temp_jsonschema_hack_no_end_newline=True
55+
)
56+
4457

4558
def merged_as_strategies(
4659
schemas: List[Schema], custom_formats: Optional[Dict[str, st.SearchStrategy[str]]]
@@ -429,7 +442,7 @@ def string_schema(
429442
elif "pattern" in schema:
430443
try:
431444
re.compile(schema["pattern"])
432-
strategy = st.from_regex(schema["pattern"])
445+
strategy = from_js_regex(schema["pattern"])
433446
except re.error as err:
434447
# Patterns that are invalid in Python, or just malformed
435448
_warn_invalid_regex(schema["pattern"], err)
@@ -585,7 +598,7 @@ def object_schema(
585598
if additional_allowed
586599
else st.nothing(),
587600
st.sampled_from(known_optional_names) if known_optional_names else st.nothing(),
588-
st.one_of([st.from_regex(p).filter(valid_name) for p in sorted(patterns)]),
601+
st.one_of([from_js_regex(p).filter(valid_name) for p in sorted(patterns)]),
589602
)
590603
all_names_strategy = st.one_of([s for s in name_strats if not s.is_empty])
591604

tests/test_from_schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,8 @@ def test_can_generate_empty_list_with_max_size_and_no_allowed_items(val):
535535
)
536536
def test_can_generate_list_with_max_size_and_no_allowed_additional_items(val):
537537
assert val == [1, 2, 3]
538+
539+
540+
@given(string=from_schema({"type": "string", "pattern": "^[a-z]+$"}))
541+
def test_does_not_generate_trailing_newline_from_dollar_pattern(string):
542+
assert not string.endswith("\n")

0 commit comments

Comments
 (0)