|
22 | 22 | import types
|
23 | 23 | import tempfile
|
24 | 24 | import textwrap
|
| 25 | +from typing import Unpack |
25 | 26 | import unicodedata
|
26 | 27 | import unittest
|
27 | 28 | import unittest.mock
|
|
47 | 48 | from test.test_inspect import inspect_stock_annotations
|
48 | 49 | from test.test_inspect import inspect_stringized_annotations
|
49 | 50 | from test.test_inspect import inspect_stringized_annotations_2
|
| 51 | +from test.test_inspect import inspect_stringized_annotations_pep695 |
50 | 52 |
|
51 | 53 |
|
52 | 54 | # Functions tested in this suite:
|
@@ -1692,6 +1694,107 @@ def wrapper(a, b):
|
1692 | 1694 | self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations), {'x': 'mytype'})
|
1693 | 1695 | self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations, eval_str=True), {'x': int})
|
1694 | 1696 |
|
| 1697 | + def test_pep695_generic_class_with_future_annotations(self): |
| 1698 | + ann_module695 = inspect_stringized_annotations_pep695 |
| 1699 | + A_annotations = inspect.get_annotations(ann_module695.A, eval_str=True) |
| 1700 | + A_type_params = ann_module695.A.__type_params__ |
| 1701 | + self.assertIs(A_annotations["x"], A_type_params[0]) |
| 1702 | + self.assertEqual(A_annotations["y"].__args__[0], Unpack[A_type_params[1]]) |
| 1703 | + self.assertIs(A_annotations["z"].__args__[0], A_type_params[2]) |
| 1704 | + |
| 1705 | + def test_pep695_generic_class_with_future_annotations_and_local_shadowing(self): |
| 1706 | + B_annotations = inspect.get_annotations( |
| 1707 | + inspect_stringized_annotations_pep695.B, eval_str=True |
| 1708 | + ) |
| 1709 | + self.assertEqual(B_annotations, {"x": int, "y": str, "z": bytes}) |
| 1710 | + |
| 1711 | + def test_pep695_generic_class_with_future_annotations_name_clash_with_global_vars(self): |
| 1712 | + ann_module695 = inspect_stringized_annotations_pep695 |
| 1713 | + C_annotations = inspect.get_annotations(ann_module695.C, eval_str=True) |
| 1714 | + self.assertEqual( |
| 1715 | + set(C_annotations.values()), |
| 1716 | + set(ann_module695.C.__type_params__) |
| 1717 | + ) |
| 1718 | + |
| 1719 | + def test_pep_695_generic_function_with_future_annotations(self): |
| 1720 | + ann_module695 = inspect_stringized_annotations_pep695 |
| 1721 | + generic_func_annotations = inspect.get_annotations( |
| 1722 | + ann_module695.generic_function, eval_str=True |
| 1723 | + ) |
| 1724 | + func_t_params = ann_module695.generic_function.__type_params__ |
| 1725 | + self.assertEqual( |
| 1726 | + generic_func_annotations.keys(), {"x", "y", "z", "zz", "return"} |
| 1727 | + ) |
| 1728 | + self.assertIs(generic_func_annotations["x"], func_t_params[0]) |
| 1729 | + self.assertEqual(generic_func_annotations["y"], Unpack[func_t_params[1]]) |
| 1730 | + self.assertIs(generic_func_annotations["z"].__origin__, func_t_params[2]) |
| 1731 | + self.assertIs(generic_func_annotations["zz"].__origin__, func_t_params[2]) |
| 1732 | + |
| 1733 | + def test_pep_695_generic_function_with_future_annotations_name_clash_with_global_vars(self): |
| 1734 | + self.assertEqual( |
| 1735 | + set( |
| 1736 | + inspect.get_annotations( |
| 1737 | + inspect_stringized_annotations_pep695.generic_function_2, |
| 1738 | + eval_str=True |
| 1739 | + ).values() |
| 1740 | + ), |
| 1741 | + set( |
| 1742 | + inspect_stringized_annotations_pep695.generic_function_2.__type_params__ |
| 1743 | + ) |
| 1744 | + ) |
| 1745 | + |
| 1746 | + def test_pep_695_generic_method_with_future_annotations(self): |
| 1747 | + ann_module695 = inspect_stringized_annotations_pep695 |
| 1748 | + generic_method_annotations = inspect.get_annotations( |
| 1749 | + ann_module695.D.generic_method, eval_str=True |
| 1750 | + ) |
| 1751 | + params = { |
| 1752 | + param.__name__: param |
| 1753 | + for param in ann_module695.D.generic_method.__type_params__ |
| 1754 | + } |
| 1755 | + self.assertEqual( |
| 1756 | + generic_method_annotations, |
| 1757 | + {"x": params["Foo"], "y": params["Bar"], "return": None} |
| 1758 | + ) |
| 1759 | + |
| 1760 | + def test_pep_695_generic_method_with_future_annotations_name_clash_with_global_vars(self): |
| 1761 | + self.assertEqual( |
| 1762 | + set( |
| 1763 | + inspect.get_annotations( |
| 1764 | + inspect_stringized_annotations_pep695.D.generic_method_2, |
| 1765 | + eval_str=True |
| 1766 | + ).values() |
| 1767 | + ), |
| 1768 | + set( |
| 1769 | + inspect_stringized_annotations_pep695.D.generic_method_2.__type_params__ |
| 1770 | + ) |
| 1771 | + ) |
| 1772 | + |
| 1773 | + def test_pep_695_generics_with_future_annotations_nested_in_function(self): |
| 1774 | + results = inspect_stringized_annotations_pep695.nested() |
| 1775 | + |
| 1776 | + self.assertEqual( |
| 1777 | + set(results.E_annotations.values()), |
| 1778 | + set(results.E.__type_params__) |
| 1779 | + ) |
| 1780 | + self.assertEqual( |
| 1781 | + set(results.E_meth_annotations.values()), |
| 1782 | + set(results.E.generic_method.__type_params__) |
| 1783 | + ) |
| 1784 | + self.assertNotEqual( |
| 1785 | + set(results.E_meth_annotations.values()), |
| 1786 | + set(results.E.__type_params__) |
| 1787 | + ) |
| 1788 | + self.assertEqual( |
| 1789 | + set(results.E_meth_annotations.values()).intersection(results.E.__type_params__), |
| 1790 | + set() |
| 1791 | + ) |
| 1792 | + |
| 1793 | + self.assertEqual( |
| 1794 | + set(results.generic_func_annotations.values()), |
| 1795 | + set(results.generic_func.__type_params__) |
| 1796 | + ) |
| 1797 | + |
1695 | 1798 |
|
1696 | 1799 | class TestFormatAnnotation(unittest.TestCase):
|
1697 | 1800 | def test_typing_replacement(self):
|
|
0 commit comments