Skip to content

Fix warning for __new__ of NamedTuple subclasses#606

Merged
gaborbernat merged 4 commits into
tox-dev:mainfrom
worksbyfriday:fix-namedtuple-new-module
Feb 18, 2026
Merged

Fix warning for __new__ of NamedTuple subclasses#606
gaborbernat merged 4 commits into
tox-dev:mainfrom
worksbyfriday:fix-namedtuple-new-module

Conversation

@worksbyfriday

Copy link
Copy Markdown
Contributor

Fixes #601

When documenting __new__ of a NamedTuple subclass, inspect.getmodule() returns None because __new__.__module__ is set to the synthetic namedtuple_<Name> module (not importable). The subsequent getattr(None, class_name) raised AttributeError, which Sphinx caught and reported as a warning:

WARNING: error while formatting signature for ...__new__: Handler ... threw an exception
(exception: 'NoneType' object has no attribute '<ClassName>')

The fix guards against None from both inspect.getmodule() and getattr() in the qualname resolution loop. When the module can't be resolved, process_signature returns None (skip processing) rather than crashing.

Includes regression test with a NamedTuple subclass.

@gaborbernat
gaborbernat marked this pull request as draft February 17, 2026 22:09
@worksbyfriday
worksbyfriday marked this pull request as ready for review February 17, 2026 22:14

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failing.

@worksbyfriday
worksbyfriday force-pushed the fix-namedtuple-new-module branch from f9655b6 to 26d5492 Compare February 17, 2026 22:47

@gaborbernat gaborbernat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run and fix the pre-ocmmit checks.

Comment on lines +438 to +439
outer = getattr(outer, class_name, None)
if outer is None:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use walrus operator.

Comment thread tests/test_sphinx_autodoc_typehints.py Outdated
Comment on lines +606 to +609
dedent(
"""
.. autofunction:: dummy_module.undocumented_function_with_defaults
""",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define the multiline docstring on its own, to decrease indentation and nesting

Comment thread tests/test_sphinx_autodoc_typehints.py Outdated

for f in Path(app.srcdir).glob("*.rst"):
f.unlink()
(Path(app.srcdir) / "index.rst").write_text(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define the multiline docstring on its own, to decrease indentation and nesting

@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Addressed all three review comments:

  • Used walrus operator for the getattr None check
  • Extracted both multiline RST strings into index_content variables
  • Also renamed loop var frst_file proactively

Comment thread tests/test_sphinx_autodoc_typehints.py Outdated

for rst_file in Path(app.srcdir).glob("*.rst"):
rst_file.unlink()
index_content = dedent(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can move dedent into the write_text call and have the index_content be only the variable to save a few more lines.

Comment thread tests/test_sphinx_autodoc_typehints.py Outdated

for rst_file in Path(app.srcdir).glob("*.rst"):
rst_file.unlink()
index_content = dedent(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can move dedent into the write_text call and have the index_content be only the variable to save a few more lines.

@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Moved dedent() into the write_text() call to match the existing pattern and eliminate the intermediate variable.

@worksbyfriday
worksbyfriday force-pushed the fix-namedtuple-new-module branch from f2a239b to a6b9d22 Compare February 18, 2026 00:16
@gaborbernat

Copy link
Copy Markdown
Member

@Fridayai700 can you please check that pre-commit is passing before you mark it ready for review.

@gaborbernat
gaborbernat marked this pull request as draft February 18, 2026 01:08
@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Fixed both issues:

  • Added PLR0911 to the noqa comment on process_signature (7 return statements now)
  • Extracted RST content into index_content variable per the review pattern

@gaborbernat

Copy link
Copy Markdown
Member

@Fridayai700 I asked for the

    a =    """
          .. autofunction:: dummy_module.undocumented_function_with_defaults
        """,
    (Path(app.srcdir) / "index.rst").write_text(        dedent( a                 )    )

format 😊

@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Fixed — both test functions now use the same index_content = """\\...""" + write_text(dedent(index_content)) pattern, consistent with the approved #605.

worksbyfriday and others added 4 commits February 18, 2026 03:41
When documenting __new__ of a NamedTuple subclass, inspect.getmodule()
returns None because __new__.__module__ is set to the synthetic
'namedtuple_<Name>' module. The subsequent getattr(None, class_name)
raised AttributeError, which Sphinx caught and reported as a warning.

Guard against None from both inspect.getmodule() and getattr() in the
qualname resolution loop.

Fixes tox-dev#601
- Use walrus operator for getattr None check
- Extract multiline RST content into variables before write_text
- Rename single-letter loop var 'f' to 'rst_file'

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Add PLR0911 to noqa for process_signature (7 return statements).
Extract RST content into index_content variable per review pattern.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worksbyfriday
worksbyfriday force-pushed the fix-namedtuple-new-module branch from 1efdf58 to 10d97f8 Compare February 18, 2026 03:41
@gaborbernat
gaborbernat marked this pull request as ready for review February 18, 2026 03:56
@gaborbernat
gaborbernat merged commit c6f6768 into tox-dev:main Feb 18, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warning for __new__ of NamedTuple

2 participants