A safe version of inspect.getmembers that handles missing attributes. This is useful when there are descriptor based attributes that for some reason raise AttributeError even though they exist. This happens in zope.interface with the __provides__ attribute.
(object: t.Any, predicate: t.Any = None)
| 239 | |
| 240 | |
| 241 | def getmembers(object: t.Any, predicate: t.Any = None) -> list[tuple[str, t.Any]]: |
| 242 | """A safe version of inspect.getmembers that handles missing attributes. |
| 243 | |
| 244 | This is useful when there are descriptor based attributes that for |
| 245 | some reason raise AttributeError even though they exist. This happens |
| 246 | in zope.interface with the __provides__ attribute. |
| 247 | """ |
| 248 | results = [] |
| 249 | for key in dir(object): |
| 250 | try: |
| 251 | value = getattr(object, key) |
| 252 | except AttributeError: |
| 253 | pass |
| 254 | else: |
| 255 | if not predicate or predicate(value): |
| 256 | results.append((key, value)) |
| 257 | results.sort() |
| 258 | return results |
| 259 | |
| 260 | |
| 261 | def _validate_link(*tuples: t.Any) -> None: |
no test coverage detected
searching dependent graphs…