|
1 | 1 | """Unit tests for zero-argument super() & related machinery.""" |
2 | 2 |
|
3 | 3 | import unittest |
4 | | -import warnings |
5 | | -from test.support import check_warnings |
6 | 4 |
|
7 | 5 |
|
8 | 6 | class A: |
@@ -173,14 +171,10 @@ def __new__(cls, name, bases, namespace): |
173 | 171 | test_namespace = namespace |
174 | 172 | return None |
175 | 173 |
|
176 | | - # This case shouldn't trigger the __classcell__ deprecation warning |
177 | | - with check_warnings() as w: |
178 | | - warnings.simplefilter("always", DeprecationWarning) |
179 | | - class A(metaclass=Meta): |
180 | | - @staticmethod |
181 | | - def f(): |
182 | | - return __class__ |
183 | | - self.assertEqual(w.warnings, []) |
| 174 | + class A(metaclass=Meta): |
| 175 | + @staticmethod |
| 176 | + def f(): |
| 177 | + return __class__ |
184 | 178 |
|
185 | 179 | self.assertIs(A, None) |
186 | 180 |
|
@@ -244,37 +238,19 @@ def __new__(cls, name, bases, namespace): |
244 | 238 | namespace.pop('__classcell__', None) |
245 | 239 | return super().__new__(cls, name, bases, namespace) |
246 | 240 |
|
247 | | - # The default case should continue to work without any warnings |
248 | | - with check_warnings() as w: |
249 | | - warnings.simplefilter("always", DeprecationWarning) |
250 | | - class WithoutClassRef(metaclass=Meta): |
251 | | - pass |
252 | | - self.assertEqual(w.warnings, []) |
| 241 | + # The default case should continue to work without any errors |
| 242 | + class WithoutClassRef(metaclass=Meta): |
| 243 | + pass |
253 | 244 |
|
254 | 245 | # With zero-arg super() or an explicit __class__ reference, we expect |
255 | | - # __build_class__ to emit a DeprecationWarning complaining that |
| 246 | + # __build_class__ to raise a RuntimeError complaining that |
256 | 247 | # __class__ was not set, and asking if __classcell__ was propagated |
257 | 248 | # to type.__new__. |
258 | | - # In Python 3.7, that warning will become a RuntimeError. |
259 | | - expected_warning = ( |
260 | | - '__class__ not set.*__classcell__ propagated', |
261 | | - DeprecationWarning |
262 | | - ) |
263 | | - with check_warnings(expected_warning): |
264 | | - warnings.simplefilter("always", DeprecationWarning) |
| 249 | + expected_error = '__class__ not set.*__classcell__ propagated' |
| 250 | + with self.assertRaisesRegex(RuntimeError, expected_error): |
265 | 251 | class WithClassRef(metaclass=Meta): |
266 | 252 | def f(self): |
267 | 253 | return __class__ |
268 | | - # Check __class__ still gets set despite the warning |
269 | | - self.assertIs(WithClassRef().f(), WithClassRef) |
270 | | - |
271 | | - # Check the warning is turned into an error as expected |
272 | | - with warnings.catch_warnings(): |
273 | | - warnings.simplefilter("error", DeprecationWarning) |
274 | | - with self.assertRaises(DeprecationWarning): |
275 | | - class WithClassRef(metaclass=Meta): |
276 | | - def f(self): |
277 | | - return __class__ |
278 | 254 |
|
279 | 255 | def test___classcell___overwrite(self): |
280 | 256 | # See issue #23722 |
|
0 commit comments