|
2 | 2 |
|
3 | 3 | import annotationlib
|
4 | 4 | import functools
|
| 5 | +import itertools |
5 | 6 | import pickle
|
6 | 7 | import unittest
|
| 8 | +from annotationlib import Format, get_annotations, get_annotate_function |
7 | 9 | from typing import Unpack
|
8 | 10 |
|
9 | 11 | from test.test_inspect import inspect_stock_annotations
|
@@ -767,5 +769,85 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
|
767 | 769 |
|
768 | 770 | self.assertEqual(
|
769 | 771 | set(results.generic_func_annotations.values()),
|
770 |
| - set(results.generic_func.__type_params__) |
| 772 | + set(results.generic_func.__type_params__), |
771 | 773 | )
|
| 774 | + |
| 775 | + |
| 776 | +class MetaclassTests(unittest.TestCase): |
| 777 | + def test_annotated_meta(self): |
| 778 | + class Meta(type): |
| 779 | + a: int |
| 780 | + |
| 781 | + class X(metaclass=Meta): |
| 782 | + pass |
| 783 | + |
| 784 | + class Y(metaclass=Meta): |
| 785 | + b: float |
| 786 | + |
| 787 | + self.assertEqual(get_annotations(Meta), {"a": int}) |
| 788 | + self.assertEqual(get_annotate_function(Meta)(Format.VALUE), {"a": int}) |
| 789 | + |
| 790 | + self.assertEqual(get_annotations(X), {}) |
| 791 | + self.assertIs(get_annotate_function(X), None) |
| 792 | + |
| 793 | + self.assertEqual(get_annotations(Y), {"b": float}) |
| 794 | + self.assertEqual(get_annotate_function(Y)(Format.VALUE), {"b": float}) |
| 795 | + |
| 796 | + def test_unannotated_meta(self): |
| 797 | + class Meta(type): pass |
| 798 | + |
| 799 | + class X(metaclass=Meta): |
| 800 | + a: str |
| 801 | + |
| 802 | + class Y(X): pass |
| 803 | + |
| 804 | + self.assertEqual(get_annotations(Meta), {}) |
| 805 | + self.assertIs(get_annotate_function(Meta), None) |
| 806 | + |
| 807 | + self.assertEqual(get_annotations(Y), {}) |
| 808 | + self.assertIs(get_annotate_function(Y), None) |
| 809 | + |
| 810 | + self.assertEqual(get_annotations(X), {"a": str}) |
| 811 | + self.assertEqual(get_annotate_function(X)(Format.VALUE), {"a": str}) |
| 812 | + |
| 813 | + def test_ordering(self): |
| 814 | + # Based on a sample by David Ellis |
| 815 | + # https://discuss.python.org/t/pep-749-implementing-pep-649/54974/38 |
| 816 | + |
| 817 | + def make_classes(): |
| 818 | + class Meta(type): |
| 819 | + a: int |
| 820 | + expected_annotations = {"a": int} |
| 821 | + |
| 822 | + class A(type, metaclass=Meta): |
| 823 | + b: float |
| 824 | + expected_annotations = {"b": float} |
| 825 | + |
| 826 | + class B(metaclass=A): |
| 827 | + c: str |
| 828 | + expected_annotations = {"c": str} |
| 829 | + |
| 830 | + class C(B): |
| 831 | + expected_annotations = {} |
| 832 | + |
| 833 | + class D(metaclass=Meta): |
| 834 | + expected_annotations = {} |
| 835 | + |
| 836 | + return Meta, A, B, C, D |
| 837 | + |
| 838 | + classes = make_classes() |
| 839 | + class_count = len(classes) |
| 840 | + for order in itertools.permutations(range(class_count), class_count): |
| 841 | + names = ", ".join(classes[i].__name__ for i in order) |
| 842 | + with self.subTest(names=names): |
| 843 | + classes = make_classes() # Regenerate classes |
| 844 | + for i in order: |
| 845 | + get_annotations(classes[i]) |
| 846 | + for c in classes: |
| 847 | + with self.subTest(c=c): |
| 848 | + self.assertEqual(get_annotations(c), c.expected_annotations) |
| 849 | + annotate_func = get_annotate_function(c) |
| 850 | + if c.expected_annotations: |
| 851 | + self.assertEqual(annotate_func(Format.VALUE), c.expected_annotations) |
| 852 | + else: |
| 853 | + self.assertIs(annotate_func, None) |
0 commit comments