Skip to content

DynamicClassAttribute drops explicitly provided empty docstrings #140972

@T90REAL

Description

@T90REAL

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions