diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-04-15 20:00:20 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-15 20:00:20 (GMT) |
commit | fbf2786c4c89430e2067016603078cf3500cfe94 (patch) | |
tree | 151141df42e6e058127ef59eae7f95c5e4644991 /Lib/inspect.py | |
parent | ba1bcffe5cafc1bb0ac6fdf9ecef51e75e342707 (diff) | |
download | cpython-fbf2786c4c89430e2067016603078cf3500cfe94.zip cpython-fbf2786c4c89430e2067016603078cf3500cfe94.tar.gz cpython-fbf2786c4c89430e2067016603078cf3500cfe94.tar.bz2 |
bpo-40257: Output object's own docstring in pydoc (GH-19479)
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 90435a1..6f7d5cd 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -542,17 +542,6 @@ def _findclass(func): return cls def _finddoc(obj): - if isclass(obj): - for base in obj.__mro__: - if base is not object: - try: - doc = base.__doc__ - except AttributeError: - continue - if doc is not None: - return doc - return None - if ismethod(obj): name = obj.__func__.__name__ self = obj.__self__ @@ -596,23 +585,35 @@ def _finddoc(obj): return None for base in cls.__mro__: try: - doc = getattr(base, name).__doc__ + doc = _getowndoc(getattr(base, name)) except AttributeError: continue if doc is not None: return doc return None +def _getowndoc(obj): + """Get the documentation string for an object if it is not + inherited from its class.""" + try: + doc = object.__getattribute__(obj, '__doc__') + if doc is None: + return None + if obj is not type: + typedoc = type(obj).__doc__ + if isinstance(typedoc, str) and typedoc == doc: + return None + return doc + except AttributeError: + return None + def getdoc(object): """Get the documentation string for an object. All tabs are expanded to spaces. To clean up docstrings that are indented to line up with blocks of code, any whitespace than can be uniformly removed from the second line onwards is removed.""" - try: - doc = object.__doc__ - except AttributeError: - return None + doc = _getowndoc(object) if doc is None: try: doc = _finddoc(object) |