Skip to content

Commit

Permalink
Merge pull request RobotLocomotion#10412 from EricCousineau-TRI/issue…
Browse files Browse the repository at this point in the history
…/10411

py symbolic: Filter updated warning message for == comparison
  • Loading branch information
EricCousineau-TRI authored Jan 15, 2019
2 parents 795c356 + fa12cb0 commit 3bf5b49
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
12 changes: 8 additions & 4 deletions bindings/pydrake/common/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ def install_numpy_warning_filters(force=False):
return
_installed_numpy_warning_filters = True
# Warnings specific to comparison with `dtype=object` should be raised to
# errors (#8315, #8491). Without them, NumPy will return effectively
# garbage values (e.g. comparison based on object ID): either a scalar bool
# or an array of bools (based on what objects are present and the NumPy
# version).
# errors (#8315, #8491). Without them, NumPy will swallow the errors and
# make a DeprecationWarning, while returning effectively garbage values
# (e.g. comparison based on object ID): either a scalar bool or an array of
# bools (based on what objects are present and the NumPy version).
# N.B. Using a `module=` regex filter does not work, as the warning is
# raised from C code, and thus inherits the calling module, which may not
# be "numpy\..*" (numpy/numpy#10861).
Expand All @@ -177,6 +177,10 @@ def install_numpy_warning_filters(force=False):
warnings.filterwarnings(
"error", category=DeprecationWarning,
message="elementwise == comparison failed")
# Error changed in 1.16.0
warnings.filterwarnings(
"error", category=DeprecationWarning,
message="elementwise comparison failed")


warnings.simplefilter('once', DrakeDeprecationWarning)
Expand Down
21 changes: 9 additions & 12 deletions bindings/pydrake/test/symbolic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pydrake.common
from pydrake.test.algebra_test_util import ScalarAlgebra, VectorizedAlgebra
from pydrake.util.containers import EqualToDict
from pydrake.util.deprecation import install_numpy_warning_filters

# TODO(eric.cousineau): Replace usages of `sym` math functions with the
# overloads from `pydrake.math`.
Expand Down Expand Up @@ -306,6 +307,14 @@ def test_iterable(self):


class TestSymbolicExpression(SymbolicTestCase):
def setUp(self):
SymbolicTestCase.setUp(self)
# For some reason, something in how `unittest` tries to scope warnings
# causes the previous filters to be lost. Re-install here.
# TODO(eric.cousineau): This used to be necessary for PY3-only, but
# with NumPy 1.16, it became PY2 too. Figure out why.
install_numpy_warning_filters(force=True)

def _check_scalar(self, actual, expected):
self.assertIsInstance(actual, sym.Expression)
# Chain conversion to ensure equivalent treatment.
Expand Down Expand Up @@ -511,18 +520,6 @@ def test_relational_operators_nonzero(self):
e_yv = np.array([e_y, e_y])
# N.B. In some versions of NumPy, `!=` for dtype=object implies ID
# comparison (e.g. `is`).
# N.B. If `__nonzero__` throws, then NumPy swallows the error and
# produces a DeprecationWarning, in addition to effectively garbage
# values. For this reason, `pydrake.symbolic` will automatically
# promote these warnings to errors.
if six.PY3:
# For some reason, something in how `unittest` tries to scope
# warnings causes the previous filters to be lost. Re-install
# here.
# TODO(eric.cousineau): Figure out better hook for this, or better
# way to restore warning filters from when everything's imported.
from pydrake.util.deprecation import install_numpy_warning_filters
install_numpy_warning_filters(force=True)
# - All false.
with self.assertRaises(DeprecationWarning):
value = (e_xv == e_yv)
Expand Down
11 changes: 9 additions & 2 deletions common/test_utilities/drake_py_unittest_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@
"client (non-batch), output may be mixed, so piping makes " +
"the output more readable.")
parser.add_argument(
"--drake_deprecation_action", type=str, default="error",
help="Action for Drake deprecation warnings. See " +
"--deprecation_action", type=str, default="once",
help="Action for any deprecation warnings. See " +
"`warnings.simplefilter()`.")
parser.add_argument(
"--drake_deprecation_action", type=str, default="error",
help="Action for Drake deprecation warnings. Applied after " +
"--deprecation_action.")
args, remaining = parser.parse_known_args()
sys.argv = sys.argv[:1] + remaining

Expand All @@ -119,6 +123,9 @@ def run():
sys.stdout.flush()
sys.stdout = sys.stderr

# Ensure deprecation warnings are always shown at least once.
warnings.simplefilter(args.deprecation_action, DeprecationWarning)
# Handle Drake-specific deprecations.
if has_pydrake:
warnings.simplefilter(
args.drake_deprecation_action, DrakeDeprecationWarning)
Expand Down

0 comments on commit 3bf5b49

Please sign in to comment.