Skip to content

Commit

Permalink
Update hypothesis to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Apr 23, 2023
1 parent 09f617b commit f1de83f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ twilio = ">=7.0.0"
uvloop = {version = "*", markers = "sys_platform != 'win32'"}

[dev-packages]
hypothesis = "<=6.47.1" # Later versions add a prerelease dependency. See https://github.com/pypa/pipenv/issues/1760
hypothesis = "*" # Versions between 6.47.1 and 6.56.4 added a prerelease dependency. See https://github.com/pypa/pipenv/issues/1760
pdoc3 = "*"
pytest = "*"
pytest-asyncio = "*"
Expand Down
14 changes: 7 additions & 7 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 20 additions & 25 deletions tests/utils/hypothesis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import contextlib
import inspect
import itertools
from inspect import Parameter, Signature

import pytest
from hypothesis.internal.reflection import (
define_function_signature,
get_signature,
impersonate
)

Expand All @@ -18,13 +19,13 @@ def autocontext(*auto_args):
that requires the `request` fixture won't work.
"""
def decorate_test(test):
original_argspec = inspect.getfullargspec(test)
argspec = new_argspec(original_argspec, auto_args)
original_signature = get_signature(test)
signature = new_signature(original_signature, auto_args)

if asyncio.iscoroutinefunction(test):
@pytest.mark.asyncio
@impersonate(test)
@define_function_signature(test.__name__, test.__doc__, argspec)
@define_function_signature(test.__name__, test.__doc__, signature)
async def wrapped_test(*args, **kwargs):
# Tell pytest to omit the body of this function from tracebacks
__tracebackhide__ = True
Expand Down Expand Up @@ -54,7 +55,7 @@ async def wrapped_test(*args, **kwargs):
return wrapped_test
else:
@impersonate(test)
@define_function_signature(test.__name__, test.__doc__, argspec)
@define_function_signature(test.__name__, test.__doc__, signature)
def wrapped_test(*args, **kwargs):
# Tell pytest to omit the body of this function from tracebacks
__tracebackhide__ = True
Expand All @@ -77,25 +78,19 @@ def wrapped_test(*args, **kwargs):
return decorate_test


def new_argspec(original_argspec, auto_args):
"""Make an updated argspec for the wrapped test."""
replaced_args = {
original_arg: auto_arg
for original_arg, auto_arg in zip(
original_argspec.args[:len(auto_args)],
auto_args
)
}
new_args = tuple(itertools.chain(
auto_args,
original_argspec.args[len(auto_args):]
def new_signature(original_signature: Signature, auto_args):
"""Make an updated signature for the wrapped test."""
# Replace the parameter names in the original signature with the names
# of the fixtures given to @autocontext(...) so that pytest will inject the
# right fixtures.
new_parameters = tuple(itertools.chain(
[
Parameter(name, Parameter.POSITIONAL_OR_KEYWORD)
for name in auto_args
],
list(original_signature.parameters.values())[len(auto_args):]
))
annots = {
replaced_args.get(k) or k: v
for k, v in original_argspec.annotations.items()
}
annots["return"] = None
return original_argspec._replace(
args=new_args,
annotations=annots
return original_signature.replace(
parameters=new_parameters,
return_annotation=None
)

0 comments on commit f1de83f

Please sign in to comment.