-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make deprecations respect warnings filters and add --ignore-pants-war…
…nings option (#7496) ### Problem #7493 solved its problem by converting an error to a warning, but it's not likely that a large monorepo is going to be able to solve that particular issue anytime soon. So while we would like to be able to deprecate the `--strict` option in that PR, we don't think it's reasonable to have a deprecation warning showing for the strict option immediately. But not every pants user is a large monorepo, so we would like to be able to do the right upstream thing (add a deprecation warning) while allowing downstream users to defer showing specific warnings in a structured way. ### Solution - Add an `--ignore-pants-warnings` global bootstrap option, which calls [`warnings.filterwarnings()`](https://docs.python.org/2/library/warnings.html#warnings.filterwarnings) to filter warning messages by regex matching. - Make `warn_or_error()` in `deprecated.py` use `warnings.warn_explicit()`, because it turns out that `warnings.showwarning()` doesn't respect the warning filters at all (oops!). ### Result It's possible to filter out specific warnings with an option, and it's possible to do that to deprecation warnings now as well.
- Loading branch information
1 parent
3f515b4
commit ef33b62
Showing
9 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
testprojects/pants-plugins/src/python/test_pants_plugin/tasks/deprecation_warning_task.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
# coding=utf-8 | |||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | |||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | |||
|
|||
from __future__ import absolute_import, division, print_function, unicode_literals | |||
|
|||
import sys | |||
|
|||
from pants.base.deprecated import warn_or_error | |||
from pants.task.task import Task | |||
|
|||
|
|||
class DeprecationWarningTask(Task): | |||
"""Make a deprecation warning so that warning filters can be integration tested.""" | |||
|
|||
def execute(self): | |||
warn_or_error(removal_version='999.999.9.dev9', | |||
deprecated_entity_description='This is a test warning!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
# coding=utf-8 | |||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | |||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | |||
|
|||
from __future__ import absolute_import, division, print_function, unicode_literals | |||
|
|||
import os | |||
|
|||
from pants.base.build_environment import get_buildroot | |||
from pants.option.scope import GLOBAL_SCOPE_CONFIG_SECTION | |||
from pants_test.pants_run_integration_test import PantsRunIntegrationTest | |||
from pants_test.testutils.py2_compat import assertRegex | |||
|
|||
|
|||
class RunnerIntegrationTest(PantsRunIntegrationTest): | |||
"""Test logic performed in PantsRunner.""" | |||
|
|||
def _deprecation_warning_cmdline(self): | |||
# Load the testprojects pants-plugins to get some testing tasks and subsystems. | |||
testproject_backend_src_dir = os.path.join( | |||
get_buildroot(), 'testprojects/pants-plugins/src/python') | |||
testproject_backend_pkg_name = 'test_pants_plugin' | |||
deprecation_warning_cmdline = [ | |||
'--no-enable-pantsd', | |||
"--pythonpath=+['{}']".format(testproject_backend_src_dir), | |||
"--backend-packages=+['{}']".format(testproject_backend_pkg_name), | |||
# This task will always emit a DeprecationWarning. | |||
'deprecation-warning-task', | |||
] | |||
return deprecation_warning_cmdline | |||
|
|||
def test_warning_filter(self): | |||
cmdline = self._deprecation_warning_cmdline() | |||
|
|||
warning_run = self.run_pants(cmdline) | |||
self.assert_success(warning_run) | |||
assertRegex( | |||
self, | |||
warning_run.stderr_data, | |||
'\\[WARN\\].*DeprecationWarning: DEPRECATED: This is a test warning!') | |||
|
|||
non_warning_run = self.run_pants(cmdline, config={ | |||
GLOBAL_SCOPE_CONFIG_SECTION: { | |||
# NB: We do *not* include the exclamation point at the end, which tests that the regexps | |||
# match from the beginning of the warning string, and don't require matching the entire | |||
# string! We also lowercase the message to check that they are matched case-insensitively. | |||
'ignore_pants_warnings': ['deprecated: this is a test warning'] | |||
}, | |||
}) | |||
self.assert_success(non_warning_run) | |||
self.assertNotIn('DEPRECATED', non_warning_run.stderr_data) | |||
self.assertNotIn('test warning', non_warning_run.stderr_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters