Skip to content

Commit 561a549

Browse files
committed
perf: use less filtering
1 parent 7094f9d commit 561a549

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/hypothesis_jsonschema/_from_schema.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ def merged_as_strategies(
5959
continue
6060
s = merged([inputs[g] for g in group])
6161
if s is not None and s != FALSEY:
62-
validators = [make_validator(s) for s in schemas]
62+
validators = [make_validator(s).is_valid for s in schemas]
6363
strats.append(
6464
from_schema(s, custom_formats=custom_formats).filter(
65-
lambda obj: all(v.is_valid(obj) for v in validators)
65+
lambda obj: all(v(obj) for v in validators)
6666
)
6767
)
6868
combined.update(group)
@@ -257,7 +257,7 @@ def number_schema(schema: dict) -> st.SearchStrategy[float]:
257257
exclude_min=exclude_min,
258258
exclude_max=exclude_max,
259259
# Filter out negative-zero as it does not exist in JSON
260-
).filter(lambda n: n != 0 or math.copysign(1, n) == 1)
260+
).map(lambda n: n if n != 0 else abs(n))
261261

262262

263263
def rfc3339(name: str) -> st.SearchStrategy[str]:
@@ -575,18 +575,19 @@ def object_schema(
575575
dep_schemas = {k: v for k, v in dependencies.items() if k not in dep_names}
576576
del dependencies
577577

578+
valid_name = make_validator(names).is_valid
579+
known_optional_names: List[str] = sorted(
580+
set(filter(valid_name, set(dep_names).union(dep_schemas, properties)))
581+
- set(required)
582+
)
578583
name_strats = (
579-
st.sampled_from(sorted(dep_names) + sorted(dep_schemas) + sorted(properties))
580-
if (dep_names or dep_schemas or properties)
581-
else st.nothing(),
582584
from_schema(names, custom_formats=custom_formats)
583585
if additional_allowed
584586
else st.nothing(),
585-
st.one_of([st.from_regex(p) for p in sorted(patterns)]),
586-
)
587-
all_names_strategy = st.one_of([s for s in name_strats if not s.is_empty]).filter(
588-
make_validator(names).is_valid
587+
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)]),
589589
)
590+
all_names_strategy = st.one_of([s for s in name_strats if not s.is_empty])
590591

591592
@st.composite # type: ignore
592593
def from_object_schema(draw: Any) -> Any:

0 commit comments

Comments
 (0)