Skip to content

Commit

Permalink
Handle unions of builtin generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Nov 28, 2021
1 parent 2fa6d0a commit 09f396f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hypothesis-python/RELEASE.rst
@@ -0,0 +1,5 @@
RELEASE_TYPE: patch

This release fixes :issue:`3080`, where :func:`~hypothesis.strategies.from_type`
failed on unions containing :pep:`585` builtin generic types (like ``list[int]``)
in Python 3.9 and later.
Expand Up @@ -81,6 +81,7 @@ def type_sorting_key(t):
raise InvalidArgument(f"thing={t} must be a type") # pragma: no cover
if t is None or t is type(None): # noqa: E721
return (-1, repr(t))
t = getattr(t, "__origin__", t)
if not isinstance(t, type): # pragma: no cover
# Some generics in the typing module are not actually types in 3.7
return (2, repr(t))
Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/tests/cover/test_lookup_py39.py
Expand Up @@ -20,6 +20,7 @@

from hypothesis import given, strategies as st
from hypothesis.errors import InvalidArgument
from tests.common.debug import find_any


@pytest.mark.parametrize(
Expand Down Expand Up @@ -85,3 +86,10 @@ def test_string_forward_ref_message():
s = st.builds(User)
with pytest.raises(InvalidArgument, match="`from __future__ import annotations`"):
s.example()


def test_issue_3080():
# Check for https://github.com/HypothesisWorks/hypothesis/issues/3080
s = st.from_type(typing.Union[list[int], int])
find_any(s, lambda x: isinstance(x, int))
find_any(s, lambda x: isinstance(x, list))

0 comments on commit 09f396f

Please sign in to comment.