Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d1e768a

Browse files
authored
bpo-36326: Let inspect.getdoc() find docstrings for __slots__ (GH-12498)
1 parent 713a8ae commit d1e768a

6 files changed

Lines changed: 32 additions & 3 deletions

File tree

Doc/whatsnew/3.8.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ gettext
174174
Added :func:`~gettext.pgettext` and its variants.
175175
(Contributed by Franz Glasner, Éric Araujo, and Cheryl Sabella in :issue:`2504`.)
176176

177+
inspect
178+
-------
179+
180+
The :func:`inspect.getdoc` function can now find docstrings for ``__slots__``
181+
if that attribute is a :class:`dict` where the values are docstrings.
182+
This provides documentation options similar to what we already have
183+
for :func:`property`, :func:`classmethod`, and :func:`staticmethod`::
184+
185+
class AudioClip:
186+
__slots__ = {'bit_rate': 'expressed in kilohertz to one decimal place',
187+
'duration': 'in seconds, rounded up to an integer'}
188+
def __init__(self, bit_rate, duration):
189+
self.bit_rate = round(bit_rate / 1000.0, 1)
190+
self.duration = ceil(duration)
177191

178192
gc
179193
--

Lib/inspect.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,12 @@ def _finddoc(obj):
582582
cls = obj.__objclass__
583583
if getattr(cls, name) is not obj:
584584
return None
585+
if ismemberdescriptor(obj):
586+
slots = getattr(cls, '__slots__', None)
587+
if isinstance(slots, dict) and name in slots:
588+
return slots[name]
585589
else:
586590
return None
587-
588591
for base in cls.__mro__:
589592
try:
590593
doc = getattr(base, name).__doc__

Lib/statistics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ class NormalDist:
709709
# https://en.wikipedia.org/wiki/Normal_distribution
710710
# https://en.wikipedia.org/wiki/Variance#Properties
711711

712-
__slots__ = ('mu', 'sigma')
712+
__slots__ = {'mu': 'Arithmetic mean of a normal distribution',
713+
'sigma': 'Standard deviation of a normal distribution'}
713714

714715
def __init__(self, mu=0.0, sigma=1.0):
715716
'NormalDist where mu is the mean and sigma is the standard deviation.'

Lib/test/test_inspect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ def assertSourceEqual(self, obj, top, bottom):
375375
self.assertEqual(inspect.getsource(obj),
376376
self.sourcerange(top, bottom))
377377

378+
class SlotUser:
379+
'Docstrings for __slots__'
380+
__slots__ = {'power': 'measured in kilowatts',
381+
'distance': 'measured in kilometers'}
382+
378383
class TestRetrievingSourceCode(GetSourceBase):
379384
fodderModule = mod
380385

@@ -429,6 +434,10 @@ def test_getdoc(self):
429434
'A longer,\n\nindented\n\ndocstring.')
430435
self.assertEqual(inspect.getdoc(git.abuse),
431436
'Another\n\ndocstring\n\ncontaining\n\ntabs')
437+
self.assertEqual(inspect.getdoc(SlotUser.power),
438+
'measured in kilowatts')
439+
self.assertEqual(inspect.getdoc(SlotUser.distance),
440+
'measured in kilometers')
432441

433442
@unittest.skipIf(sys.flags.optimize >= 2,
434443
"Docstrings are omitted with -O2 and above")

Lib/test/test_statistics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,7 @@ def test_slots(self):
20512051
nd = statistics.NormalDist(300, 23)
20522052
with self.assertRaises(TypeError):
20532053
vars(nd)
2054-
self.assertEqual(nd.__slots__, ('mu', 'sigma'))
2054+
self.assertEqual(tuple(nd.__slots__), ('mu', 'sigma'))
20552055

20562056
def test_instantiation_and_attributes(self):
20572057
nd = statistics.NormalDist(500, 17)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
inspect.getdoc() can now find docstrings for member objects when __slots__
2+
is a dictionary.

0 commit comments

Comments
 (0)