aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_warnings/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_warnings/__init__.py')
-rw-r--r--Lib/test/test_warnings/__init__.py70
1 files changed, 65 insertions, 5 deletions
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 03126cebe03..5c3b1250ceb 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -102,7 +102,7 @@ class PublicAPITests(BaseTest):
"""
def test_module_all_attribute(self):
- self.assertTrue(hasattr(self.module, '__all__'))
+ self.assertHasAttr(self.module, '__all__')
target_api = ["warn", "warn_explicit", "showwarning",
"formatwarning", "filterwarnings", "simplefilter",
"resetwarnings", "catch_warnings", "deprecated"]
@@ -735,7 +735,7 @@ class CWarnTests(WarnTests, unittest.TestCase):
# test.import_helper.import_fresh_module utility function
def test_accelerated(self):
self.assertIsNot(original_warnings, self.module)
- self.assertFalse(hasattr(self.module.warn, '__code__'))
+ self.assertNotHasAttr(self.module.warn, '__code__')
class PyWarnTests(WarnTests, unittest.TestCase):
module = py_warnings
@@ -744,7 +744,7 @@ class PyWarnTests(WarnTests, unittest.TestCase):
# test.import_helper.import_fresh_module utility function
def test_pure_python(self):
self.assertIsNot(original_warnings, self.module)
- self.assertTrue(hasattr(self.module.warn, '__code__'))
+ self.assertHasAttr(self.module.warn, '__code__')
class WCmdLineTests(BaseTest):
@@ -1528,12 +1528,12 @@ a=A()
# (_warnings will try to import it)
code = "f = open(%a)" % __file__
rc, out, err = assert_python_ok("-Wd", "-c", code)
- self.assertTrue(err.startswith(expected), ascii(err))
+ self.assertStartsWith(err, expected)
# import the warnings module
code = "import warnings; f = open(%a)" % __file__
rc, out, err = assert_python_ok("-Wd", "-c", code)
- self.assertTrue(err.startswith(expected), ascii(err))
+ self.assertStartsWith(err, expected)
class AsyncTests(BaseTest):
@@ -2018,10 +2018,70 @@ class DeprecatedTests(PyPublicAPITests):
self.assertFalse(inspect.iscoroutinefunction(Cls.sync))
self.assertTrue(inspect.iscoroutinefunction(Cls.coro))
+ def test_inspect_class_signature(self):
+ class Cls1: # no __init__ or __new__
+ pass
+
+ class Cls2: # __new__ only
+ def __new__(cls, x, y):
+ return super().__new__(cls)
+
+ class Cls3: # __init__ only
+ def __init__(self, x, y):
+ pass
+
+ class Cls4: # __new__ and __init__
+ def __new__(cls, x, y):
+ return super().__new__(cls)
+
+ def __init__(self, x, y):
+ pass
+
+ class Cls5(Cls1): # inherits no __init__ or __new__
+ pass
+
+ class Cls6(Cls2): # inherits __new__ only
+ pass
+
+ class Cls7(Cls3): # inherits __init__ only
+ pass
+
+ class Cls8(Cls4): # inherits __new__ and __init__
+ pass
+
+ # The `@deprecated` decorator will update the class in-place.
+ # Test the child classes first.
+ for cls in reversed((Cls1, Cls2, Cls3, Cls4, Cls5, Cls6, Cls7, Cls8)):
+ with self.subTest(f'class {cls.__name__} signature'):
+ try:
+ original_signature = inspect.signature(cls)
+ except ValueError:
+ original_signature = None
+ try:
+ original_new_signature = inspect.signature(cls.__new__)
+ except ValueError:
+ original_new_signature = None
+
+ deprecated_cls = deprecated("depr")(cls)
+
+ try:
+ deprecated_signature = inspect.signature(deprecated_cls)
+ except ValueError:
+ deprecated_signature = None
+ self.assertEqual(original_signature, deprecated_signature)
+
+ try:
+ deprecated_new_signature = inspect.signature(deprecated_cls.__new__)
+ except ValueError:
+ deprecated_new_signature = None
+ self.assertEqual(original_new_signature, deprecated_new_signature)
+
+
def setUpModule():
py_warnings.onceregistry.clear()
c_warnings.onceregistry.clear()
+
tearDownModule = setUpModule
if __name__ == "__main__":