Ensure getattr() is invoked no more than once per attribute
(self)
| 102 | if x.startswith('s')]) |
| 103 | |
| 104 | def test_excessive_getattr(self): |
| 105 | """Ensure getattr() is invoked no more than once per attribute""" |
| 106 | |
| 107 | # note the special case for @property methods below; that is why |
| 108 | # we use __dir__ and __getattr__ in class Foo to create a "magic" |
| 109 | # class attribute 'bar'. This forces `getattr` to call __getattr__ |
| 110 | # (which is doesn't necessarily do). |
| 111 | class Foo: |
| 112 | calls = 0 |
| 113 | bar = '' |
| 114 | def __getattribute__(self, name): |
| 115 | if name == 'bar': |
| 116 | self.calls += 1 |
| 117 | return None |
| 118 | return super().__getattribute__(name) |
| 119 | |
| 120 | f = Foo() |
| 121 | completer = rlcompleter.Completer(dict(f=f)) |
| 122 | self.assertEqual(completer.complete('f.b', 0), 'f.bar') |
| 123 | self.assertEqual(f.calls, 1) |
| 124 | |
| 125 | def test_property_method_not_called(self): |
| 126 | class Foo: |
nothing calls this directly
no test coverage detected