Skip to content

Commit 21ef29e

Browse files
committed
add DefaultDict for deprecation handling
1 parent db174b7 commit 21ef29e

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

control/config.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,43 @@
2020
'control.squeeze_time_response': None,
2121
'forced_response.return_x': False,
2222
}
23-
defaults = dict(_control_defaults)
23+
24+
25+
class DefaultDict(dict):
26+
def __init__(self, *args, **kwargs):
27+
super().__init__(*args, **kwargs)
28+
29+
def __getitem__(self, key):
30+
return super().__getitem__(self._check_deprecation(key))
31+
32+
def __setitem__(self, key, value):
33+
super().__setitem__(self._check_deprecation(key), value)
34+
35+
def __missing__(self, key):
36+
repl = self._check_deprecation(key)
37+
if self.__contains__(repl):
38+
return self[repl]
39+
else:
40+
raise KeyError
41+
42+
def copy(self):
43+
return DefaultDict(self)
44+
45+
def get(self, key, default=None):
46+
return super().get(self._check_deprecation(key), default)
47+
48+
def _check_deprecation(self, key):
49+
if self.__contains__(f"deprecated.{key}"):
50+
repl = self[f"deprecated.{key}"]
51+
warnings.warn(f"config.defaults['{key}'] has been renamed to "
52+
f"config.defaults['{repl}'].",
53+
DeprecationWarning)
54+
return repl
55+
else:
56+
return key
57+
58+
59+
defaults = DefaultDict(_control_defaults)
2460

2561

2662
def set_defaults(module, **keywords):

control/freqplot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@
6868
'freqplot.Hz': False, # Plot frequency in Hertz
6969
'freqplot.grid': True, # Turn on grid for gain and phase
7070
'freqplot.wrap_phase': False, # Wrap the phase plot at a given value
71+
72+
# deprecations
73+
'deprecated.bode.dB': 'freqplot.dB',
74+
'deprecated.bode.deg': 'freqplot.deg',
75+
'deprecated.bode.Hz': 'freqplot.Hz',
76+
'deprecated.bode.grid': 'freqplot.grid',
77+
'deprecated.bode.wrap_phase': 'freqplot.wrap_phase',
7178
}
7279

80+
7381
#
7482
# Main plotting functions
7583
#

control/tests/config_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,43 @@ def test_get_param(self):
4949

5050
assert ct.config._get_param('config', 'test4', {'test4': 1}, None) == 1
5151

52+
def test_default_deprecation(self):
53+
ct.config.defaults['config.newkey'] = 1
54+
ct.config.defaults['deprecated.config.oldkey'] = 'config.newkey'
55+
ct.config.defaults['deprecated.config.oldmiss'] = 'config.newmiss'
56+
57+
msgpattern = r'config\.oldkey.* has been renamed to .*config\.newkey'
58+
59+
with pytest.warns(DeprecationWarning, match=msgpattern):
60+
assert ct.config.defaults['config.oldkey'] == 1
61+
with pytest.warns(DeprecationWarning, match=msgpattern):
62+
ct.config.defaults['config.oldkey'] = 2
63+
with pytest.warns(DeprecationWarning, match=msgpattern):
64+
assert ct.config.defaults['config.oldkey'] == 2
65+
assert ct.config.defaults['config.newkey'] == 2
66+
67+
ct.config.set_defaults('config', newkey=3)
68+
with pytest.warns(DeprecationWarning, match=msgpattern):
69+
assert ct.config._get_param('config', 'oldkey') == 3
70+
with pytest.warns(DeprecationWarning, match=msgpattern):
71+
ct.config.set_defaults('config', oldkey=4)
72+
with pytest.warns(DeprecationWarning, match=msgpattern):
73+
assert ct.config.defaults['config.oldkey'] == 4
74+
assert ct.config.defaults['config.newkey'] == 4
75+
76+
with pytest.raises(KeyError):
77+
with pytest.warns(DeprecationWarning, match=msgpattern):
78+
ct.config.defaults['config.oldmiss']
79+
with pytest.raises(KeyError):
80+
ct.config.defaults['config.neverdefined']
81+
82+
# assert that reset defaults keeps the custom type
83+
ct.config.reset_defaults()
84+
with pytest.warns(DeprecationWarning,
85+
match='bode.* has been renamed to.*freqplot'):
86+
assert ct.config.defaults['bode.Hz'] \
87+
== ct.config.defaults['freqplot.Hz']
88+
5289
@mplcleanup
5390
def test_fbs_bode(self):
5491
ct.use_fbs_defaults()

0 commit comments

Comments
 (0)