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

maint(test): suppress warnings from the test suite #23745

Closed
wants to merge 10 commits into from

Conversation

oscarbenjamin
Copy link
Collaborator

References to other Issues or PRs

#23061

Brief description of what is fixed or changed

Other comments

Release Notes

NO ENTRY

@oscarbenjamin oscarbenjamin added this to the SymPy 1.11 milestone Jul 8, 2022
@sympy-bot
Copy link

sympy-bot commented Jul 8, 2022

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

  • No release notes entry will be added for this pull request.
Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

#23061

#### Brief description of what is fixed or changed


#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->

@oscarbenjamin oscarbenjamin changed the title maint(test): suppress warnigns from the test suite maint(test): suppress warnings from the test suite Jul 8, 2022
@oscarbenjamin oscarbenjamin force-pushed the pr_test_warnings branch 2 times, most recently from ead1b96 to 6c9db2a Compare July 10, 2022 13:01
@@ -139,7 +139,7 @@ def _get_or_create(self, s, name=None, dtype=None, broadcastable=None):
if key in self.cache:
return self.cache[key]

value = aet.tensor(name=name, dtype=dtype, broadcastable=broadcastable)
value = aet.tensor(name=name, dtype=dtype, shape=broadcastable)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@brandonwillard is this change safe for both Theano and Aesara?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also another warning that I haven't managed to suppress yet:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/aesara/link/c/lazylinker_c.py", line 129, in <module>
    code = open(cfile).read()
ResourceWarning: unclosed file <_io.TextIOWrapper name='/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/aesara/link/c/c_code/lazylinker_c.c' mode='r' encoding='UTF-8'>

The complaint is about not using with but it's coming from the Aesara code.

Copy link
Contributor

Choose a reason for hiding this comment

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

@brandonwillard is this change safe for both Theano and Aesara?

No, it won't work for Theano.

Copy link
Contributor

@brandonwillard brandonwillard Jul 10, 2022

Choose a reason for hiding this comment

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

Also another warning that I haven't managed to suppress yet:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/aesara/link/c/lazylinker_c.py", line 129, in <module>
    code = open(cfile).read()
ResourceWarning: unclosed file <_io.TextIOWrapper name='/opt/hostedtoolcache/Python/3.9.13/x64/lib/python3.9/site-packages/aesara/link/c/c_code/lazylinker_c.c' mode='r' encoding='UTF-8'>

The complaint is about not using with but it's coming from the Aesara code.

We can create an Aesara issue for that, but let's see if we can reproduce it in isolation first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it won't work for Theano.

Thanks. I made a spinout PR from this one that included that change but I guess I'll need to revert that and give this more thought: https://github.com/sympy/sympy/pull/23754/files#r917456138

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let's see if we can reproduce it in isolation first.

The warning is seen with -Werror as in

$ python -Werror myscript.py

or alternatively

$ PYTHONWARNINGS=error python myscript.py

Either way the line code = open(cfile).read() should be changed to use with if that line is in the codebase.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've opened a PR to clean up those issues: aesara-devs/aesara#1049.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks.

Another one that I often see is this:

>>> import aesara
WARNING (aesara.tensor.blas): Using NumPy C-API based implementation for BLAS functions.

That one is a logger warning rather than a Warning warning so it isn't filtered in the usual way. There is some code here to hide it:

aesaralogger = logging.getLogger('aesara.configdefaults')
aesaralogger.setLevel(logging.CRITICAL)
aesara = import_module('aesara')
aesaralogger.setLevel(logging.WARNING)

The problem is that the warning is emitted on import so we would need to guard every place that imports aesara in case some other place imports it first:
if import_module('aesara') is None:

I'm not sure what the best approach is for that warning but emitting a visible message to stderr during import doesn't seem great. Note that the line of code above is just testing to see if aesara is installed but that triggers the warning.

Comment on lines 388 to 396
# XXX: This should be fixed in experimental_lambdify or by using
# ordinary lambdify so that it doesn't warn. The error results from
# passing an array of values as the integration limit.
#
# UserWarning: The evaluation of the expression is problematic. We are
# trying a failback method that may still work. Please report this as a
# bug.
with ignore_warnings(UserWarning):
p.save(os.path.join(tmpdir, filename))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure if there is already an issue for this but I seem to remember a PR that attempted to fix this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The issue is #19996

