|
1 | | -from contextlib import contextmanager |
| 1 | +from contextlib import contextmanager, redirect_stdout |
2 | 2 | import linecache |
3 | 3 | import os |
4 | 4 | import importlib |
@@ -1821,6 +1821,50 @@ async def coro(self): |
1821 | 1821 | self.assertFalse(inspect.iscoroutinefunction(Cls.sync)) |
1822 | 1822 | self.assertTrue(inspect.iscoroutinefunction(Cls.coro)) |
1823 | 1823 |
|
| 1824 | + |
| 1825 | +class TestDeprecatedHelp(unittest.TestCase): |
| 1826 | + CODE_SIMPLE = r""" |
| 1827 | +from warnings import deprecated |
| 1828 | +@deprecated("Test") |
| 1829 | +class A: |
| 1830 | + pass |
| 1831 | +a = A() |
| 1832 | +""" |
| 1833 | + CODE_SUBCLASS = r""" |
| 1834 | +from warnings import deprecated |
| 1835 | +@deprecated("Test") |
| 1836 | +class A: |
| 1837 | + def __init_subclass__(self, **kwargs): |
| 1838 | + pass |
| 1839 | +class B(A): |
| 1840 | + pass |
| 1841 | +b = B() |
| 1842 | +""" |
| 1843 | + def setUp(self): |
| 1844 | + self.module = types.ModuleType("testmodule") |
| 1845 | + |
| 1846 | + def tearDown(self): |
| 1847 | + if "testmodule" in sys.modules: |
| 1848 | + del sys.modules["testmodule"] |
| 1849 | + |
| 1850 | + def _get_help_output(self, code): |
| 1851 | + with self.assertWarns(DeprecationWarning) as cm: |
| 1852 | + exec(code, self.module.__dict__) |
| 1853 | + sys.modules["testmodule"] = self.module |
| 1854 | + |
| 1855 | + f = StringIO() |
| 1856 | + with redirect_stdout(f): |
| 1857 | + help(self.module) |
| 1858 | + |
| 1859 | + self.assertEqual(str(cm.warning), "Test") |
| 1860 | + return f.getvalue() |
| 1861 | + |
| 1862 | + def test_help_output(self): |
| 1863 | + for code in (self.CODE_SIMPLE, self.CODE_SUBCLASS): |
| 1864 | + with self.subTest(code=code): |
| 1865 | + help_output = self._get_help_output(code) |
| 1866 | + self.assertIn("Help on module testmodule:", help_output) |
| 1867 | + |
1824 | 1868 | def setUpModule(): |
1825 | 1869 | py_warnings.onceregistry.clear() |
1826 | 1870 | c_warnings.onceregistry.clear() |
|
0 commit comments