python / python/cpython

`DynamicClassAttribute` drops explicitly provided empty docstrings

Open
#140,972 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

types.DynamicClassAttribute loses an explicitly provided empty docstring. Its constructor assigns self.__doc__ = doc or fget.__doc__, so when you pass doc='' and fget is None, the descriptor ends up with None.__doc__ (“The type of the None singleton.”) instead of retaining the empty string. This breaks parity with property, which preserves an empty doc, and cascades through setter()/deleter().

from types import DynamicClassAttribute

attr = DynamicClassAttribute(fget=None, fset=None, fdel=None, doc='')
print(repr(attr.__doc__))  # Expected '', but prints 'The type of the None singleton.'
None

Easy Fix:

 class DynamicClassAttribute:
     def __init__(self, fget=None, fset=None, fdel=None, doc=None):
         self.fget = fget
         self.fset = fset
         self.fdel = fdel
-        self.__doc__ = doc or fget.__doc__
-        self.overwrite_doc = doc is None
+        if doc is None and fget is not None:
+            doc = fget.__doc__
+        self.__doc__ = doc
+        self.overwrite_doc = doc is None
CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs
  • gh-140975
  • gh-141242

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the DynamicClassAttribute constructor in CPython's types implementation and inspect how an explicitly provided empty docstring is handled when fget is None. Confirm that the descriptor and its setter()/deleter() results preserve '', matching property behavior, and check the linked PRs for work already underway.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.