-
-
Notifications
You must be signed in to change notification settings - Fork 167
Overly verbose output for namedtuples #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Also, if one tries to actually document the fields, e.g.: _Foo = namedtuple("_Foo", "bar")
class Foo(_Foo):
"""
A Foo.
Attributes
----------
bar : str
The bar contents of Foo.
"""
__slots__ = () then both the documented description ("The bar contents of Foo") and the docstring autogenerated by namedtuple ("Alias for field number 0") are output by numpydoc. |
I think this is fairly easy to fix. A quick and dirty modification in Change @property
def properties(self):
if self._cls is None:
return []
return [
name
for name, func in inspect.getmembers(self._cls)
if (
not name.startswith("_")
and not self._should_skip_member(name, self._cls) #this added
and (
func is None
or isinstance(func, (property, cached_property))
or inspect.isdatadescriptor(func)
)
and self._is_show_member(name)
)
] and add @staticmethod
def _should_skip_member(name, klass):
if (issubclass(klass, tuple) and hasattr(klass, '_asdict')
and hasattr(klass, '_fields') and name in klass._fields):
return True
return False I think this should be the default behavior. I tested it with a namedtuple having code like this: class SomeClass(NamedTuple):
"""A docstring.
"""
foo: str = 'text'
"""This is a docstring of foo
"""
and it still preserves the docstring of would you like to have a PR? |
That looks like a clear improvement to me; a PR would be welcome! |
There you are :) |
numpydoc generates rather verbose outputs for namedtuples, especially when combined with autosummary:
then edit
source/index.rst
to containand
foo.py
to containthen build with
This results in

Compare with the result using either not numpydoc at all, or sphinx.ext.napoleon instead:

I guess whether "count" and "index" should be listed as methods can be argued over, but having "bar" and "baz" being listed twice seems clearly too much, and the "Attributes" entry in the autosummary table is not so great either.
The text was updated successfully, but these errors were encountered: