Skip to content

Commit

Permalink
Merge pull request #526 from iamdefinitelyahuman/warn-hypothesis
Browse files Browse the repository at this point in the history
Warn on direct import of `hypothesis.given`
  • Loading branch information
iamdefinitelyahuman committed May 17, 2020
2 parents 4cc215f + 8ce3e74 commit 5a22ede
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 4 additions & 0 deletions brownie/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,7 @@ class BrownieEnvironmentWarning(Warning):

class InvalidArgumentWarning(BrownieEnvironmentWarning):
pass


class BrownieTestWarning(Warning):
pass
19 changes: 18 additions & 1 deletion brownie/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hypothesis.errors import HypothesisDeprecationWarning

from brownie import network
from brownie.exceptions import BrownieTestWarning

from .stateful import state_machine # NOQA: F401
from .strategies import strategy # NOQA: F401
Expand All @@ -30,11 +31,27 @@ def isolation_wrapper(*args, **kwargs):

# hypothesis.given must wrap the target test to correctly
# impersonate the call signature for pytest
hy_given = hypothesis.given(*given_args, **given_kwargs)
hy_given = _hypothesis_given(*given_args, **given_kwargs)
hy_wrapped = hy_given(test)

if hasattr(hy_wrapped, "hypothesis"):
hy_wrapped.hypothesis.inner_test = isolation_wrapper
return hy_wrapped

return outer_wrapper


def _given_warning_wrapper(*args, **kwargs):
warnings.warn(
"Directly importing `hypothesis.given` may result in improper isolation"
" between test runs. You should import `brownie.test.given` instead.",
BrownieTestWarning,
)
return _hypothesis_given(*args, **kwargs)


def _apply_given_wrapper():
hypothesis.given = _given_warning_wrapper


_hypothesis_given = hypothesis.given
3 changes: 2 additions & 1 deletion brownie/test/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

from brownie._config import CONFIG
from brownie.project.scripts import _get_ast_hash
from brownie.test import coverage, output
from brownie.test import _apply_given_wrapper, coverage, output

from .utils import convert_outcome


class PytestBrownieBase:
def __init__(self, config, project):
_apply_given_wrapper()

self.config = config
# required when brownie project is in a subfolder of another project
Expand Down

0 comments on commit 5a22ede

Please sign in to comment.