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

[ruff] Classes with mixed type variable style (RUF053) #15841

Merged
merged 10 commits into from
Feb 6, 2025

Conversation

InSyncWithFoo
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo commented Jan 31, 2025

Summary

Resolves #15620.

RUF053 reports the first Generic[...] base of any class with a PEP 695 type parameter list. It reuses much of the logic added in #15565, #15659 and other PRs.

Since the original code is fundamentally flawed, the fix takes some liberty in its rewrite. As such, it is always marked as unsafe.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Jan 31, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Jan 31, 2025
@dhruvmanila dhruvmanila requested a review from ntBre January 31, 2025 05:31
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! Some comments below. I haven't looked in detail at the implementation yet, just at docs and the behaviour of the rule as indicated by the snapshots.

I don't think this should be a pyupgrade rule, since all our existing pyupgrade rules are modernisation/stylistic rules. This rule is different, since it detects (and fixes) behaviour that would cause the code to raise an exception at runtime. I understand that you want to share the code being used in the existing pyugprade PEP-695 rules, but we could always move those utilities to somewhere else in the ruff_linter crate, where it could be accessed by both the pyupgrade rules and this rule

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

I haven't reviewed the code in detail yet either, but it looks a little longer than I expected. I might be oversimplifying things, but can we pretty much reuse the logic from the UP046 class rule and just perform an Edit::insertion at the end of type parameter list instead of writing a full list? That would save you the effort of duplicating all the checks from UP046 (like the default kwarg that Alex pointed out) and converting existing TypeParams to TypeVars only to write them back into TypeParams. This would also lose you any deduplication it looks like you're doing with is_existing_param_of_same_class, but the incoming code was a real mess if it had mixed type variables with duplicates 😆

@InSyncWithFoo
Copy link
Contributor Author

[...] can we [...] just perform an Edit::insertion at the end of type parameter list instead of writing a full list?

I added a testcase for this:

# Original
class C[
	T  # Comment about `T`
](Generic[_A]): ...

# Wrong fix
class C[
	T  # Comment about `T`, _A
]: ...

# Also wrong fix (slightly)
class C[
	T, _A  # Comment about `T`
]: ...

# Correct fix, but looks irritating
class C[
	T  # Comment about `T`
, _A]: ...

I wasn't sure if relying on the formatter is the way to go.

@ntBre
Copy link
Contributor

ntBre commented Jan 31, 2025

Ah interesting, I did not catch that about comments, thanks! I'll try to give this a fuller review today.

@InSyncWithFoo InSyncWithFoo force-pushed the UP050 branch 3 times, most recently from 79763f4 to 7915f10 Compare January 31, 2025 21:20
@AlexWaygood
Copy link
Member

AlexWaygood commented Jan 31, 2025

Could you possibly retitle the PR and edit the description to make clear that this is no longer proposed as a pyupgrade rule?

@InSyncWithFoo InSyncWithFoo changed the title [pyupgrade] Classes with mixed type variable style (UP050) [ruff] Classes with mixed type variable style (RUF060) Jan 31, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks for this! It looks good to me overall besides a couple of nits and a bigger question about reusing the TypeVarReferenceVisitor.

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this. I especially appreciate the thorough test fixtures. I think I'll need to steal some of those for UP046 and UP047, if you don't get to them first 🙂

I'd like to see my latest comments addressed, but they're mostly nits so I'm still happy to approve.

@InSyncWithFoo InSyncWithFoo force-pushed the UP050 branch 2 times, most recently from 547a1bd to 32684c9 Compare February 5, 2025 21:38
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.

Thank you -- this overall looks great now! And I'll second @ntBre that your tests are excellent. I just have a few coding nits:

@AlexWaygood AlexWaygood dismissed their stale review February 6, 2025 14:22

requested changes were made

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.

Nice!

@InSyncWithFoo InSyncWithFoo changed the title [ruff] Classes with mixed type variable style (RUF060) [ruff] Classes with mixed type variable style (RUF053) Feb 6, 2025
@AlexWaygood AlexWaygood merged commit 84ceddc into astral-sh:main Feb 6, 2025
21 checks passed
@InSyncWithFoo InSyncWithFoo deleted the UP050 branch February 6, 2025 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect mix of generic type parameters and Generic subclass
4 participants