diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2016-01-29 01:17:55 +0100 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2016-09-02 16:39:17 +0200 |
commit | 4ec016784b02f69347c4429829b9eb8225ace5ee (patch) | |
tree | c058c5b7e4edec3ee0b5bc13dff55ded65cb5042 | |
parent | 7884a8c9f5f5c6657413dbeaa59ad969280d38ea (diff) | |
download | numpy-4ec016784b02f69347c4429829b9eb8225ace5ee.tar.gz |
TST: Add tests for stacklevel in warnings and "ignore" filters.
Enforces that stacklevel is used in warnings.warn and "ignore"
is not used in filterwarnings or simplefilter for most of numpy.
-rw-r--r-- | numpy/distutils/command/egg_info.py | 2 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 8 | ||||
-rw-r--r-- | numpy/tests/test_warnings.py | 86 |
3 files changed, 91 insertions, 5 deletions
diff --git a/numpy/distutils/command/egg_info.py b/numpy/distutils/command/egg_info.py index 972a27df3..7176f9212 100644 --- a/numpy/distutils/command/egg_info.py +++ b/numpy/distutils/command/egg_info.py @@ -10,7 +10,7 @@ class egg_info(_egg_info): import warnings warnings.warn("`build_src` is being run, this may lead to missing " "files in your sdist! See numpy issue gh-7127 for " - "details", UserWarning) + "details", UserWarning, stacklevel=2) # We need to ensure that build_src has been executed in order to give # setuptools' egg_info command real filenames instead of functions which diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index cbab5ad5b..7cac90628 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -2450,12 +2450,12 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): x /= t(2) assert_equal(x, y) except (DeprecationWarning, TypeError) as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=1) try: xm /= t(2) assert_equal(xm, y) except (DeprecationWarning, TypeError) as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=1) if issubclass(t, np.integer): assert_equal(len(sup.log), 2, "Failed on type=%s." % t) @@ -2485,7 +2485,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): x /= a assert_equal(x, y / a) except (DeprecationWarning, TypeError) as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=1) try: xm /= a assert_equal(xm, y / a) @@ -2494,7 +2494,7 @@ class TestMaskedArrayInPlaceArithmetics(TestCase): mask_or(mask_or(m, a.mask), (a == t(0))) ) except (DeprecationWarning, TypeError) as e: - warnings.warn(str(e)) + warnings.warn(str(e), stacklevel=1) if issubclass(t, np.integer): assert_equal(len(sup.log), 2, "Failed on type=%s." % t) diff --git a/numpy/tests/test_warnings.py b/numpy/tests/test_warnings.py new file mode 100644 index 000000000..dcae60a81 --- /dev/null +++ b/numpy/tests/test_warnings.py @@ -0,0 +1,86 @@ +""" +Tests which scan for certain occurances in the code, they may not find +all of these occurances but should catch almost all. +""" + + +from __future__ import division, absolute_import, print_function + + +import sys +if sys.version_info >= (3, 4): + from pathlib import Path + import ast + import tokenize + import numpy + from numpy.testing import run_module_suite + from numpy.testing.decorators import slow + + + class ParseCall(ast.NodeVisitor): + def __init__(self): + self.ls = [] + + def visit_Attribute(self, node): + ast.NodeVisitor.generic_visit(self, node) + self.ls.append(node.attr) + + def visit_Name(self, node): + self.ls.append(node.id) + + + class FindFuncs(ast.NodeVisitor): + def __init__(self, filename): + super().__init__() + self.__filename = filename + + def visit_Call(self, node): + p = ParseCall() + p.visit(node.func) + ast.NodeVisitor.generic_visit(self, node) + + if p.ls[-1] == 'simplefilter' or p.ls[-1] == 'filterwarnings': + if node.args[0].s == "ignore": + raise AssertionError( + "ignore filter should not be used; found in " + "{} on line {}".format(self.__filename, node.lineno)) + + if p.ls[-1] == 'warn' and ( + len(p.ls) == 1 or p.ls[-2] == 'warnings'): + + if "testing/tests/test_warnings.py" is self.__filename: + # This file + return + + # See if stacklevel exists: + if len(node.args) == 3: + return + args = {kw.arg for kw in node.keywords} + if "stacklevel" in args: + return + raise AssertionError( + "warnings should have an appropriate stacklevel; found in " + "{} on line {}".format(self.__filename, node.lineno)) + + + @slow + def test_warning_calls(): + # combined "ignore" and stacklevel error + base = Path(numpy.__file__).parent + + for path in base.rglob("*.py"): + if base / "testing" in path.parents: + continue + if path == base / "__init__.py": + continue + if path == base / "random" / "__init__.py": + continue + # use tokenize to auto-detect encoding on systems where no + # default encoding is defined (e.g. LANG='C') + with tokenize.open(str(path)) as file: + tree = ast.parse(file.read()) + FindFuncs(path).visit(tree) + + + if __name__ == "__main__": + run_module_suite() |