harbor run -t swe-rebench/tox-dev__sphinx-autodoc-typehints-606harbor download swe-rebench/tox-dev__sphinx-autodoc-typehints-606Warning for new of NamedTuple
We subclass NamedTuple and want to automatically document the __new__ method of classes.
We get this warning:
WARNING: error while formatting signature for icalendar.cal.alarm.Alarm.Triggers.__new__: Handler <function process_signature at 0x105f32de0> for event 'autodoc-process-signature' threw an exception (exception: 'NoneType' object has no attribute 'Triggers') [autodoc]
WARNING: error while formatting signature for icalendar.prop.AdrFields.__new__: Handler <function process_signature at 0x105f32de0> for event 'autodoc-process-signature' threw an exception (exception: 'NoneType' object has no attribute 'AdrFields') [autodoc]
WARNING: error while formatting signature for icalendar.prop.NFields.__new__: Handler <function process_signature at 0x105f32de0> for event 'autodoc-process-signature' threw an exception (exception: 'NoneType' object has no attribute 'NFields') [autodoc]
I can reproduce that the module is not properly set. The last line contains the correct module.
~/icalendar 1079-document-init-methods$ python
Python 3.13.2 (main, Feb 5 2025, 19:11:32) [Clang 19.1.6 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import icalendar
>>> icalendar.NFields.__new__.__module__
'namedtuple_NFields'
>>> icalendar.NFields.__init__.__module__
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
icalendar.NFields.__init__.__module__
AttributeError: 'wrapper_descriptor' object has no attribute '__module__'. Did you mean: '__reduce__'?
>>> icalendar.NFields.__module__
'icalendar.prop'
Code to reproduce:
from typing import NamedTuple
from datetime import timedelta
class Triggers(NamedTuple):
"""The computed times of alarm triggers.
"""
start: tuple[timedelta]
end: tuple[timedelta]
absolute: tuple[datetime]
autodoc_default_options = {
"ignore-module-all": True,
"members": True,
"show-inheritance": True,
"special-members": "__init__,__new__",
"undoc-members": True,
}
docs:
Module contents
---------------
.. automodule:: icalendar.timezone
:members:
:show-inheritance:
:undoc-members:
Adding these lines changes the error message:
Triggers.__new__.__module__ = __module__
Triggers.__new__.__qualname__ = f"{Triggers.__qualname__}.__new__"
Error:
WARNING: Cannot resolve forward reference in type annotations of "icalendar.cal.alarm.Alarm.Triggers.__new__": name 'tuple' is not defined [sphinx_autodoc_typehints.forward_reference]
Related: