Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ gettext
Added :func:`~gettext.pgettext` and its variants.
(Contributed by Franz Glasner, Éric Araujo, and Cheryl Sabella in :issue:`2504`.)

inspect
-------

The :func:`inspect.getdoc` function can now find docstrings for ``__slots__``
if that attribute is a :class:`dict` where the values are docstrings.
This provides documentation options similar to what we already have
for :func:`property`, :func:`classmethod`, and :func:`staticmethod`::

class AudioClip:
__slots__ = {'bit_rate': 'expressed in kilohertz to one decimal place',
'duration': 'in seconds, rounded up to an integer'}
def __init__(self, bit_rate, duration):
self.bit_rate = round(bit_rate / 1000.0, 1)
self.duration = ceil(duration)

gc
--
Expand Down
5 changes: 4 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,12 @@ def _finddoc(obj):
cls = obj.__objclass__
if getattr(cls, name) is not obj:
return None
if ismemberdescriptor(obj):
slots = getattr(cls, '__slots__', None)
if isinstance(slots, dict) and name in slots:
return slots[name]
else:
return None

for base in cls.__mro__:
try:
doc = getattr(base, name).__doc__
Expand Down
3 changes: 2 additions & 1 deletion Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,8 @@ class NormalDist:
# https://en.wikipedia.org/wiki/Normal_distribution
# https://en.wikipedia.org/wiki/Variance#Properties

__slots__ = ('mu', 'sigma')
__slots__ = {'mu': 'Arithmetic mean of a normal distribution',
'sigma': 'Standard deviation of a normal distribution'}

def __init__(self, mu=0.0, sigma=1.0):
'NormalDist where mu is the mean and sigma is the standard deviation.'
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ def assertSourceEqual(self, obj, top, bottom):
self.assertEqual(inspect.getsource(obj),
self.sourcerange(top, bottom))

class SlotUser:
'Docstrings for __slots__'
__slots__ = {'power': 'measured in kilowatts',
'distance': 'measured in kilometers'}

class TestRetrievingSourceCode(GetSourceBase):
fodderModule = mod

Expand Down Expand Up @@ -429,6 +434,10 @@ def test_getdoc(self):
'A longer,\n\nindented\n\ndocstring.')
self.assertEqual(inspect.getdoc(git.abuse),
'Another\n\ndocstring\n\ncontaining\n\ntabs')
self.assertEqual(inspect.getdoc(SlotUser.power),
'measured in kilowatts')
self.assertEqual(inspect.getdoc(SlotUser.distance),
'measured in kilometers')

@unittest.skipIf(sys.flags.optimize >= 2,
"Docstrings are omitted with -O2 and above")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,7 @@ def test_slots(self):
nd = statistics.NormalDist(300, 23)
with self.assertRaises(TypeError):
vars(nd)
self.assertEqual(nd.__slots__, ('mu', 'sigma'))
self.assertEqual(tuple(nd.__slots__), ('mu', 'sigma'))

def test_instantiation_and_attributes(self):
nd = statistics.NormalDist(500, 17)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
inspect.getdoc() can now find docstrings for member objects when __slots__
is a dictionary.