Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Avoid PytestRemovedIn8Warning for pytest.warns(None)
  • Loading branch information
bnavigator committed Jun 16, 2022
commit 7f883f67b7f0e2782a1fa8e965b9e9bd7dbd691d
39 changes: 16 additions & 23 deletions control/tests/iosys_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import re
import warnings

import numpy as np
import pytest
Expand All @@ -17,6 +18,7 @@
from control import iosys as ios
from control.tests.conftest import matrixfilter


class TestIOSys:

@pytest.fixture
Expand Down Expand Up @@ -1416,11 +1418,10 @@ def test_duplicates(self, tsys):
nlios2 = ios.NonlinearIOSystem(None,
lambda t, x, u, params: u * u,
inputs=1, outputs=1, name="nlios2")
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
ct.InterconnectedSystem([nlios1, iosys_siso, nlios2],
inputs=0, outputs=0, states=0)
if record:
pytest.fail("Warning not expected: " + record[0].message)


def test_linear_interconnection():
Expand Down Expand Up @@ -1510,7 +1511,7 @@ def predprey(t, x, u, params={}):

def pvtol(t, x, u, params={}):
"""Reduced planar vertical takeoff and landing dynamics"""
from math import sin, cos
from math import cos, sin
m = params.get('m', 4.) # kg, system mass
J = params.get('J', 0.0475) # kg m^2, system inertia
r = params.get('r', 0.25) # m, thrust offset
Expand All @@ -1526,7 +1527,7 @@ def pvtol(t, x, u, params={}):


def pvtol_full(t, x, u, params={}):
from math import sin, cos
from math import cos, sin
m = params.get('m', 4.) # kg, system mass
J = params.get('J', 0.0475) # kg m^2, system inertia
r = params.get('r', 0.25) # m, thrust offset
Expand Down Expand Up @@ -1579,8 +1580,12 @@ def test_interconnect_unused_input():
inputs=['r'],
outputs=['y'])

with pytest.warns(None) as record:
with warnings.catch_warnings():
# no warning if output explicitly ignored, various argument forms
warnings.simplefilter("error")
# strip out matrix warnings
warnings.filterwarnings("ignore", "the matrix subclass",
category=PendingDeprecationWarning)
h = ct.interconnect([g,s,k],
inputs=['r'],
outputs=['y'],
Expand All @@ -1595,14 +1600,6 @@ def test_interconnect_unused_input():
h = ct.interconnect([g,s,k],
connections=False)

#https://docs.pytest.org/en/6.2.x/warnings.html#recwarn
for r in record:
# strip out matrix warnings
if re.match(r'.*matrix subclass', str(r.message)):
continue
print(r.message)
pytest.fail(f'Unexpected warning: {r.message}')


# warn if explicity ignored input in fact used
with pytest.warns(
Expand Down Expand Up @@ -1657,7 +1654,11 @@ def test_interconnect_unused_output():


# no warning if output explicitly ignored
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
# strip out matrix warnings
warnings.filterwarnings("ignore", "the matrix subclass",
category=PendingDeprecationWarning)
h = ct.interconnect([g,s,k],
inputs=['r'],
outputs=['y'],
Expand All @@ -1672,14 +1673,6 @@ def test_interconnect_unused_output():
h = ct.interconnect([g,s,k],
connections=False)

#https://docs.pytest.org/en/6.2.x/warnings.html#recwarn
for r in record:
# strip out matrix warnings
if re.match(r'.*matrix subclass', str(r.message)):
continue
print(r.message)
pytest.fail(f'Unexpected warning: {r.message}')

# warn if explicity ignored output in fact used
with pytest.warns(
UserWarning,
Expand Down
8 changes: 6 additions & 2 deletions control/tests/namedio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import re
from copy import copy
import warnings

import numpy as np
import control as ct
Expand Down Expand Up @@ -243,9 +244,12 @@ def test_duplicate_sysname():
sys = ct.rss(4, 1, 1)

# No warnings should be generated if we reuse an an unnamed system
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
# strip out matrix warnings
warnings.filterwarnings("ignore", "the matrix subclass",
category=PendingDeprecationWarning)
res = sys * sys
assert not any([type(msg) == UserWarning for msg in record])

# Generate a warning if the system is named
sys = ct.rss(4, 1, 1, name='sys')
Expand Down
7 changes: 5 additions & 2 deletions control/tests/nyquist_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

"""

import warnings

import pytest
import numpy as np
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -225,10 +227,11 @@ def test_nyquist_encirclements():
sys = 1 / (s**2 + s + 1)
with pytest.warns(UserWarning, match="encirclements was a non-integer"):
count = ct.nyquist_plot(sys, omega_limits=[0.5, 1e3])
with pytest.warns(None) as records:
with warnings.catch_warnings():
warnings.simplefilter("error")
# strip out matrix warnings
count = ct.nyquist_plot(
sys, omega_limits=[0.5, 1e3], encirclement_threshold=0.2)
assert len(records) == 0
plt.title("Non-integer number of encirclements [%g]" % count)


Expand Down