Skip to content

Commit e0ef1da

Browse files
Merge pull request sympy#19247 from oscarbenjamin/pr_warnings_tests
Fix handling of warnings in the test runner
2 parents 6908bc8 + b38ec6b commit e0ef1da

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

sympy/core/basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ def _has(self, pattern):
12391239
return any(f.func == pattern or f == pattern
12401240
for f in self.atoms(Function, UndefinedFunction))
12411241

1242-
pattern = sympify(pattern)
1242+
pattern = _sympify(pattern)
12431243
if isinstance(pattern, BasicMeta):
12441244
return any(isinstance(arg, pattern)
12451245
for arg in preorder_traversal(self))

sympy/core/containers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Dict(Basic):
202202
cannot be changed afterwards. Otherwise it behaves identically
203203
to the Python dict.
204204
205+
>>> from sympy import Symbol
205206
>>> from sympy.core.containers import Dict
206207
207208
>>> D = Dict({1: 'one', 2: 'two'})
@@ -215,7 +216,7 @@ class Dict(Basic):
215216
216217
>>> 1 in D
217218
True
218-
>>> D.has('one') # searches keys and values
219+
>>> D.has(Symbol('one')) # searches keys and values
219220
True
220221
>>> 'one' in D # not in the keys
221222
False

sympy/core/tests/test_basic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
_atomic, _aresame)
99
from sympy.core.singleton import S
1010
from sympy.core.symbol import symbols, Symbol
11+
from sympy.core.sympify import SympifyError
1112
from sympy.core.function import Function, Lambda
1213
from sympy.core.compatibility import default_sort_key
1314

@@ -102,6 +103,7 @@ def test_has():
102103
assert b21.has(Basic)
103104
assert not b1.has(b21, b3)
104105
assert not b21.has()
106+
raises(SympifyError, lambda: Symbol("x").has("x"))
105107

106108

107109
def test_subs():

sympy/functions/elementary/miscellaneous.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ def sqrt(arg, evaluate=None):
129129
>>> sqrt(x).has(sqrt)
130130
Traceback (most recent call last):
131131
...
132-
sympy.core.sympify.SympifyError: Sympify of expression 'could not parse
133-
'<function sqrt at 0x7f79ad860f80>'' failed, because of exception being
134-
raised:
135-
SyntaxError: invalid syntax
132+
sympy.core.sympify.SympifyError: SympifyError: <function sqrt at 0x10e8900d0>
136133
137134
To find ``sqrt`` look for ``Pow`` with an exponent of ``1/2``:
138135

