File: test_special_methods_access.py

package info (click to toggle)
anytree 2.12.1-3.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 872 kB
  • sloc: python: 4,044; makefile: 12
file content (367 lines) | stat: -rw-r--r-- 9,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
The methods of the `NodeMixin` class should not access the user class special methods.

For instance, the user define a `MyNode` class as bellow:

```python
from anytree import NodeMixin


class MyNode(NodeMixin):
    def __init__(self, name, parent=None, children=None):
        super(MyNode, self).__init__()
        self.name = name
        self.parent = parent
        if children:
            self.children = children
```

In this class, the used can implement some special methods, like ``__eq__`` or ``__len__``,
which can have a specific meaning not related to the Tree structure.
A good exemple could be a `NodeMixin` subclass which also implements `collections.abc.Mapping`:


```python
import collections

from anytree import NodeMixin


class MyMapping(NodeMixin, collections.abc.Mapping):
    def __init__(self, name, parent=None, children=None):
        super(MyMapping, self).__init__()
        self.name = name
        self.parent = parent
        if children:
            self.children = children

    def __iter__(self):
        for child in self.children:
            yield child
            for item in child:
                yield item

    def __len__(self):
        return len(list(iter(self)))

    def __getitem__(self, name):
        for child in self:
            if child.name == name:
                return child
        raise KeyError(name)
```

In this example, the `NodeMixin` class shouldn't make any call to `__iter__`, `__len__` or `__getitem__`.

To avoid that, the `NodeMixin` class should respect the following rules:

- Do not compare nodes by value but by reference.
  In other words, the `NodeMixin` class should not use `==` or `!=` but compare nodes with the `id()` function.
  Comparison could be done with the `is` or `is not` operator. Avoid using `in` or `not in`.

- Do not use truth value testing to check if a node is "empty".
  The `NodeMixin` class should not `if node`, `if not node`, `while node` nor `while not node`.
  Instead, it must compare the node with `None` like this: `if node is not None`, `if node is None`,
  `while node is not None` or `while node is None`.

- Do not presume that nodes are hashable.
  The `NodeMixin` class should not store nodes in `set` or `dict`.
  Instead, it can store node IDs using the `id()` function.
