Skip to content

Commit

Permalink
Rename rules containing PEP reference in name
Browse files Browse the repository at this point in the history
  • Loading branch information
not-my-profile authored and charliermarsh committed Feb 11, 2023
1 parent 42924c0 commit 7db6a2d
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 70 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -845,10 +845,10 @@ For more, see [pyupgrade](https://pypi.org/project/pyupgrade/) on PyPI.
| UP003 | type-of-primitive | Use `{}` instead of `type(...)` | 🛠 |
| UP004 | useless-object-inheritance | Class `{name}` inherits from `object` | 🛠 |
| UP005 | deprecated-unittest-alias | `{alias}` is deprecated, use `{target}` | 🛠 |
| UP006 | use-pep585-annotation | Use `{}` instead of `{}` for type annotations | 🛠 |
| UP007 | use-pep604-annotation | Use `X \| Y` for type annotations | 🛠 |
| UP006 | deprecated-collection-type | Use `{}` instead of `{}` for type annotations | 🛠 |
| UP007 | typing-union | Use `X \| Y` for type annotations | 🛠 |
| UP008 | super-call-with-parameters | Use `super()` instead of `super(__class__, self)` | 🛠 |
| UP009 | pep3120-unnecessary-coding-comment | UTF-8 encoding declaration is unnecessary | 🛠 |
| UP009 | utf8-encoding-declaration | UTF-8 encoding declaration is unnecessary | 🛠 |
| UP010 | unnecessary-future-import | Unnecessary `__future__` import `{import}` for target Python version | 🛠 |
| UP011 | lru-cache-without-parameters | Unnecessary parameters to `functools.lru_cache` | 🛠 |
| UP012 | unnecessary-encode-utf8 | Unnecessary call to `encode` as UTF-8 | 🛠 |
Expand Down
6 changes: 3 additions & 3 deletions crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,7 @@ where
// Ex) Optional[...]
if !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP604Annotation)
&& self.settings.rules.enabled(&Rule::TypingUnion)
&& (self.settings.target_version >= PythonVersion::Py310
|| (self.settings.target_version >= PythonVersion::Py37
&& self.annotations_future_enabled
Expand Down Expand Up @@ -2117,7 +2117,7 @@ where
// Ex) List[...]
if !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP585Annotation)
&& self.settings.rules.enabled(&Rule::DeprecatedCollectionType)
&& (self.settings.target_version >= PythonVersion::Py39
|| (self.settings.target_version >= PythonVersion::Py37
&& self.annotations_future_enabled
Expand Down Expand Up @@ -2162,7 +2162,7 @@ where
// Ex) typing.List[...]
if !self.in_deferred_string_type_definition
&& !self.settings.pyupgrade.keep_runtime_typing
&& self.settings.rules.enabled(&Rule::UsePEP585Annotation)
&& self.settings.rules.enabled(&Rule::DeprecatedCollectionType)
&& (self.settings.target_version >= PythonVersion::Py39
|| (self.settings.target_version >= PythonVersion::Py37
&& self.annotations_future_enabled
Expand Down
8 changes: 2 additions & 6 deletions crates/ruff/src/checkers/physical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,12 @@ pub fn check_physical_lines(
let enforce_doc_line_too_long = settings.rules.enabled(&Rule::DocLineTooLong);
let enforce_line_too_long = settings.rules.enabled(&Rule::LineTooLong);
let enforce_no_newline_at_end_of_file = settings.rules.enabled(&Rule::NoNewLineAtEndOfFile);
let enforce_unnecessary_coding_comment = settings
.rules
.enabled(&Rule::PEP3120UnnecessaryCodingComment);
let enforce_unnecessary_coding_comment = settings.rules.enabled(&Rule::UTF8EncodingDeclaration);
let enforce_mixed_spaces_and_tabs = settings.rules.enabled(&Rule::MixedSpacesAndTabs);
let enforce_bidirectional_unicode = settings.rules.enabled(&Rule::BidirectionalUnicode);

let fix_unnecessary_coding_comment = matches!(autofix, flags::Autofix::Enabled)
&& settings
.rules
.should_fix(&Rule::PEP3120UnnecessaryCodingComment);
&& settings.rules.should_fix(&Rule::UTF8EncodingDeclaration);
let fix_shebang_whitespace = matches!(autofix, flags::Autofix::Enabled)
&& settings.rules.should_fix(&Rule::ShebangWhitespace);

Expand Down
8 changes: 4 additions & 4 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ ruff_macros::define_rule_mapping!(
UP003 => rules::pyupgrade::rules::TypeOfPrimitive,
UP004 => rules::pyupgrade::rules::UselessObjectInheritance,
UP005 => rules::pyupgrade::rules::DeprecatedUnittestAlias,
UP006 => rules::pyupgrade::rules::UsePEP585Annotation,
UP007 => rules::pyupgrade::rules::UsePEP604Annotation,
UP006 => rules::pyupgrade::rules::DeprecatedCollectionType,
UP007 => rules::pyupgrade::rules::TypingUnion,
UP008 => rules::pyupgrade::rules::SuperCallWithParameters,
UP009 => rules::pyupgrade::rules::PEP3120UnnecessaryCodingComment,
UP009 => rules::pyupgrade::rules::UTF8EncodingDeclaration,
UP010 => rules::pyupgrade::rules::UnnecessaryFutureImport,
UP011 => rules::pyupgrade::rules::LRUCacheWithoutParameters,
UP012 => rules::pyupgrade::rules::UnnecessaryEncodeUTF8,
Expand Down Expand Up @@ -751,7 +751,7 @@ impl Rule {
| Rule::LineTooLong
| Rule::MixedSpacesAndTabs
| Rule::NoNewLineAtEndOfFile
| Rule::PEP3120UnnecessaryCodingComment
| Rule::UTF8EncodingDeclaration
| Rule::ShebangMissingExecutableFile
| Rule::ShebangNotExecutable
| Rule::ShebangNewline
Expand Down
22 changes: 11 additions & 11 deletions crates/ruff/src/rules/pyupgrade/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ mod tests {
#[test_case(Rule::TypeOfPrimitive, Path::new("UP003.py"); "UP003")]
#[test_case(Rule::UselessObjectInheritance, Path::new("UP004.py"); "UP004")]
#[test_case(Rule::DeprecatedUnittestAlias, Path::new("UP005.py"); "UP005")]
#[test_case(Rule::UsePEP585Annotation, Path::new("UP006.py"); "UP006")]
#[test_case(Rule::UsePEP604Annotation, Path::new("UP007.py"); "UP007")]
#[test_case(Rule::DeprecatedCollectionType, Path::new("UP006.py"); "UP006")]
#[test_case(Rule::TypingUnion, Path::new("UP007.py"); "UP007")]
#[test_case(Rule::SuperCallWithParameters, Path::new("UP008.py"); "UP008")]
#[test_case(Rule::PEP3120UnnecessaryCodingComment, Path::new("UP009_0.py"); "UP009_0")]
#[test_case(Rule::PEP3120UnnecessaryCodingComment, Path::new("UP009_1.py"); "UP009_1")]
#[test_case(Rule::PEP3120UnnecessaryCodingComment, Path::new("UP009_2.py"); "UP009_2")]
#[test_case(Rule::PEP3120UnnecessaryCodingComment, Path::new("UP009_3.py"); "UP009_3")]
#[test_case(Rule::PEP3120UnnecessaryCodingComment, Path::new("UP009_4.py"); "UP009_4")]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_0.py"); "UP009_0")]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_1.py"); "UP009_1")]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_2.py"); "UP009_2")]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_3.py"); "UP009_3")]
#[test_case(Rule::UTF8EncodingDeclaration, Path::new("UP009_4.py"); "UP009_4")]
#[test_case(Rule::UnnecessaryFutureImport, Path::new("UP010.py"); "UP010")]
#[test_case(Rule::LRUCacheWithoutParameters, Path::new("UP011.py"); "UP011")]
#[test_case(Rule::UnnecessaryEncodeUTF8, Path::new("UP012.py"); "UP012")]
Expand Down Expand Up @@ -81,7 +81,7 @@ mod tests {
Path::new("pyupgrade/future_annotations.py"),
&settings::Settings {
target_version: PythonVersion::Py37,
..settings::Settings::for_rule(Rule::UsePEP585Annotation)
..settings::Settings::for_rule(Rule::DeprecatedCollectionType)
},
)?;
assert_yaml_snapshot!(diagnostics);
Expand All @@ -94,7 +94,7 @@ mod tests {
Path::new("pyupgrade/future_annotations.py"),
&settings::Settings {
target_version: PythonVersion::Py310,
..settings::Settings::for_rule(Rule::UsePEP585Annotation)
..settings::Settings::for_rule(Rule::DeprecatedCollectionType)
},
)?;
assert_yaml_snapshot!(diagnostics);
Expand All @@ -107,7 +107,7 @@ mod tests {
Path::new("pyupgrade/future_annotations.py"),
&settings::Settings {
target_version: PythonVersion::Py37,
..settings::Settings::for_rule(Rule::UsePEP604Annotation)
..settings::Settings::for_rule(Rule::TypingUnion)
},
)?;
assert_yaml_snapshot!(diagnostics);
Expand All @@ -120,7 +120,7 @@ mod tests {
Path::new("pyupgrade/future_annotations.py"),
&settings::Settings {
target_version: PythonVersion::Py310,
..settings::Settings::for_rule(Rule::UsePEP604Annotation)
..settings::Settings::for_rule(Rule::TypingUnion)
},
)?;
assert_yaml_snapshot!(diagnostics);
Expand Down
8 changes: 3 additions & 5 deletions crates/ruff/src/rules/pyupgrade/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ pub(crate) use super_call_with_parameters::{super_call_with_parameters, SuperCal
pub(crate) use type_of_primitive::{type_of_primitive, TypeOfPrimitive};
pub(crate) use typing_text_str_alias::{typing_text_str_alias, TypingTextStrAlias};
pub(crate) use unnecessary_builtin_import::{unnecessary_builtin_import, UnnecessaryBuiltinImport};
pub(crate) use unnecessary_coding_comment::{
unnecessary_coding_comment, PEP3120UnnecessaryCodingComment,
};
pub(crate) use unnecessary_coding_comment::{unnecessary_coding_comment, UTF8EncodingDeclaration};
pub(crate) use unnecessary_encode_utf8::{unnecessary_encode_utf8, UnnecessaryEncodeUTF8};
pub(crate) use unnecessary_future_import::{unnecessary_future_import, UnnecessaryFutureImport};
pub(crate) use unpack_list_comprehension::{unpack_list_comprehension, RewriteListComprehension};
pub(crate) use use_pep585_annotation::{use_pep585_annotation, UsePEP585Annotation};
pub(crate) use use_pep604_annotation::{use_pep604_annotation, UsePEP604Annotation};
pub(crate) use use_pep585_annotation::{use_pep585_annotation, DeprecatedCollectionType};
pub(crate) use use_pep604_annotation::{use_pep604_annotation, TypingUnion};
pub(crate) use useless_metaclass_type::{useless_metaclass_type, UselessMetaclassType};
pub(crate) use useless_object_inheritance::{useless_object_inheritance, UselessObjectInheritance};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::registry::Diagnostic;
use crate::violation::AlwaysAutofixableViolation;

define_violation!(
pub struct PEP3120UnnecessaryCodingComment;
// TODO: document referencing [PEP 3120]: https://peps.python.org/pep-3120/
pub struct UTF8EncodingDeclaration;
);
impl AlwaysAutofixableViolation for PEP3120UnnecessaryCodingComment {
impl AlwaysAutofixableViolation for UTF8EncodingDeclaration {
#[derive_message_formats]
fn message(&self) -> String {
format!("UTF-8 encoding declaration is unnecessary")
Expand All @@ -31,7 +32,7 @@ pub fn unnecessary_coding_comment(lineno: usize, line: &str, autofix: bool) -> O
// PEP3120 makes utf-8 the default encoding.
if CODING_COMMENT_REGEX.is_match(line) {
let mut diagnostic = Diagnostic::new(
PEP3120UnnecessaryCodingComment,
UTF8EncodingDeclaration,
Range::new(Location::new(lineno + 1, 0), Location::new(lineno + 2, 0)),
);
if autofix {
Expand Down
11 changes: 6 additions & 5 deletions crates/ruff/src/rules/pyupgrade/rules/use_pep585_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use crate::registry::Diagnostic;
use crate::violation::AlwaysAutofixableViolation;

define_violation!(
pub struct UsePEP585Annotation {
// TODO: document referencing [PEP 585]: https://peps.python.org/pep-0585/
pub struct DeprecatedCollectionType {
pub name: String,
}
);
impl AlwaysAutofixableViolation for UsePEP585Annotation {
impl AlwaysAutofixableViolation for DeprecatedCollectionType {
#[derive_message_formats]
fn message(&self) -> String {
let UsePEP585Annotation { name } = self;
let DeprecatedCollectionType { name } = self;
format!(
"Use `{}` instead of `{}` for type annotations",
name.to_lowercase(),
Expand All @@ -24,7 +25,7 @@ impl AlwaysAutofixableViolation for UsePEP585Annotation {
}

fn autofix_title(&self) -> String {
let UsePEP585Annotation { name } = self;
let DeprecatedCollectionType { name } = self;
format!("Replace `{name}` with `{}`", name.to_lowercase(),)
}
}
Expand All @@ -36,7 +37,7 @@ pub fn use_pep585_annotation(checker: &mut Checker, expr: &Expr) {
.and_then(|call_path| call_path.last().copied())
{
let mut diagnostic = Diagnostic::new(
UsePEP585Annotation {
DeprecatedCollectionType {
name: binding.to_string(),
},
Range::from_located(expr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::registry::Diagnostic;
use crate::violation::AlwaysAutofixableViolation;

define_violation!(
pub struct UsePEP604Annotation;
// TODO: document referencing [PEP 604]: https://peps.python.org/pep-0604/
pub struct TypingUnion;
);
impl AlwaysAutofixableViolation for UsePEP604Annotation {
impl AlwaysAutofixableViolation for TypingUnion {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use `X | Y` for type annotations")
Expand Down Expand Up @@ -95,7 +96,7 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s

match typing_member {
TypingMember::Optional => {
let mut diagnostic = Diagnostic::new(UsePEP604Annotation, Range::from_located(expr));
let mut diagnostic = Diagnostic::new(TypingUnion, Range::from_located(expr));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
unparse_expr(&optional(slice), checker.stylist),
Expand All @@ -106,7 +107,7 @@ pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, s
checker.diagnostics.push(diagnostic);
}
TypingMember::Union => {
let mut diagnostic = Diagnostic::new(UsePEP604Annotation, Range::from_located(expr));
let mut diagnostic = Diagnostic::new(TypingUnion, Range::from_located(expr));
if checker.patch(diagnostic.kind.rule()) {
match &slice.node {
ExprKind::Slice { .. } => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pyupgrade/mod.rs
expression: diagnostics
---
- kind:
UsePEP585Annotation:
DeprecatedCollectionType:
name: List
location:
row: 4
Expand All @@ -22,7 +22,7 @@ expression: diagnostics
column: 20
parent: ~
- kind:
UsePEP585Annotation:
DeprecatedCollectionType:
name: List
location:
row: 11
Expand All @@ -41,7 +41,7 @@ expression: diagnostics
column: 13
parent: ~
- kind:
UsePEP585Annotation:
DeprecatedCollectionType:
name: List
location:
row: 18
Expand All @@ -60,7 +60,7 @@ expression: diagnostics
column: 15
parent: ~
- kind:
UsePEP585Annotation:
DeprecatedCollectionType:
name: List
location:
row: 25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pyupgrade/mod.rs
expression: diagnostics
---
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 6
column: 9
Expand All @@ -21,7 +21,7 @@ expression: diagnostics
column: 22
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 10
column: 9
Expand All @@ -39,7 +39,7 @@ expression: diagnostics
column: 29
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 14
column: 9
Expand All @@ -57,7 +57,7 @@ expression: diagnostics
column: 45
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 14
column: 25
Expand All @@ -75,7 +75,7 @@ expression: diagnostics
column: 44
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 18
column: 9
Expand All @@ -93,7 +93,7 @@ expression: diagnostics
column: 31
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 22
column: 9
Expand All @@ -111,7 +111,7 @@ expression: diagnostics
column: 33
parent: ~
- kind:
UsePEP604Annotation: ~
TypingUnion: ~
location:
row: 26
column: 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pyupgrade/mod.rs
expression: diagnostics
---
- kind:
PEP3120UnnecessaryCodingComment: ~
UTF8EncodingDeclaration: ~
location:
row: 1
column: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ruff/src/rules/pyupgrade/mod.rs
expression: diagnostics
---
- kind:
PEP3120UnnecessaryCodingComment: ~
UTF8EncodingDeclaration: ~
location:
row: 2
column: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
source: src/rules/pyupgrade/mod.rs
source: crates/ruff/src/rules/pyupgrade/mod.rs
expression: diagnostics
---
- kind:
UsePEP585Annotation:
DeprecatedCollectionType:
name: List
location:
row: 34
Expand Down
Loading

0 comments on commit 7db6a2d

Please sign in to comment.