sympy/printing/tableform.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def __init__(self, data, **kwarg):
9696
Examples
9797
========
9898
99-
>>> from sympy import TableForm, Matrix
99+
>>> from sympy import TableForm, Symbol
100100
>>> TableForm([[5, 7], [4, 2], [10, 3]])
101101
5 7
102102
4 2
@@ -107,7 +107,7 @@ def __init__(self, data, **kwarg):
107107
1 | .
108108
2 | . .
109109
3 | . . .
110-
>>> TableForm([['.'*(j if not i%2 else 1) for i in range(3)]
110+
>>> TableForm([[Symbol('.'*(j if not i%2 else 1)) for i in range(3)]
111111
... for j in range(4)], alignments='rcl')
112112
.
113113
. . .

sympy/solvers/solveset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,8 +3003,8 @@ def _solve_using_known_values(result, solver):
30033003
# list.
30043004
result.remove(res)
30053005
continue # skip as it's independent of desired symbols
3006-
depen = (eq2.rewrite(Add)).as_independent(unsolved_syms)[0]
3007-
if depen.has(Abs) and solver == solveset_complex:
3006+
depen1, depen2 = (eq2.rewrite(Add)).as_independent(*unsolved_syms)
3007+
if (depen1.has(Abs) or depen2.has(Abs)) and solver == solveset_complex:
30083008
# Absolute values cannot be inverted in the
30093009
# complex domain
30103010
continue

sympy/testing/runtests.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
import signal
3333
import stat
3434
import tempfile
35+
import warnings
36+
from contextlib import contextmanager
3537

3638
from sympy.core.cache import clear_cache
3739
from sympy.core.compatibility import (exec_, PY3, unwrap,
3840
unicode)
3941
from sympy.utilities.misc import find_executable
4042
from sympy.external import import_module
41-
from sympy.utilities.exceptions import SymPyDeprecationWarning
4243

4344
IS_WINDOWS = (os.name == 'nt')
4445
ON_TRAVIS = os.getenv('TRAVIS_BUILD_NUMBER', None)
@@ -156,6 +157,21 @@ def setup_pprint():
156157
interactive_printing.NO_GLOBAL = True
157158
return use_unicode_prev
158159

160+
161+
@contextmanager
162+
def raise_on_deprecated():
163+
"""Context manager to make DeprecationWarning raise an error
164+
165+
This is to catch SymPyDeprecationWarning from library code while running
166+
tests and doctests. It is important to use this context manager around
167+
each individual test/doctest in case some tests modify the warning
168+
filters.
169+
"""
170+
with warnings.catch_warnings():
171+
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
172+
yield
173+
174+
159175
def run_in_subprocess_with_hash_randomization(
160176
function, function_args=(),
161177
function_kwargs=None, command=sys.executable,
@@ -537,11 +553,6 @@ def _test(*paths, **kwargs):
537553
fast_threshold=fast_threshold,
538554
slow_threshold=slow_threshold)
539555

540-
# Show deprecation warnings
541-
import warnings
542-
warnings.simplefilter("error", SymPyDeprecationWarning)
543-
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
544-
545556
test_files = t.get_test_files('sympy')
546557

547558
not_blacklisted = [f for f in test_files
@@ -786,11 +797,6 @@ def _doctest(*paths, **kwargs):
786797
from sympy.plotting.plot import unset_show
787798
unset_show()
788799

789-
# Show deprecation warnings
790-
import warnings
791-
warnings.simplefilter("error", SymPyDeprecationWarning)
792-
warnings.filterwarnings('error', '.*', DeprecationWarning, module='sympy.*')
793-
794800
r = PyTestReporter(verbose, split=split, colors=colors,\
795801
force_colors=force_colors)
796802
t = SymPyDocTests(r, normal)
@@ -1278,11 +1284,12 @@ def test_file(self, filename, sort=True, timeout=False, slow=False,
12781284
try:
12791285
if getattr(f, '_slow', False) and not slow:
12801286
raise Skipped("Slow")
1281-
if timeout:
1282-
self._timeout(f, timeout, fail_on_timeout)
1283-
else:
1284-
random.seed(self._seed)
1285-
f()
1287+
with raise_on_deprecated():
1288+
if timeout:
1289+
self._timeout(f, timeout, fail_on_timeout)
1290+
else:
1291+
random.seed(self._seed)
1292+
f()
12861293
except KeyboardInterrupt:
12871294
if getattr(f, '_slow', False):
12881295
reporter.test_skip("KeyboardInterrupt")
@@ -1828,15 +1835,17 @@ def run(self, test, compileflags=None, out=None, clear_globs=True):
18281835
self.save_linecache_getlines = pdoctest.linecache.getlines
18291836
linecache.getlines = self.__patched_linecache_getlines
18301837

1831-
try:
1832-
test.globs['print_function'] = print_function
1833-
return self.__run(test, compileflags, out)
1834-
finally:
1835-
sys.stdout = save_stdout
1836-
pdb.set_trace = save_set_trace
1837-
linecache.getlines = self.save_linecache_getlines
1838-
if clear_globs:
1839-
test.globs.clear()
1838+
# Fail for deprecation warnings
1839+
with raise_on_deprecated():
1840+
try:
1841+
test.globs['print_function'] = print_function
1842+
return self.__run(test, compileflags, out)
1843+
finally:
1844+
sys.stdout = save_stdout
1845+
pdb.set_trace = save_set_trace
1846+
linecache.getlines = self.save_linecache_getlines
1847+
if clear_globs:
1848+
test.globs.clear()
18401849

18411850

18421851
# We have to override the name mangled methods.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy