Skip to content

[pyupgrade] Fix UP007: ignore Optional[NamedTuple] #18682

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

robsdedude
Copy link
Contributor

Summary

Make UP007 / UP045 ignore Optional[NamedTuple] as NamedTuple is a function (not a proper type). Rewriting it to NamedTuple | None breaks at runtime. While type checkers currently accept NamedTuple as a type, they arguably shouldn't. Therefore, we outright ignore it and don't touch or lint on it.

For a more detailed discussion, see the linked issue.

Test Plan

Added examples to the existing tests.

Related Issues

Fixes: #18619

@robsdedude robsdedude force-pushed the fix/18619-up007-named-tuple branch from 687cd54 to ca36006 Compare June 15, 2025 11:08
Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@robsdedude robsdedude marked this pull request as ready for review June 15, 2025 11:46
@ntBre ntBre added the rule Implementing or modifying a lint rule label Jun 15, 2025
@ntBre ntBre self-requested a review June 15, 2025 17:21
Copy link
Contributor

Choose a reason for hiding this comment

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

This is getting very slightly ahead of ourselves, but we're stabilizing UP045 and the preview behavior of UP007 in the upcoming release, which is taking place tomorrow. So could you update the tests in this file only to test the Union behavior? More generally, do we need to add tests for Union? I'm assuming an error is raised for NamedTuple | int, for example, not just NamedTuple | None.

Comment on lines +2 to +5
from typing import NamedTuple, Optional, Union

import typing_extensions
from typing_extensions import Optional as OptionalTE
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I know it's not a good way to format real Python code, but I generally prefer adding new imports at the bottom of the file too, just to avoid shifting all of the existing snapshots and making the diff larger. Another option is moving the existing test file to a name like UP007_0.py and adding an entirely new fixture called UP007_1.py to accompany it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UP007 crashes with TypeError on all version of Python prior to 3.14 for Optional[typing.NamedTuple]
2 participants