diff --git a/sphinx/ext/autodoc/importer.py b/sphinx/ext/autodoc/importer.py index 133ce1439c9..52b07639af6 100644 --- a/sphinx/ext/autodoc/importer.py +++ b/sphinx/ext/autodoc/importer.py @@ -206,7 +206,10 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable, if isclass(subject) and getattr(subject, '__slots__', None) is not None: from sphinx.ext.autodoc import SLOTSATTR - for name in subject.__slots__: + slots = subject.__slots__ + if isinstance(slots, str): + slots = [slots] + for name in slots: members[name] = Attribute(name, True, SLOTSATTR) # other members diff --git a/tests/roots/test-ext-autodoc/target/slots.py b/tests/roots/test-ext-autodoc/target/slots.py index 44e7503205f..17e469caca7 100644 --- a/tests/roots/test-ext-autodoc/target/slots.py +++ b/tests/roots/test-ext-autodoc/target/slots.py @@ -2,6 +2,10 @@ class Foo: __slots__ = ['attr'] +class FooSingleString: + __slots__ = 'attr' + + class Bar: __slots__ = {'attr1': 'docstring of attr1', 'attr2': 'docstring of attr2', diff --git a/tests/test_ext_autodoc.py b/tests/test_ext_autodoc.py index 9cb54de5b06..c0676f23ff1 100644 --- a/tests/test_ext_autodoc.py +++ b/tests/test_ext_autodoc.py @@ -1205,6 +1205,14 @@ def test_slots(app): ' .. py:attribute:: Foo.attr', ' :module: target.slots', '', + '', + '.. py:class:: FooSingleString()', + ' :module: target.slots', + '', + '', + ' .. py:attribute:: FooSingleString.attr', + ' :module: target.slots', + '', ]