assert list(textplot_str(sqrt(x), -1, 1)) == lines
# RuntimeWarning: invalid value encountered in sqrt
with ignore_warnings(RuntimeWarning):
assert list(textplot_str(sqrt(x), -1, 1)) == lines
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Maybe the plotting module should use np.errstate if it expects to deliberately pass invalid values:
https://numpy.org/doc/stable/reference/generated/numpy.errstate.html

@@ -138,9 +139,11 @@ def test_Matrix1():

def test_Matrix2():
m = Matrix([[x, x**2], [5, 2/x]])
assert (matrix(m.subs(x, 2)) == matrix([[2, 4], [5, 1]])).all()
with ignore_warnings(PendingDeprecationWarning):
assert (matrix(m.subs(x, 2)) == matrix([[2, 4], [5, 1]])).all()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As far as I can tell all the tests involving np.matrix are just checking that it can be used with sympy. It doesn't look like sympy actually uses np.matrix for anything.

stderr=subprocess.STDOUT)
if retcode != 0:
return False
with open(os.devnull, 'w') as null:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I found cases where with should be used to ensure that files are closed properly. That should probably be reviewed in bulk across the codebase.

Comment on lines +314 to +319
# The ufuncify function generates a setup.py that uses distutils which
# is now deprecated:
#
# DeprecationWarning: The distutils package is deprecated and slated
# for removal in Python 3.12. Use setuptools or check PEP 632 for
# potential alternatives
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Places where distutils is used:

$ git grep distutils
bin/test_travis.sh:import distutils.sysconfig
bin/test_travis.sh:with open(os.path.join(distutils.sysconfig.get_python_lib(), 'coverage.pth'), 'w') as pth:
doc/src/modules/utilities/autowrap.rst:system installed f2py command. The Cython backend runs a distutils setup script
release/fabfile.py:    distutils_check()
release/fabfile.py:def distutils_check():
setup.py:This uses Distutils (https://python.org/sigs/distutils-sig/) the standard
setup.py:from distutils.command.sdist import sdist
setup.py:    from distutils.core import setup, Command
setup.py:    user_options = []  # distutils complains if this is not here.
setup.py:    def initialize_options(self):  # distutils wants this
setup.py:    user_options = []  # distutils complains if this is not here.
setup.py:    def initialize_options(self):  # distutils wants this
setup.py:    user_options = []  # distutils complains if this is not here.
setup.py:    def initialize_options(self):  # distutils wants this
sympy/external/importtools.py:    # Simplified from distutils.version.LooseVersion which was deprecated in
sympy/utilities/_compilation/compilation.py:    # from distutils/command/build_ext.py:
sympy/utilities/_compilation/util.py:    # distutils: language = c++
sympy/utilities/autowrap.py:    from distutils.core import setup
sympy/utilities/autowrap.py:    from distutils.extension import Extension
sympy/utilities/autowrap.py:        The following optional parameters get passed to ``distutils.Extension``
sympy/utilities/autowrap.py:    from numpy.distutils.misc_util import Configuration
sympy/utilities/autowrap.py:    from numpy.distutils.core import setup
sympy/utilities/tests/test_autowrap.py:    from distutils.core import setup
sympy/utilities/tests/test_autowrap.py:    from distutils.extension import Extension
sympy/utilities/tests/test_autowrap.py:    from distutils.core import setup
sympy/utilities/tests/test_autowrap.py:    from distutils.extension import Extension
sympy/utilities/tests/test_autowrap.py:    from distutils.core import setup
sympy/utilities/tests/test_autowrap.py:    from distutils.extension import Extension
  • Travis and fabfile don't matter.
  • The main setup.py should be removed and replaced with pyproject.toml.
  • The external module only shows up because of a comment.
  • The problematic cases are all in autowrap.

The autowrap module needs to be changed to not use distutils or numpy.distutils which are both deprecated. It isn't immediately clear to me what the appropriate replacements for these are for a nontrivial setup.py that actually needs to compile some C code.

examples/all.py Outdated
Comment on lines 88 to 94
def load_example_module(example):
"""Loads modules based upon the given package name"""
mod = __import__(example)
return mod
from importlib import import_module

exmod = os.path.split(EXAMPLE_DIR)[1]
modname = exmod + '.' + example
return import_module(modname)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure if this code is precisely equivalent but the previous code was somewhat over engineered for what actually needs to be done here.

Comment on lines +66 to +74
# aesaracode imports aesara which imports distutils which warns:
#
# DeprecationWarning: The distutils package is deprecated and
# slated for removal in Python 3.12. Use setuptools or check PEP
# 632 for potential alternatives
if submodule == 'sympy.printing.aesaracode':
warnings = DeprecationWarning
else:
warnings = ()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Lots of places where we need to suppress warnings from Aesara. This can be reverted if Aesara stops using distutils.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we have an issue for this change, but I can quickly make a PR regardless.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I was thinking of just plain distutils. I've already created a PR for that, though: aesara-devs/aesara#1050.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's both kinds of distutils :)