"""
import functools
import unittest

from anytree import NodeMixin

try:
    from collections.abc import Mapping
except ImportError:
    from collections import Mapping


# List of method names to which we want to control access:
#
# - This list does not contain the access control methods (`__getattribute__`, `__getattr__`,
#   `__setattr__`, and `__delattr__`) which are used by the NodeMixin class anyway.
# - This list does not contain `__new__`, `__init__`, and `__del__` which are required for testing.
# - Some methods are not available in all Python version: `__bytes__`, `__unicode__` and `__dir__`.

SPECIAL_METHODS = [
    # rich comparison methods
    "__lt__",
    "__le__",
    "__eq__",
    "__ne__",
    "__gt__",
    "__ge__",
    # repr/str/bytes
    "__repr__",
    "__str__",
    # "__bytes__",
    # "__unicode__",
    "__format__",
    # hashing
    "__hash__",
    # attribute listing
    # "__dir__",
    # pickle protocol
    "__getnewargs_ex__",
    "__getnewargs__",
    "__getstate__",
    "__setstate__",
    "__reduce_ex__",
    # object size
    "__sizeof__",
    # truth value testing
    "__bool__",
    # callable objects
    "__call__",
    # container types
    "__len__",
    "__length_hint__",
    "__getitem__",
    "__setitem__",
    "__delitem__",
    "__missing__",
    "__iter__",
    "__reversed__",
    "__contains__",
    # numeric types
    "__add__",
    "__sub__",
    "__mul__",
    "__matmul__",
    "__truediv__",
    "__floordiv__",
    "__mod__",
    "__divmod__",
    "__pow__",
    "__lshift__",
    "__rshift__",
    "__and__",
    "__xor__",
    "__or__",
    "__radd__",
    "__rsub__",
    "__rmul__",
    "__rmatmul__",
    "__rtruediv__",
    "__rfloordiv__",
    "__rmod__",
    "__rdivmod__",
    "__rpow__",
    "__rlshift__",
    "__rrshift__",
    "__rand__",
    "__rxor__",
    "__ror__",
    "__iadd__",
    "__isub__",
    "__imul__",
    "__imatmul__",
    "__itruediv__",
    "__ifloordiv__",
    "__imod__",
    "__ipow__",
    "__ilshift__",
    "__irshift__",
    "__iand__",
    "__ixor__",
    "__ior__",
    "__neg__",
    "__pos__",
    "__abs__",
    "__invert__",
    "__complex__",
    "__int__",
    "__float__",
    "__index__",
    "__round__",
    "__trunc__",
    "__floor__",
    "__ceil__",
    # With Statement Context Managers
    "__enter__",
    "__exit__",
]
SPECIAL_METHODS += [attr for attr in ["__bytes__", "__unicode__", "__dir"] if hasattr(object, attr)]


def prevent_access(attr, *args, **kwargs):
    raise AssertionError("invalid call to " + attr)


class MyNode(NodeMixin):
    @staticmethod
    def __new__(cls, *args, **kwargs):
        for attr in SPECIAL_METHODS:
            setattr(cls, attr, functools.partial(prevent_access, attr))
        instance = super(NodeMixin, cls).__new__(cls)
        return instance

    def __init__(self, name, parent=None, children=None):
        super(MyNode, self).__init__()
        self.name = name
        self.parent = parent
        if children:
            self.children = children


class TestConsistency(unittest.TestCase):
    """Control the access to special methods"""

    def setUp(self):
        super(TestConsistency, self).setUp()
        self.root1 = MyNode("root1")
        self.child1 = MyNode("child1", parent=self.root1)
        self.child2a = MyNode("child2a", parent=self.child1)
        self.child2b = MyNode("child2b", parent=self.child1)
        self.other = MyNode("other")

    def test_parent__root1(self):
        _ = self.root1.parent

    def test_parent__setter__root1(self):
        self.root1.parent = self.other

    def test_children__root1(self):
        _ = self.root1.children

    def test_children__setter__root1(self):
        self.root1.children = [self.other]

    def test_path__root1(self):
        _ = self.root1.path

    def test_iter_path_reverse__root1(self):
        for _ in self.root1.iter_path_reverse():
            pass

    def test_ancestors__root1(self):
        _ = self.root1.ancestors

    def test_descendants__root1(self):
        _ = self.root1.descendants

    def test_root__root1(self):
        _ = self.root1.root

    def test_siblings__root1(self):
        _ = self.root1.siblings

    def test_leaves__root1(self):
        _ = self.root1.leaves

    def test_is_leaf__root1(self):
        _ = self.root1.is_leaf

    def test_is_root__root1(self):
        _ = self.root1.is_root

    def test_height__root1(self):
        _ = self.root1.height

    def test_depth__root1(self):
        _ = self.root1.depth

    def test_parent__child2b(self):
        _ = self.child2b.parent

    def test_parent__setter__child2b(self):
        self.child2b.parent = self.other

    def test_children__child2b(self):
        _ = self.child2b.children

    def test_children__setter__child2b(self):
        self.child2b.children = [self.other]

    def test_path__child2b(self):
        _ = self.child2b.path

    def test_iter_path_reverse__child2b(self):
        for _ in self.child2b.iter_path_reverse():
            pass

    def test_ancestors__child2b(self):
        _ = self.child2b.ancestors

    def test_descendants__child2b(self):
        _ = self.child2b.descendants

    def test_root__child2b(self):
        _ = self.child2b.root

    def test_siblings__child2b(self):
        _ = self.child2b.siblings

    def test_leaves__child2b(self):
        _ = self.child2b.leaves

    def test_is_leaf__child2b(self):
        _ = self.child2b.is_leaf

    def test_is_root__child2b(self):
        _ = self.child2b.is_root

    def test_height__child2b(self):
        _ = self.child2b.height

    def test_depth__child2b(self):
        _ = self.child2b.depth


class MyMapping(NodeMixin, Mapping):
    """
    This class is used to demonstrate a possible implementation
    which defines some special methods.
    """

    def __init__(self, name, parent=None, children=None):
        super(MyMapping, self).__init__()
        self.name = name
        self.parent = parent
        if children:
            self.children = children

    def __iter__(self):
        """Iterate over all children recursively."""
        for child in self.children:
            yield child
            for item in child:
                yield item

    def __len__(self):
        """Total number of children."""
        return len(list(iter(self)))

    def __getitem__(self, name):
        for child in self:
            if child.name == name:
                return child
        raise KeyError(name)


class TestMyMapping(unittest.TestCase):
    def setUp(self):
        super(TestMyMapping, self).setUp()
        self.root1 = MyMapping("root1")
        self.child1 = MyMapping("child1", parent=self.root1)
        self.child2a = MyMapping("child2a", parent=self.child1)
        self.child2b = MyMapping("child2b", parent=self.child1)

    def test_iter(self):
        expected_list = [self.child1, self.child2a, self.child2b]
        for actual, expected in zip(iter(self.root1), expected_list):
            self.assertIs(actual, expected)

    def test_len(self):
        self.assertEqual(len(self.root1), 3)

    def test_getitem(self):
        self.assertIs(self.root1["child1"], self.child1)
        self.assertIs(self.root1["child2a"], self.child2a)
        self.assertIs(self.root1["child2b"], self.child2b)
        with self.assertRaises(KeyError):
            _ = self.root1["missing"]