Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

st.from_regex() alphabet improvements #4008

Closed
Zac-HD opened this issue Jun 5, 2024 · 0 comments · Fixed by #4046
Closed

st.from_regex() alphabet improvements #4008

Zac-HD opened this issue Jun 5, 2024 · 0 comments · Fixed by #4046
Labels
bug something is clearly wrong here enhancement it's not broken, but we want it to be better

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Jun 5, 2024

Ensure that unions of st.characters() and st.sample_from() are usable as alphabet= arguments in st.text() and st.from_regex().

We don't want to auto-convert all such things to characters() by default because that affects shrink order, but as the alphabet argument where we're already more constrained it's fine. Motivating example based on zarr-developers/zarr-python#1746:

from hypothesis import given, strategies as st

zarr_key_chars = st.sampled_from("abc")
array_names_filter = st.text(zarr_key_chars | st.just("."), min_size=1).filter(lambda t: t[0] != ".")
array_names_regex = st.from_regex(
    r"[^.].*",
    fullmatch=True,
    alphabet=zarr_key_chars | st.just("."),
    # InvalidArgument: alphabet=one_of(sampled_from('abc'), just('.')) must be a sampled_from() or characters() strategy
)

@given(array_names_regex)
def test(name): ...

Separately, we should remove the prints here, and change "prefix caret" to "prefix caret to group containing pattern"; in the current form the semantics of match a|b is converted to match a|.*b.

print(f"{condition=}")
print(f"{condition.__name__=}")
if condition.__name__ == "match":
# Replace with an easier-to-handle equivalent condition
caret = "^" if kind is str else b"^"
pattern = re.compile(caret + pattern.pattern, flags=pattern.flags)

@Zac-HD Zac-HD added the enhancement it's not broken, but we want it to be better label Jun 5, 2024
@Zac-HD Zac-HD added the bug something is clearly wrong here label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something is clearly wrong here enhancement it's not broken, but we want it to be better
Projects
None yet
1 participant