MCPcopy Index your code
hub / github.com/ipython/ipython / dir2

Function dir2

IPython/utils/dir2.py:25–53  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

23
24
25def 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
56def get_real_method(obj: object, name: str) -> Callable[..., Any] | None:

Callers 6

dict_dirFunction · 0.90
_attr_matchesMethod · 0.90
test_baseFunction · 0.90
test_SubClassFunction · 0.90

Calls 1

safe_hasattrFunction · 0.85

Tested by 4

test_baseFunction · 0.72
test_SubClassFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…