|
| 1 | +import re |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from sphinxcontrib.prettyspecialmethods import SPECIAL_METHODS |
| 6 | + |
| 7 | +# from https://raw.githubusercontent.com/python/cpython/3.8/Doc/reference/datamodel.rst |
| 8 | +all_methods = { |
| 9 | + "__abs__", |
| 10 | + "__add__", |
| 11 | + "__aenter__", |
| 12 | + "__aexit__", |
| 13 | + "__aiter__", |
| 14 | + "__and__", |
| 15 | + "__anext__", |
| 16 | + "__await__", |
| 17 | + "__bool__", |
| 18 | + "__bytes__", |
| 19 | + "__call__", |
| 20 | + "__ceil__", |
| 21 | + "__class_getitem__", |
| 22 | + "__complex__", |
| 23 | + "__contains__", |
| 24 | + "__del__", |
| 25 | + "__delattr__", |
| 26 | + "__delete__", |
| 27 | + "__delitem__", |
| 28 | + "__dict__", |
| 29 | + "__dir__", |
| 30 | + "__divmod__", |
| 31 | + "__enter__", |
| 32 | + "__eq__", |
| 33 | + "__exit__", |
| 34 | + "__float__", |
| 35 | + "__floor__", |
| 36 | + "__floordiv__", |
| 37 | + "__format__", |
| 38 | + "__ge__", |
| 39 | + "__get__", |
| 40 | + "__getattr__", |
| 41 | + "__getattribute__", |
| 42 | + "__getitem__", |
| 43 | + "__gt__", |
| 44 | + "__hash__", |
| 45 | + "__iadd__", |
| 46 | + "__iand__", |
| 47 | + "__ifloordiv__", |
| 48 | + "__ilshift__", |
| 49 | + "__imatmul__", |
| 50 | + "__imod__", |
| 51 | + "__imul__", |
| 52 | + "__index__", |
| 53 | + "__init__", |
| 54 | + "__init_subclass__", |
| 55 | + "__int__", |
| 56 | + "__invert__", |
| 57 | + "__ior__", |
| 58 | + "__ipow__", |
| 59 | + "__irshift__", |
| 60 | + "__isub__", |
| 61 | + "__iter__", |
| 62 | + "__itruediv__", |
| 63 | + "__ixor__", |
| 64 | + "__le__", |
| 65 | + "__len__", |
| 66 | + "__length_hint__", |
| 67 | + "__lshift__", |
| 68 | + "__lt__", |
| 69 | + "__matmul__", |
| 70 | + "__missing__", |
| 71 | + "__mod__", |
| 72 | + "__mul__", |
| 73 | + "__ne__", |
| 74 | + "__neg__", |
| 75 | + "__new__", |
| 76 | + "__or__", |
| 77 | + "__pos__", |
| 78 | + "__pow__", |
| 79 | + "__radd__", |
| 80 | + "__rand__", |
| 81 | + "__rdivmod__", |
| 82 | + "__repr__", |
| 83 | + "__reversed__", |
| 84 | + "__rfloordiv__", |
| 85 | + "__rlshift__", |
| 86 | + "__rmatmul__", |
| 87 | + "__rmod__", |
| 88 | + "__rmul__", |
| 89 | + "__ror__", |
| 90 | + "__round__", |
| 91 | + "__rpow__", |
| 92 | + "__rrshift__", |
| 93 | + "__rshift__", |
| 94 | + "__rsub__", |
| 95 | + "__rtruediv__", |
| 96 | + "__rxor__", |
| 97 | + "__set__", |
| 98 | + "__set_name__", |
| 99 | + "__setattr__", |
| 100 | + "__setitem__", |
| 101 | + "__slots__", |
| 102 | + "__str__", |
| 103 | + "__sub__", |
| 104 | + "__truediv__", |
| 105 | + "__trunc__", |
| 106 | + "__xor__", |
| 107 | +} |
| 108 | + |
| 109 | +# not listed in the page above |
| 110 | +all_methods |= {'__sizeof__'} |
| 111 | + |
| 112 | + |
| 113 | +unimplemented_ops = dict( |
| 114 | + # not implemented |
| 115 | + inplace_ops={re.sub('^__', '__i', m) for m in all_methods} & all_methods, |
| 116 | + |
| 117 | + # typically redundant |
| 118 | + reverse_ops={re.sub('^__', '__r', m) for m in all_methods} & all_methods, |
| 119 | + |
| 120 | + # diffult to show clearly |
| 121 | + descriptor_ops={'__get__', '__set__', '__delete__', '__set_name__'}, |
| 122 | + |
| 123 | + # unclear whether to apply the formatting to enter or exit |
| 124 | + with_ops={'__enter__', '__exit__'}, |
| 125 | + async_with_ops={'__aenter__', '__aexit__'}, |
| 126 | + |
| 127 | + # no aiter or anext builtin |
| 128 | + async_iter_ops={'__aiter__', '__anext__'}, |
| 129 | + |
| 130 | + # difficult to represent |
| 131 | + other_ops={ |
| 132 | + '__await__', |
| 133 | + '__class_getitem__', |
| 134 | + '__del__', |
| 135 | + '__dict__', |
| 136 | + '__getattribute__', |
| 137 | + '__init__', |
| 138 | + '__init_subclass__', |
| 139 | + '__missing__', |
| 140 | + '__new__', |
| 141 | + '__slots__', |
| 142 | + }, |
| 143 | +) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.parametrize("unimplemented", [ |
| 147 | + pytest.param(v, id=k) for k, v in unimplemented_ops.items() |
| 148 | +]) |
| 149 | +def test_not_implemented(unimplemented): |
| 150 | + """ Test that the sets above are accurate """ |
| 151 | + assert all_methods & unimplemented == unimplemented |
| 152 | + assert SPECIAL_METHODS.keys() & unimplemented == set() |
| 153 | + |
| 154 | + |
| 155 | +def test_lists_exhaustive(): |
| 156 | + implemented = all_methods - set.union(*unimplemented_ops.values()) |
| 157 | + assert set(SPECIAL_METHODS.keys()) == implemented |
0 commit comments