From fbf2786c4c89430e2067016603078cf3500cfe94 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 15 Apr 2020 23:00:20 +0300 Subject: bpo-40257: Output object's own docstring in pydoc (GH-19479) --- Lib/inspect.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'Lib/inspect.py') diff --git a/Lib/inspect.py b/Lib/inspect.py index 90435a10ca..6f7d5cd19c 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) -- cgit v1.2.1