Skip to content

Commit b4114e9

Browse files
committed
improve st.characters categories repr
1 parent 0cf9192 commit b4114e9

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
Improve the string representation of |st.characters| in some cases.

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,23 @@ def from_characters_args(
7575
)
7676
if codec is not None:
7777
intervals &= charmap.intervals_from_codec(codec)
78-
_arg_repr = ", ".join(
79-
f"{k}={v!r}"
80-
for k, v in [
81-
("codec", codec),
82-
("min_codepoint", min_codepoint),
83-
("max_codepoint", max_codepoint),
84-
("categories", categories),
85-
("exclude_characters", exclude_characters),
86-
("include_characters", include_characters),
87-
]
88-
if v not in (None, "", set(charmap.categories()) - {"Cs"})
89-
)
78+
79+
_args = []
80+
if codec is not None:
81+
_args.append(("codec", codec))
82+
if min_codepoint is not None:
83+
_args.append(("min_codepoint", min_codepoint))
84+
if max_codepoint is not None:
85+
_args.append(("max_codepoint", max_codepoint))
86+
if categories is not None and set(categories) != set(charmap.categories()) - {
87+
"Cs"
88+
}:
89+
_args.append(("categories", categories))
90+
if exclude_characters not in [None, ""]:
91+
_args.append(("exclude_characters", exclude_characters))
92+
if include_characters not in [None, ""]:
93+
_args.append(("include_characters", include_characters))
94+
_arg_repr = ", ".join(f"{k}={v!r}" for k, v in _args)
9095
if not intervals:
9196
raise InvalidArgument(
9297
"No characters are allowed to be generated by this "

hypothesis-python/tests/cover/test_custom_reprs.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pytest
1414

1515
from hypothesis import given, settings, strategies as st
16+
from hypothesis.strategies._internal.lazy import unwrap_strategies
1617

1718

1819
def test_includes_non_default_args_in_repr():
@@ -165,3 +166,33 @@ def inner(a, b):
165166
)
166167
"""
167168
assert "\n".join(err.value.__notes__).strip() == expected.strip()
169+
170+
171+
@pytest.mark.parametrize(
172+
"strategy, expected_repr",
173+
[
174+
(st.characters(), "characters()"),
175+
(st.characters(codec="utf-8"), "characters(codec='utf-8')"),
176+
(st.characters(min_codepoint=65), "characters(min_codepoint=65)"),
177+
(st.characters(max_codepoint=127), "characters(max_codepoint=127)"),
178+
(st.characters(categories=["Lu", "Ll"]), "characters(categories=('Lu', 'Ll'))"),
179+
(
180+
st.characters(exclude_characters="abc"),
181+
"characters(exclude_characters='abc')",
182+
),
183+
(
184+
st.characters(min_codepoint=65, max_codepoint=90),
185+
"characters(min_codepoint=65, max_codepoint=90)",
186+
),
187+
(
188+
st.characters(codec="ascii", min_codepoint=32, max_codepoint=126),
189+
"characters(min_codepoint=32, max_codepoint=126)",
190+
),
191+
(
192+
st.characters(categories=["Lu"], exclude_characters="AZ"),
193+
"characters(categories=('Lu',), exclude_characters='AZ')",
194+
),
195+
],
196+
)
197+
def test_characters_repr(strategy, expected_repr):
198+
assert repr(unwrap_strategies(strategy)) == expected_repr

0 commit comments

Comments
 (0)