Skip to content
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

[pyupgrade] Don't introduce invalid syntax when upgrading old-style type aliases with parenthesized multiline values (UP040) #16026

Merged
merged 3 commits into from
Feb 7, 2025

Conversation

AlexWaygood
Copy link
Member

Summary

We previously autofixed this:

from typing import TypeAlias

T: TypeAlias = (
    int
    | str
)

to this:

type T = int
    | str

When we should be autofixing it to this:

type T = (
    int
    | str
)

This PR fixes the bug. Closes #16023

Test Plan

New fixture added that repros the issue on main.

@AlexWaygood AlexWaygood added bug Something isn't working fixes Related to suggested fixes for violations python312 Related to Python 3.12 labels Feb 7, 2025
@AlexWaygood AlexWaygood changed the title add failing test [pyupgrade] Don't introduce invalid syntax when upgrading old-style type aliases (UP040) Feb 7, 2025
@AlexWaygood AlexWaygood changed the title [pyupgrade] Don't introduce invalid syntax when upgrading old-style type aliases (UP040) [pyupgrade] Don't introduce invalid syntax when upgrading old-style type aliases with parenthesized multiline values (UP040) Feb 7, 2025
@AlexWaygood
Copy link
Member Author

I think this is a small regression from #15840 (cc. @ntBre) -- totally one I should have caught in review, though!

Copy link
Contributor

github-actions bot commented Feb 7, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

T: TypeAlias = (
int
| str
)
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo Feb 7, 2025

Choose a reason for hiding this comment

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

This might be a good test case to add:

T: TypeAlias = (  # This comment should not make the fix unsafe
	int | str
)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! However, all fixes for this rule are unsafe (because you can't use PEP-695 type aliases as the second argument in isinstance() calls, whereas you can with old-style type aliases that use TypeAlias)

Copy link
Contributor

Choose a reason for hiding this comment

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

However, all fixes for this rule are unsafe

Aren't they safe in stubs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Aren't they safe in stubs?

hmm, good point. Want to make a followup PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. I'm ready whenever you are.

@ntBre
Copy link
Contributor

ntBre commented Feb 7, 2025

Sorry about that! Thanks for the quick fix! I don't have anything to add on top of the other reviews, LGTM.

@AlexWaygood AlexWaygood enabled auto-merge (squash) February 7, 2025 17:01
@AlexWaygood AlexWaygood merged commit efa8a3d into main Feb 7, 2025
20 checks passed
@AlexWaygood AlexWaygood deleted the alex/up040 branch February 7, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations python312 Related to Python 3.12
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UP040 fixes introduce a syntax error
4 participants