Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow downstream to disable deprecations-as-errors with environment var #989

Merged
merged 1 commit into from Nov 29, 2017

Conversation

Zac-HD
Copy link
Member

@Zac-HD Zac-HD commented Nov 26, 2017

Closes #969 - the Arch package has unbundled some upstream dependencies, so this option will be useful for @felixonmars 😄

CC @DRMacIver in case this is one of those things where we have divergent taste in solutions.

warnings.filterwarnings('error', category=HypothesisDeprecationWarning)
# We further enable deprecations as errors for all dependencies,
# unless disabled for Django 1.8, or by downstream (see issue #969)
disable_werror = os.environ.get('HYPOTHESIS_TESTS_NO_WERROR') == 'true'
Copy link
Member

@kxepal kxepal Nov 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  1. Shouldn't we document this?
  2. Should we provide pytest cli option to control this?
  3. Should we check env variable value our just ensure that this variable is set? For instance, standard Python env variables works in this way, so there is no difference between PYTHONOPTIMIZE=1 and PYTHONOPTIMIZE=true and PYTHONOPTIMIZE=yes-please.

UPD. sorry, nvm, that's internal change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s three questions. 😉

I’d prefer a pytest CLI option – particularly because tox doesn’t pass through environment variables by default, so at minimum this needs a change to tox.ini or I’m pretty sure won’t work. A CLI option is more explicit in that regard.

(Also I just generally dislike environment variables as a way to control behaviour, because they’re easy to set and forget, and then you have behaviour that seems to work by magic.)

Copy link
Member

@kxepal kxepal Nov 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I was hope I'll fix my misunderstood before anyone noticed (: Restored comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I liked your questions!

Copy link
Member

@kxepal kxepal Nov 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get all back, no worries (: And sorry for confussion /:

@Zac-HD
Copy link
Member Author

Zac-HD commented Nov 27, 2017

I'd be delighted if someone could show me how to add an argument via pytest - when I tried this it didn't work at all.

I'd document this in the notes added by #988, and will do that once either pull is merged.

I like explicitly checking the value of the var to avoid surprises if it's set to eg "0" or "false" - but I've added a warning for that case.

@kxepal
Copy link
Member

kxepal commented Nov 27, 2017

@Zac-HD
How about something like this?

diff --git a/src/hypothesis/extra/pytestplugin.py b/src/hypothesis/extra/pytestplugin.py
index 86f01d13..60c1eed2 100644
--- a/src/hypothesis/extra/pytestplugin.py
+++ b/src/hypothesis/extra/pytestplugin.py
@@ -17,6 +17,8 @@
 
 from __future__ import division, print_function, absolute_import
 
+import warnings
+
 import pytest
 
 import hypothesis.core as core
@@ -29,6 +31,7 @@ from hypothesis.internal.detection import is_hypothesis_test
 LOAD_PROFILE_OPTION = '--hypothesis-profile'
 PRINT_STATISTICS_OPTION = '--hypothesis-show-statistics'
 SEED_OPTION = '--hypothesis-seed'
+TEST_NO_WERROR = '--hypothesis-test-no-werror'
 
 
 class StoringReporter(object):
@@ -63,6 +66,12 @@ def pytest_addoption(parser):
         action='store',
         help='Set a seed to use for all Hypothesis tests'
     )
+    group.addoption(
+        TEST_NO_WERROR,
+        action='store_true',
+        help='Stop count warnings as errors',
+        default=False
+    )
 
 
 def pytest_configure(config):
@@ -81,6 +90,8 @@ def pytest_configure(config):
     config.addinivalue_line(
         'markers',
         'hypothesis: Tests which use hypothesis.')
+    if not config.getoption(TEST_NO_WERROR):
+        warnings.filterwarnings('error', category=DeprecationWarning)
 
 
 gathered_statistics = OrderedDict()

@kxepal
Copy link
Member

kxepal commented Nov 27, 2017

However, may be in this case would better to have WError enable flag rather then disable. I'm not sure if condition is correct and wouldn't be overriden later by someone else.

@kxepal
Copy link
Member

kxepal commented Nov 27, 2017

And again...if this switch is only for Hypothesis tests and doesn't affects users, it may be not wise to have it on public since it causes no effect for them (or at least it supposed to in original implementation - mine patch makes it global, eventually).

@DRMacIver
Copy link
Member

I feel like this might make more sense inverted? i.e. we don't turn warnings on unless an environment variable is set. This feature is mostly useful for our testing, and downstream packagers are almost guaranteed to want to disable it, so why not make it do the right thing by default for them?

I think if you do that, adding a pytest command line option is probably not that useful - if this is just for our testing and not for others' consumption, the option that involves the least amount of code is fine. :-)

@Zac-HD
Copy link
Member Author

Zac-HD commented Nov 27, 2017

...we should probably just set PYTHONWARNINGS in our Tox config. We can even override this for older Django in tox.ini, and it will always run in CI but can be trivially disabled locally.

@Zac-HD
Copy link
Member Author

Zac-HD commented Nov 28, 2017

  • Happy path confirmed OK via Travis
  • PYTHONWARNINGS=error::DeprecationWarning tox --recreate -e django18 fails as expected; passes without the env var

So we're ready to go!

In summary: if you set the standard PYTHONWARNINGS env var, you get the expected behaviour (including language defaults if it's set to the empty string). If the var is unset (eg on Travis), our Tox config defaults it to deprecation-warnings-as-errors.

Copy link
Member

@DRMacIver DRMacIver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks for doing the leg work on this!

@DRMacIver DRMacIver merged commit dd50f31 into HypothesisWorks:master Nov 29, 2017
@Zac-HD Zac-HD deleted the optional-test-werror branch November 29, 2017 14:09
@Zac-HD
Copy link
Member Author

Zac-HD commented Nov 29, 2017

No problem - I always learn something from the little fixes, and the tooling around Hypothesis is good enough that I never feel like my time is wasted 😄

@felixonmars
Copy link
Contributor

Works great here, thanks a lot :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants