8000 add DefaultDict for deprecation handling · python-control/python-control@0ccf0a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ccf0a6

Browse files
committed
add DefaultDict for deprecation handling
1 parent db174b7 commit 0ccf0a6

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,36 @@ 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+
56+
msgpattern = r'config\.oldkey.* has been renamed to .*config\.newkey'
57+
58+
with pytest.warns(DeprecationWarning, match=msgpattern):
59+
assert ct.config.defaults['config.oldkey'] == 1
60+
with pytest.warns(DeprecationWarning, match=msgpattern):
61+
ct.config.defaults['config.oldkey'] = 2
62+
with pytest.warns(DeprecationWarning, match=msgpattern):
63+
assert ct.config.defaults['config.oldkey'] == 2
64+
assert ct.config.defaults['config.newkey'] == 2
65+
66+
ct.config.set_defaults('config', newkey=3)
67+
with pytest.warns(DeprecationWarning, match=msgpattern):
68+
assert ct.config._get_param('config', 'oldkey') == 3
69+
with pytest.warns(DeprecationWarning, match=msgpattern):
70+
ct.config.set_defaults('config', oldkey=4)
71+
with pytest.warns(DeprecationWarning, match=msgpattern):
72+
assert ct.config.defaults['config.oldkey'] == 4
73+
assert ct.config.defaults['config.newkey'] == 4
74+
75+
# assert that reset defaults keeps the custom type
76+
ct.config.reset_defaults()
77+
with pytest.warns(DeprecationWarning,
78+
match='bode.* has been renamed to.*freqplot'):
79+
assert ct.config.defaults['bode.Hz'] \
80+
== ct.config.defaults['freqplot.Hz']
81+
5282
@mplcleanup
5383
def test_fbs_bode(self):
5484
ct.use_fbs_defaults()

0 commit comments

Comments
 (0)
0