Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite0dab93

Browse files
committed
use collections.UserDict for DefaultDict
1 parentc37df52 commite0dab93

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+
importcollections
1012
importwarnings
1113

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

2426

25-
classDefaultDict(dict):
27+
classDefaultDict(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
ifself.__contains__(repl):
3546
returnself[repl]
3647
else:
37-
raiseKeyError
38-
39-
defcopy(self):
40-
returnDefaultDict(self)
41-
42-
defget(self,key,default=None):
43-
returnsuper().get(self._check_deprecation(key),default)
48+
raiseKeyError(key)
4449

4550
def_check_deprecation(self,key):
4651
ifself.__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
returnrepl
5257
else:
5358
returnkey

‎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
assertct.config._get_param('config','test4', {'test4':1},None)==1
5151

5252
deftest_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-
withpytest.warns(DeprecationWarning,match=msgpattern):
58+
ct.config.defaults['config.newkey']=1
59+
withpytest.warns(FutureWarning,match=msgpattern):
6060
assertct.config.defaults['config.oldkey']==1
61-
withpytest.warns(DeprecationWarning,match=msgpattern):
61+
withpytest.warns(FutureWarning,match=msgpattern):
6262
ct.config.defaults['config.oldkey']=2
63-
withpytest.warns(DeprecationWarning,match=msgpattern):
63+
withpytest.warns(FutureWarning,match=msgpattern):
6464
assertct.config.defaults['config.oldkey']==2
6565
assertct.config.defaults['config.newkey']==2
6666

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

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp