3
3
import unittest
4
4
import warnings
5
5
import importlib
6
+ import contextlib
6
7
7
8
from . import fixtures
8
9
from importlib .metadata import (
17
18
)
18
19
19
20
21
+ @contextlib .contextmanager
22
+ def suppress_known_deprecation ():
23
+ with warnings .catch_warnings (record = True ) as ctx :
24
+ warnings .simplefilter ('default' )
25
+ yield ctx
26
+
27
+
20
28
class APITests (
21
29
fixtures .EggInfoPkg ,
22
30
fixtures .DistInfoPkg ,
@@ -118,8 +126,7 @@ def test_entry_points_dict_construction(self):
118
126
# Prior versions of entry_points() returned simple lists and
119
127
# allowed casting those lists into maps by name using ``dict()``.
120
128
# Capture this now deprecated use-case.
121
- with warnings .catch_warnings (record = True ) as caught :
122
- warnings .filterwarnings ("default" , category = DeprecationWarning )
129
+ with suppress_known_deprecation () as caught :
123
130
eps = dict (entry_points (group = 'entries' ))
124
131
125
132
assert 'main' in eps
@@ -138,8 +145,7 @@ def test_entry_points_by_index(self):
138
145
See python/importlib_metadata#300 and bpo-44246.
139
146
"""
140
147
eps = distribution ('distinfo-pkg' ).entry_points
141
- with warnings .catch_warnings (record = True ) as caught :
142
- warnings .filterwarnings ("default" , category = DeprecationWarning )
148
+ with suppress_known_deprecation () as caught :
143
149
eps [0 ]
144
150
145
151
# check warning
@@ -151,7 +157,7 @@ def test_entry_points_groups_getitem(self):
151
157
# Prior versions of entry_points() returned a dict. Ensure
152
158
# that callers using '.__getitem__()' are supported but warned to
153
159
# migrate.
154
- with warnings . catch_warnings ( record = True ):
160
+ with suppress_known_deprecation ( ):
155
161
entry_points ()['entries' ] == entry_points (group = 'entries' )
156
162
157
163
with self .assertRaises (KeyError ):
@@ -161,7 +167,7 @@ def test_entry_points_groups_get(self):
161
167
# Prior versions of entry_points() returned a dict. Ensure
162
168
# that callers using '.get()' are supported but warned to
163
169
# migrate.
164
- with warnings . catch_warnings ( record = True ):
170
+ with suppress_known_deprecation ( ):
165
171
entry_points ().get ('missing' , 'default' ) == 'default'
166
172
entry_points ().get ('entries' , 'default' ) == entry_points ()['entries' ]
167
173
entry_points ().get ('missing' , ()) == ()
0 commit comments