|
23 | 23 | from typing import assert_type, cast, runtime_checkable
|
24 | 24 | from typing import get_type_hints
|
25 | 25 | from typing import get_origin, get_args
|
26 |
| -from typing import override |
| 26 | +from typing import override, deprecated |
27 | 27 | from typing import is_typeddict
|
28 | 28 | from typing import reveal_type
|
29 | 29 | from typing import dataclass_transform
|
@@ -4699,6 +4699,152 @@ def on_bottom(self, a: int) -> int:
|
4699 | 4699 | self.assertTrue(instance.on_bottom.__override__)
|
4700 | 4700 |
|
4701 | 4701 |
|
| 4702 | +class DeprecatedTests(BaseTestCase): |
| 4703 | + def test_dunder_deprecated(self): |
| 4704 | + @deprecated("A will go away soon") |
| 4705 | + class A: |
| 4706 | + pass |
| 4707 | + |
| 4708 | + self.assertEqual(A.__deprecated__, "A will go away soon") |
| 4709 | + self.assertIsInstance(A, type) |
| 4710 | + |
| 4711 | + @deprecated("b will go away soon") |
| 4712 | + def b(): |
| 4713 | + pass |
| 4714 | + |
| 4715 | + self.assertEqual(b.__deprecated__, "b will go away soon") |
| 4716 | + self.assertIsInstance(b, types.FunctionType) |
| 4717 | + |
| 4718 | + @overload |
| 4719 | + @deprecated("no more ints") |
| 4720 | + def h(x: int) -> int: ... |
| 4721 | + @overload |
| 4722 | + def h(x: str) -> str: ... |
| 4723 | + def h(x): |
| 4724 | + return x |
| 4725 | + |
| 4726 | + overloads = get_overloads(h) |
| 4727 | + self.assertEqual(len(overloads), 2) |
| 4728 | + self.assertEqual(overloads[0].__deprecated__, "no more ints") |
| 4729 | + |
| 4730 | + def test_class(self): |
| 4731 | + @deprecated("A will go away soon") |
| 4732 | + class A: |
| 4733 | + pass |
| 4734 | + |
| 4735 | + with self.assertWarnsRegex(DeprecationWarning, "A will go away soon"): |
| 4736 | + A() |
| 4737 | + with self.assertRaises(TypeError), self.assertWarnsRegex(DeprecationWarning, "A will go away soon"): |
| 4738 | + A(42) |
| 4739 | + |
| 4740 | + @deprecated("HasInit will go away soon") |
| 4741 | + class HasInit: |
| 4742 | + def __init__(self, x): |
| 4743 | + self.x = x |
| 4744 | + |
| 4745 | + with self.assertWarnsRegex(DeprecationWarning, "HasInit will go away soon"): |
| 4746 | + instance = HasInit(42) |
| 4747 | + self.assertEqual(instance.x, 42) |
| 4748 | + |
| 4749 | + has_new_called = False |
| 4750 | + |
| 4751 | + @deprecated("HasNew will go away soon") |
| 4752 | + class HasNew: |
| 4753 | + def __new__(cls, x): |
| 4754 | + nonlocal has_new_called |
| 4755 | + has_new_called = True |
| 4756 | + return super().__new__(cls) |
| 4757 | + |
| 4758 | + def __init__(self, x) -> None: |
| 4759 | + self.x = x |
| 4760 | + |
| 4761 | + with self.assertWarnsRegex(DeprecationWarning, "HasNew will go away soon"): |
| 4762 | + instance = HasNew(42) |
| 4763 | + self.assertEqual(instance.x, 42) |
| 4764 | + self.assertTrue(has_new_called) |
| 4765 | + new_base_called = False |
| 4766 | + |
| 4767 | + class NewBase: |
| 4768 | + def __new__(cls, x): |
| 4769 | + nonlocal new_base_called |
| 4770 | + new_base_called = True |
| 4771 | + return super().__new__(cls) |
| 4772 | + |
| 4773 | + def __init__(self, x) -> None: |
| 4774 | + self.x = x |
| 4775 | + |
| 4776 | + @deprecated("HasInheritedNew will go away soon") |
| 4777 | + class HasInheritedNew(NewBase): |
| 4778 | + pass |
| 4779 | + |
| 4780 | + with self.assertWarnsRegex(DeprecationWarning, "HasInheritedNew will go away soon"): |
| 4781 | + instance = HasInheritedNew(42) |
| 4782 | + self.assertEqual(instance.x, 42) |
| 4783 | + self.assertTrue(new_base_called) |
| 4784 | + |
| 4785 | + def test_function(self): |
| 4786 | + @deprecated("b will go away soon") |
| 4787 | + def b(): |
| 4788 | + pass |
| 4789 | + |
| 4790 | + with self.assertWarnsRegex(DeprecationWarning, "b will go away soon"): |
| 4791 | + b() |
| 4792 | + |
| 4793 | + def test_method(self): |
| 4794 | + class Capybara: |
| 4795 | + @deprecated("x will go away soon") |
| 4796 | + def x(self): |
| 4797 | + pass |
| 4798 | + |
| 4799 | + instance = Capybara() |
| 4800 | + with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"): |
| 4801 | + instance.x() |
| 4802 | + |
| 4803 | + def test_property(self): |
| 4804 | + class Capybara: |
| 4805 | + @property |
| 4806 | + @deprecated("x will go away soon") |
| 4807 | + def x(self): |
| 4808 | + pass |
| 4809 | + |
| 4810 | + @property |
| 4811 | + def no_more_setting(self): |
| 4812 | + return 42 |
| 4813 | + |
| 4814 | + @no_more_setting.setter |
| 4815 | + @deprecated("no more setting") |
| 4816 | + def no_more_setting(self, value): |
| 4817 | + pass |
| 4818 | + |
| 4819 | + instance = Capybara() |
| 4820 | + with self.assertWarnsRegex(DeprecationWarning, "x will go away soon"): |
| 4821 | + instance.x |
| 4822 | + |
| 4823 | + with warnings.catch_warnings(): |
| 4824 | + warnings.simplefilter("error") |
| 4825 | + self.assertEqual(instance.no_more_setting, 42) |
| 4826 | + |
| 4827 | + with self.assertWarnsRegex(DeprecationWarning, "no more setting"): |
| 4828 | + instance.no_more_setting = 42 |
| 4829 | + |
| 4830 | + def test_category(self): |
| 4831 | + @deprecated("c will go away soon", category=RuntimeWarning) |
| 4832 | + def c(): |
| 4833 | + pass |
| 4834 | + |
| 4835 | + with self.assertWarnsRegex(RuntimeWarning, "c will go away soon"): |
| 4836 | + c() |
| 4837 | + |
| 4838 | + def test_turn_off_warnings(self): |
| 4839 | + @deprecated("d will go away soon", category=None) |
| 4840 | + def d(): |
| 4841 | + pass |
| 4842 | + |
| 4843 | + with warnings.catch_warnings(): |
| 4844 | + warnings.simplefilter("error") |
| 4845 | + d() |
| 4846 | + |
| 4847 | + |
4702 | 4848 | class CastTests(BaseTestCase):
|
4703 | 4849 |
|
4704 | 4850 | def test_basics(self):
|
|
0 commit comments