The numpy.distutils deprecation is just a bit louder:

$ python -Werror -c 'import aesara'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/__init__.py", line 126, in <module>
    from aesara import scalar, tensor
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/scalar/__init__.py", line 1, in <module>
    from .basic import *
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/scalar/basic.py", line 25, in <module>
    from aesara import printing
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/printing.py", line 20, in <module>
    from aesara.compile import Function, SharedVariable
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/compile/__init__.py", line 19, in <module>
    from aesara.compile.mode import (
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/compile/mode.py", line 28, in <module>
    from aesara.link.c.basic import CLinker, OpWiseCLinker
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/link/c/basic.py", line 26, in <module>
    from aesara.link.c.cmodule import (
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/aesara/link/c/cmodule.py", line 25, in <module>
    import numpy.distutils
  File "/home/oscar/current/sympy/sympy.git/38venv/lib/python3.8/site-packages/numpy/distutils/__init__.py", line 31, in <module>
    warnings.warn("\n\n"
DeprecationWarning: 

  `numpy.distutils` is deprecated since NumPy 1.23.0, as a result
  of the deprecation of `distutils` itself. It will be removed for
  Python >= 3.12. For older Python versions it will remain present.
  It is recommended to use `setuptools < 60.0` for those Python versions.
  For more details, see:
    https://numpy.org/devdocs/reference/distutils_status_migration.html 

Copy link
Contributor

Choose a reason for hiding this comment

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

All right, I added both to that PR.

Comment on lines +34 to +41

# Warning when importing jax (comes indirectly because jax imports
# flatbuffers):
#
# DeprecationWarning: the imp module is deprecated in favour of importlib; see
# the module's documentation for alternative uses
with ignore_warnings(DeprecationWarning):
jax = import_module('jax')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines +22 to +29
with ignore_warnings(DeprecationWarning):
# Warnings when importing tensorflow:
#
# DeprecationWarning: The distutils package is deprecated and slated for
# removal in Python 3.12. Use setuptools or check PEP 632 for potential
# alternatives
from sympy.printing.tensorflow import tensorflow_code
tf = tensorflow = import_module("tensorflow")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Similar to Aesara. When tensorflow stops using distutils these kinds of changes can be reverted.

Comment on lines +167 to +175
# XXX: Sampling with scipy under -Werror fails with
#
# RuntimeWarning: invalid value encountered in _cdf_single (vectorized)
#
# It isn't immediately clear if this is a bug in SciPy or SymPy.

with ignore_warnings(RuntimeWarning):
assert sample(Z) in Z.pspace.domain.set
sym, val = list(Z.pspace.sample().items())[0]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This warrants further investigation. When I checked in the debugger it looked as if sympy was passing valid inputs to scipy so I don't know if the warning arises from a bug in the scipy code. Need to distil a reproducer that doesn't involve the layers of sympy code that make this quite convoluted to debug.

Comment on lines +214 to +222
# XXX: This should be fixed in lambdify to not use numpy functions from the
# scipy module.
#
# DeprecationWarning: scipy.greater is deprecated and will be removed
# in SciPy 2.0.0, use numpy.greater instead
# DeprecationWarning: scipy.sin is deprecated and will be removed in SciPy
# 2.0.0, use numpy.sin instead
with ignore_warnings(DeprecationWarning):
assert P(X + Y > 0, Y < 0, numsamples=10).is_number
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is a significant problem in lambdify. The scipy module has lots of functions that are really just imported from numpy like scipy.sin etc. These are all now deprecated. That means that lambdify should always prefer a function like numpy.sin over scipy.sin even if the module is set to "scipy".

I'm not sure how easy it is to fix this in lambdify because it isn't clear to me exactly how these names are picked from one module or the other e.g.:

In [8]: f = lambdify(x, sin(x), 'scipy')

In [9]: print(inspect.getsource(f))
def _lambdifygenerated(x):
    return sin(x)

@@ -2030,7 +2057,7 @@ def findout_terminal_width():
process = subprocess.Popen(['stty', '-a'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout = process.stdout.read()
stdout, stderr = process.communicate()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Similar to using with open this is needed to ensure that the subprocess completes rather than becoming a dangling zombie process.

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

Successfully merging this pull request may close these issues.

3 participants