Fixes #8146: When identifying bases, only use classes from builtins

In inheritance_diagram extension, while iterating over bases,
we verify if the base class is one of the Python built-in
class or not.
As of now, we simply check for its presence in all `builtins`
objects. Please note, `builtins` not only has built-in classes,
but also functions (like `open`) and other built-in objects.

To avoid any sort of future problem, it seems better to only
use classes (and of course exception classes).
This commit is contained in:
Gaurav Lalchandani 2020-08-22 20:00:41 +05:30
parent 9d48cb9798
commit 4313060c8a

View File

@ -66,6 +66,10 @@ module_sig_re = re.compile(r'''^(?:([\w.]*)\.)? # module names
''', re.VERBOSE) ''', re.VERBOSE)
py_builtins = [obj for obj in vars(builtins).values()
if inspect.isclass(obj)]
def try_import(objname: str) -> Any: def try_import(objname: str) -> Any:
"""Import a object or module using *name* and *currentmodule*. """Import a object or module using *name* and *currentmodule*.
*name* should be a relative name from *currentmodule* or *name* should be a relative name from *currentmodule* or
@ -178,7 +182,6 @@ class InheritanceGraph:
traverse to. Multiple names can be specified separated by comma. traverse to. Multiple names can be specified separated by comma.
""" """
all_classes = {} all_classes = {}
py_builtins = vars(builtins).values()
def recurse(cls: Any) -> None: def recurse(cls: Any) -> None:
if not show_builtins and cls in py_builtins: if not show_builtins and cls in py_builtins: