Skip to content

Commit e0dab93

Browse files
committed
use collections.UserDict for DefaultDict
1 parent c37df52 commit e0dab93

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

control/config.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# files. For now, you can just choose between MATLAB and FBS default
88
# values + tweak a few other things.
99

10+
11+
import collections
1012
import warnings
1113

1214
__all__ = ['defaults', 'set_defaults', 'reset_defaults',
@@ -22,32 +24,35 @@
2224
}
2325

2426

25-
class DefaultDict(dict):
27+
class DefaultDict(collections.UserDict):
28+
"""Map names for settings from older version to their renamed ones.
29+
30+
If a user wants to write to an old setting, issue a warning and write to
31+
the renamed setting instead. Accessing the old setting returns the value
32+
from the new name.
33+
"""
34+
2635
def __init__(self, *args, **kwargs):
2736
super().__init__(*args, **kwargs)
2837

2938
def __setitem__(self, key, value):
3039
super().__setitem__(self._check_deprecation(key), value)
3140

3241
def __missing__(self, key):
42+
# An old key should never have been set. If it is being accessed
43+
# through __getitem__, return the value from the new name.
3344
repl = self._check_deprecation(key)
3445
if self.__contains__(repl):
3546
return self[repl]
3647
else:
37-
raise KeyError
38-
39-
def copy(self):
40-
return DefaultDict(self)
41-
42-
def get(self, key, default=None):
43-
return super().get(self._check_deprecation(key), default)
48+
raise KeyError(key)
4449

4550
def _check_deprecation(self, key):
4651
if self.__contains__(f"deprecated.{key}"):
4752
repl = self[f"deprecated.{key}"]
4853
warnings.warn(f"config.defaults['{key}'] has been renamed to "
4954
f"config.defaults['{repl}'].",
50-
DeprecationWarning)
55+
FutureWarning, stacklevel=3)
5156
return repl
5257
else:
5358
return key

control/tests/config_test.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,38 +50,44 @@ def test_get_param(self):
5050
assert ct.config._get_param('config', 'test4', {'test4': 1}, None) == 1
5151

5252
def test_default_deprecation(self):
53-
ct.config.defaults['config.newkey'] = 1
5453
ct.config.defaults['deprecated.config.oldkey'] = 'config.newkey'
5554
ct.config.defaults['deprecated.config.oldmiss'] = 'config.newmiss'
5655

5756
msgpattern = r'config\.oldkey.* has been renamed to .*config\.newkey'
5857

59-
with pytest.warns(DeprecationWarning, match=msgpattern):
58+
ct.config.defaults['config.newkey'] = 1
59+
with pytest.warns(FutureWarning, match=msgpattern):
6060
assert ct.config.defaults['config.oldkey'] == 1
61-
with pytest.warns(DeprecationWarning, match=msgpattern):
61+
with pytest.warns(FutureWarning, match=msgpattern):
6262
ct.config.defaults['config.oldkey'] = 2
63-
with pytest.warns(DeprecationWarning, match=msgpattern):
63+
with pytest.warns(FutureWarning, match=msgpattern):
6464
assert ct.config.defaults['config.oldkey'] == 2
6565
assert ct.config.defaults['config.newkey'] == 2
6666

6767
ct.config.set_defaults('config', newkey=3)
68-
with pytest.warns(DeprecationWarning, match=msgpattern):
68+
with pytest.warns(FutureWarning, match=msgpattern):
6969
assert ct.config._get_param('config', 'oldkey') == 3
70-
with pytest.warns(DeprecationWarning, match=msgpattern):
70+
with pytest.warns(FutureWarning, match=msgpattern):
7171
ct.config.set_defaults('config', oldkey=4)
72-
with pytest.warns(DeprecationWarning, match=msgpattern):
72+
with pytest.warns(FutureWarning, match=msgpattern):
7373
assert ct.config.defaults['config.oldkey'] == 4
7474
assert ct.config.defaults['config.newkey'] == 4
7575

76+
ct.config.defaults.update({'config.newkey': 5})
77+
with pytest.warns(FutureWarning, match=msgpattern):
78+
ct.config.defaults.update({'config.oldkey': 6})
79+
with pytest.warns(FutureWarning, match=msgpattern):
80+
assert ct.config.defaults.get('config.oldkey') == 6
81+
7682
with pytest.raises(KeyError):
77-
with pytest.warns(DeprecationWarning, match=msgpattern):
83+
with pytest.warns(FutureWarning, match=msgpattern):
7884
ct.config.defaults['config.oldmiss']
7985
with pytest.raises(KeyError):
8086
ct.config.defaults['config.neverdefined']
8187

8288
# assert that reset defaults keeps the custom type
8389
ct.config.reset_defaults()
84-
with pytest.warns(DeprecationWarning,
90+
with pytest.warns(FutureWarning,
8591
match='bode.* has been renamed to.*freqplot'):
8692
assert ct.config.defaults['bode.Hz'] \
8793
== ct.config.defaults['freqplot.Hz']

0 commit comments

Comments
 (0)