Skip to content

[flake8_pyi] Fix PYI041's fix causing TypeError with None | None | ... #18637

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 2 commits into
base: main
Choose a base branch
from

Conversation

robsdedude
Copy link
Contributor

@robsdedude robsdedude commented Jun 11, 2025

Summary

Fix PYI041's fix turning None | int | None | float into None | None | float, which raises a TypeError when executed.

The fix consists of making sure that the merged super-type is inserted where the first type that is merged was before.

Test Plan

Tests have been expanded with examples from the issue.

Related Issue

Fixes #18298

Copy link
Contributor

github-actions bot commented Jun 11, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@robsdedude robsdedude force-pushed the fix/18298-PYI041-fix-causing-type-error branch from 10a6bad to be1fde1 Compare June 11, 2025 21:58
@robsdedude robsdedude marked this pull request as ready for review June 11, 2025 22:10
@robsdedude robsdedude requested a review from AlexWaygood as a code owner June 11, 2025 22:10
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Jun 12, 2025
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Thanks!

Comment on lines 32 to 33
24 |-def f1(arg1: float, *, arg2: float | list[str] | type[bool] | complex) -> None: ... # PYI041
24 |+def f1(arg1: float, *, arg2: list[str] | type[bool] | complex) -> None: ... # PYI041
24 |+def f1(arg1: float, *, arg2: complex | list[str] | type[bool]) -> None: ... # PYI041
Copy link
Member

Choose a reason for hiding this comment

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

this is a clever way of fixing the issue, but I'm not totally sure that users will like it if the fix ends up reordering unions like this, even when the unions don't contain None. I think that would be pretty unexpected if I were a Ruff user.

I'm leaning towards just not providing a fix for the rule if there are multiple Nones in the union. It feels easy to understand, and well-written code should never have multiple Nones in a union anyway -- we even have other lints that would already detect that!

Copy link
Contributor Author

@robsdedude robsdedude Jun 12, 2025

Choose a reason for hiding this comment

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

Sure. Your project, your direction. I personally wouldn't be surprised. I've seen ruff fixes go beyond such changes. Further, I'd think of this more as merging elements in the union than reordering them. The PR will put the super-type in the place of the first type it's subsuming.

Anyway, I'll change the PR to just omit the fix. However, I suggest to only suppress the fix if not in a typing only context (as this is only a problem at runtime).

Copy link
Contributor Author

@robsdedude robsdedude Jun 12, 2025

Choose a reason for hiding this comment

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

While working on this fix, I've noticed that this checker is run before deferred string type annotations are visited and resolved. This means that the lint won't trigger on those. Not sure you'd want that to be changed or whether that's a deliberate design decision. Happy to pour this into an issue to tackle it later if you're interested.

@robsdedude robsdedude requested a review from AlexWaygood June 12, 2025 12:42
@AlexWaygood AlexWaygood requested a review from ntBre June 17, 2025 09:51
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PYI041 fixes None | int | None | float to None | None | float
3 participants