-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[pyupgrade]: new rule UP050 (useless-class-metaclass-type)
#18334
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
Merged
MichaReiser
merged 8 commits into
astral-sh:main
from
chirizxc:feat/useless-class-metaclass-type
May 28, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc1a878
[`pyupgrade`]: new rule UP050 (`useless-class-metaclass-type`)
chirizxc a36d619
fix: clippy
chirizxc e9321e4
fix: test
chirizxc e1b5094
feat: improve `UP050` documentation
chirizxc f70fcbc
fix: resolve review
chirizxc 6ab1c16
chore: simplify
chirizxc f7f4ad3
feat: add one more test
chirizxc 2a7538e
Update crates/ruff_linter/src/codes.rs
MichaReiser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP050.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| class A: | ||
| ... | ||
|
|
||
|
|
||
| class A(metaclass=type): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| metaclass=type | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| metaclass=type | ||
| # | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| # | ||
| metaclass=type | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| metaclass=type, | ||
| # | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| # | ||
| metaclass=type, | ||
| # | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class B(A, metaclass=type): | ||
| ... | ||
|
|
||
|
|
||
| class B( | ||
| A, | ||
| metaclass=type, | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| class B( | ||
| A, | ||
| # comment | ||
| metaclass=type, | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| def foo(): | ||
| class A(metaclass=type): | ||
| ... | ||
|
|
||
|
|
||
| class A( | ||
| metaclass=type # comment | ||
| , | ||
| ): | ||
| ... | ||
|
|
||
|
|
||
| type = str | ||
|
|
||
| class Foo(metaclass=type): | ||
| ... | ||
|
|
||
|
|
||
| import builtins | ||
|
|
||
| class A(metaclass=builtins.type): | ||
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
crates/ruff_linter/src/rules/pyupgrade/rules/useless_class_metaclass_type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use crate::checkers::ast::Checker; | ||
| use crate::fix::edits::{Parentheses, remove_argument}; | ||
| use ruff_diagnostics::{Diagnostic, Fix, FixAvailability, Violation}; | ||
| use ruff_macros::{ViolationMetadata, derive_message_formats}; | ||
| use ruff_python_ast::StmtClassDef; | ||
| use ruff_text_size::Ranged; | ||
|
|
||
| /// ## What it does | ||
| /// Checks for `metaclass=type` in class definitions. | ||
| /// | ||
| /// ## Why is this bad? | ||
| /// Since Python 3, the default metaclass is `type`, so specifying it explicitly is redundant. | ||
| /// | ||
| /// Even though `__prepare__` is not required, the default metaclass (`type`) implements it, | ||
| /// for the convenience of subclasses calling it via `super()`. | ||
| /// ## Example | ||
| /// | ||
| /// ```python | ||
| /// class Foo(metaclass=type): ... | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// | ||
| /// ```python | ||
| /// class Foo: ... | ||
| /// ``` | ||
| /// | ||
| /// ## References | ||
| /// - [PEP 3115 – Metaclasses in Python 3000](https://peps.python.org/pep-3115/) | ||
| #[derive(ViolationMetadata)] | ||
| pub(crate) struct UselessClassMetaclassType { | ||
| name: String, | ||
| } | ||
|
|
||
| impl Violation for UselessClassMetaclassType { | ||
| const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; | ||
|
|
||
| #[derive_message_formats] | ||
| fn message(&self) -> String { | ||
| let UselessClassMetaclassType { name } = self; | ||
| format!("Class `{name}` uses `metaclass=type`, which is redundant") | ||
| } | ||
|
|
||
| fn fix_title(&self) -> Option<String> { | ||
| Some("Remove `metaclass=type`".to_string()) | ||
| } | ||
| } | ||
|
|
||
| /// UP050 | ||
| pub(crate) fn useless_class_metaclass_type(checker: &Checker, class_def: &StmtClassDef) { | ||
| let Some(arguments) = class_def.arguments.as_deref() else { | ||
| return; | ||
| }; | ||
|
|
||
| for keyword in &arguments.keywords { | ||
| if let (Some("metaclass"), expr) = (keyword.arg.as_deref(), &keyword.value) { | ||
| if checker.semantic().match_builtin_expr(expr, "type") { | ||
| let mut diagnostic = Diagnostic::new( | ||
| UselessClassMetaclassType { | ||
| name: class_def.name.to_string(), | ||
| }, | ||
| keyword.range(), | ||
| ); | ||
|
|
||
| diagnostic.try_set_fix(|| { | ||
| remove_argument( | ||
| keyword, | ||
| arguments, | ||
| Parentheses::Remove, | ||
| checker.locator().contents(), | ||
| ) | ||
| .map(Fix::safe_edit) | ||
| }); | ||
|
|
||
| checker.report_diagnostic(diagnostic); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.