dir2(obj) -> list of strings Extended version of the Python builtin dir(), which does a few extra checks. This version is guaranteed to return only a list of true strings, whereas dir() returns anything that objects inject into themselves, even if they are later not really vali
(obj: object)
| 23 | |
| 24 | |
| 25 | def dir2(obj: object) -> list[str]: |
| 26 | """dir2(obj) -> list of strings |
| 27 | |
| 28 | Extended version of the Python builtin dir(), which does a few extra |
| 29 | checks. |
| 30 | |
| 31 | This version is guaranteed to return only a list of true strings, whereas |
| 32 | dir() returns anything that objects inject into themselves, even if they |
| 33 | are later not really valid for attribute access (many extension libraries |
| 34 | have such bugs). |
| 35 | """ |
| 36 | |
| 37 | # Start building the attribute list via dir(), and then complete it |
| 38 | # with a few extra special-purpose calls. |
| 39 | |
| 40 | try: |
| 41 | words = set(dir(obj)) |
| 42 | except Exception: |
| 43 | # TypeError: dir(obj) does not return a list |
| 44 | words = set() |
| 45 | |
| 46 | if safe_hasattr(obj, "__class__"): |
| 47 | words |= set(dir(obj.__class__)) |
| 48 | |
| 49 | # filter out non-string attributes which may be stuffed by dir() calls |
| 50 | # and poor coding in third-party modules |
| 51 | |
| 52 | words = [w for w in words if isinstance(w, str)] |
| 53 | return sorted(words) |
| 54 | |
| 55 | |
| 56 | def get_real_method(obj: object, name: str) -> Callable[..., Any] | None: |
searching dependent graphs…