Skip to content

Commit

Permalink
Merge pull request #3040 from HypothesisWorks/create-pull-request/patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD authored Jul 26, 2021
2 parents a17a6d9 + d5ec1d3 commit 0283c24
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
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

This patch fixes :func:`~hypothesis.strategies.from_type` and
:func:`~hypothesis.strategies.register_type_strategy` for
:obj:`python:typing.NewType` on Python 3.10, which changed the
underlying implementation (see :bpo:`44353` for details).
10 changes: 8 additions & 2 deletions hypothesis-python/src/hypothesis/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,14 @@ def all(cls) -> List["HealthCheck"]:
return list(HealthCheck)

data_too_large = 1
"""Check for when the typical size of the examples you are generating
exceeds the maximum allowed size too often."""
"""Checks if too many examples are aborted for being too large.
This is measured by the number of random choices that Hypothesis makes
in order to generate something, not the size of the generated object.
For example, choosing a 100MB object from a predefined list would take
only a few bits, while generating 10KB of JSON from scratch might trigger
this health check.
"""

filter_too_much = 2
"""Check for when the test is filtering out too many examples, either
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ def register_type_strategy(
from hypothesis.strategies._internal import types

if not types.is_a_type(custom_type):
raise InvalidArgument("custom_type=%r must be a type")
raise InvalidArgument(f"custom_type={custom_type!r} must be a type")
elif not (isinstance(strategy, SearchStrategy) or callable(strategy)):
raise InvalidArgument(
"strategy=%r must be a SearchStrategy, or a function that takes "
Expand Down
18 changes: 11 additions & 7 deletions hypothesis-python/src/hypothesis/strategies/_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ def try_issubclass(thing, superclass):


def is_a_new_type(thing):
# At runtime, `typing.NewType` returns an identity function rather
# than an actual type, but we can check whether that thing matches.
return (
hasattr(thing, "__supertype__")
and getattr(thing, "__module__", None) in ("typing", "typing_extensions")
and inspect.isfunction(thing)
)
if sys.version_info[:2] < (3, 10):
# At runtime, `typing.NewType` returns an identity function rather
# than an actual type, but we can check whether that thing matches.
return (
hasattr(thing, "__supertype__")
and getattr(thing, "__module__", None) in ("typing", "typing_extensions")
and inspect.isfunction(thing)
)
# In 3.10 and later, NewType is actually a class - which simplifies things.
# See https://bugs.python.org/issue44353 for links to the various patches.
return isinstance(thing, typing.NewType) # pragma: no cover # on 3.8, anyway


def is_a_type(thing):
Expand Down
2 changes: 1 addition & 1 deletion requirements/coverage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ coverage==5.5
# via -r requirements/coverage.in
lark-parser==0.11.3
# via -r requirements/coverage.in
numpy==1.21.0
numpy==1.21.1
# via
# -r requirements/coverage.in
# pandas
Expand Down
15 changes: 8 additions & 7 deletions requirements/tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ packaging==21.0
# tox
parso==0.8.2
# via jedi
pathspec==0.8.1
pathspec==0.9.0
# via black
pbr==5.6.0
# via stevedore
pep517==0.10.0
pep517==0.11.0
# via pip-tools
pexpect==4.8.0
# via ipython
Expand Down Expand Up @@ -215,7 +215,7 @@ pytz==2021.1
# via
# babel
# django
pyupgrade==2.21.2
pyupgrade==2.22.0
# via shed
pyyaml==5.4.1
# via
Expand Down Expand Up @@ -290,11 +290,12 @@ toml==0.10.2
# via
# -r requirements/tools.in
# mypy
# pep517
# pytest
# tox
tomli==1.0.4
# via black
tomli==1.1.0
# via
# black
# pep517
tox==3.24.0
# via -r requirements/tools.in
tqdm==4.61.2
Expand All @@ -303,7 +304,7 @@ traitlets==4.3.3
# via
# -r requirements/tools.in
# ipython
twine==3.4.1
twine==3.4.2
# via -r requirements/tools.in
types-click==7.1.2
# via -r requirements/tools.in
Expand Down

0 comments on commit 0283c24

Please sign in to comment.