Skip to content

Commit

Permalink
Merge pull request #2158 from GrigoriosGiann/add-supports-index
Browse files Browse the repository at this point in the history
Add support for `SupportsIndex` in Python 3.8
  • Loading branch information
Zac-HD committed Oct 30, 2019
2 parents 50c059c + aa01918 commit d491aeb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RELEASE_TYPE: patch

Python 3.8's new :class:`python:typing.SupportsIndex` type - see :pep:`357`
for details - is now supported in :func:`~hypothesis.strategies.from_type`.

Thanks to Grigorios Giannakopoulos for the patch!
12 changes: 11 additions & 1 deletion hypothesis-python/src/hypothesis/searchstrategy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def from_typing_type(thing):
array_shapes,
scalar_dtypes,
nested_dtypes,
from_dtype,
integer_dtypes,
unsigned_integer_dtypes,
)

_global_type_lookup.update(
Expand All @@ -247,7 +250,7 @@ def from_typing_type(thing):
}
)
except ImportError: # pragma: no cover
pass
np = None

try:
import typing
Expand Down Expand Up @@ -275,6 +278,13 @@ def from_typing_type(thing):
_global_type_lookup[typing.SupportsRound] = st.complex_numbers()
except AttributeError: # pragma: no cover
pass
try:
strat = st.integers() | st.booleans()
if np is not None: # pragma: no branch
strat |= (unsigned_integer_dtypes() | integer_dtypes()).flatmap(from_dtype)
_global_type_lookup[typing.SupportsIndex] = strat # type: ignore
except AttributeError: # pragma: no cover
pass

def register(type_, fallback=None):
if isinstance(type_, str):
Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/tests/py3/test_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ def inner_3(ex):
st.from_type.__clear_cache()


def if_available(name):
try:
return getattr(typing, name)
except AttributeError:
return pytest.param(name, marks=[pytest.mark.skip])


@pytest.mark.parametrize(
"typ",
[
Expand All @@ -245,6 +252,7 @@ def inner_3(ex):
typing.SupportsFloat,
typing.SupportsInt,
typing.SupportsRound,
if_available("SupportsIndex"),
],
)
def test_resolves_weird_types(typ):
Expand Down

0 comments on commit d491aeb

Please sign in to comment.