Skip to content

Fix false positive on generic NamedTuple __new__ - #22258

Merged
hauntsaninja merged 1 commit into
python:masterfrom
hauntsaninja:simply12
Oct 23, 2026
Merged

hauntsaninja merged 1 commit into
python:masterfrom
hauntsaninja:simply12

Conversation

@hauntsaninja

Copy link
Copy Markdown
Collaborator

Use erased named-tuple items when filling type variables with Any

fill_typevars_with_any computes an erased tuple but copies the original items back into its result. Keep the erased items so they agree with the Any-parameterized fallback, and remove the caller's compensating erasure.

This also prevents false new return-type errors for generic named-tuple subclasses. Extend the constructor test to accept a concrete specialization while still rejecting a return type outside the subclass.

Authored by Codex

Use erased named-tuple items when filling type variables with Any

fill_typevars_with_any computes an erased tuple but copies the original
items back into its result. Keep the erased items so they agree with the
Any-parameterized fallback, and remove the caller's compensating erasure.

This also prevents false __new__ return-type errors for generic named-tuple
subclasses. Extend the constructor test to accept a concrete specialization
while still rejecting a return type outside the subclass.

Authored by Codex
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sphinx (https://github.com/sphinx-doc/sphinx)
- sphinx/testing/util.py:80:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ scipy-stubs/_lib/_ccallback.pyi:123: error: Unused "type: ignore" comment  [unused-ignore]
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@hauntsaninja
hauntsaninja marked this pull request as ready for review October 21, 2026 19:22
@hauntsaninja

Copy link
Copy Markdown
Collaborator Author

Great, scipy-stubs unused ignore is correct

@ilevkivskyi ilevkivskyi 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.

Thanks!

@hauntsaninja
hauntsaninja merged commit 8fdf437 into python:master Oct 23, 2026
25 checks passed
@hauntsaninja
hauntsaninja deleted the simply12 branch October 23, 2026 00:30
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.

2 participants