Skip to content

Commit

Permalink
Replace tuples with type union in isinstance or issubclass calls (ast…
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlehoux committed Feb 28, 2023
1 parent 0ed9fcc commit bd15992
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/ruff/resources/test/fixtures/pyupgrade/UP007.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ def f(x: Union[("str", "int"), float]) -> None:
def f() -> None:
x: Optional[str]
x = Optional[str]

isinstance(1, (int, float))
issubclass("yes", (int, float, str))
5 changes: 5 additions & 0 deletions crates/ruff/src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,11 @@ where
if self.settings.rules.enabled(&Rule::OSErrorAlias) {
pyupgrade::rules::os_error_alias(self, &expr);
}
if self.settings.rules.enabled(&Rule::TypingUnion) {
pyupgrade::rules::use_pep604_annotation_for_isinstance_or_issubclass(
self, expr, func, args,
);
}

// flake8-print
if self.settings.rules.enabled(&Rule::PrintFound)
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff/src/rules/pyupgrade/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub(crate) use unnecessary_encode_utf8::{unnecessary_encode_utf8, UnnecessaryEnc
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, DeprecatedCollectionType};
pub(crate) use use_pep604_annotation::{use_pep604_annotation, TypingUnion};
pub(crate) use use_pep604_annotation::{
use_pep604_annotation, use_pep604_annotation_for_isinstance_or_issubclass, TypingUnion,
};
pub(crate) use useless_metaclass_type::{useless_metaclass_type, UselessMetaclassType};
pub(crate) use useless_object_inheritance::{useless_object_inheritance, UselessObjectInheritance};

Expand Down
30 changes: 30 additions & 0 deletions crates/ruff/src/rules/pyupgrade/rules/use_pep604_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ enum TypingMember {
Optional,
}

pub fn use_pep604_annotation_for_isinstance_or_issubclass(
checker: &mut Checker,
expr: &Expr,
func: &Expr,
args: &[Expr],
) {
match &func.node {
ExprKind::Name { id, .. } if id == "isinstance" || id == "issubclass" => {
if let Some(types) = args.get(1) {
match &types.node {
ExprKind::Tuple { elts, .. } => {
let mut diagnostic =
Diagnostic::new(TypingUnion, Range::from_located(expr));
if checker.patch(diagnostic.kind.rule()) {
diagnostic.amend(Fix::replacement(
unparse_expr(&union(elts), checker.stylist),
types.location,
types.end_location.unwrap(),
));
}
checker.diagnostics.push(diagnostic);
}
_ => (),
}
}
}
_ => (),
}
}

/// UP007
pub fn use_pep604_annotation(checker: &mut Checker, expr: &Expr, value: &Expr, slice: &Expr) {
// Avoid rewriting forward annotations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,38 @@ expression: diagnostics
row: 47
column: 20
parent: ~
- kind:
TypingUnion: ~
location:
row: 50
column: 0
end_location:
row: 50
column: 27
fix:
content: int | float
location:
row: 50
column: 14
end_location:
row: 50
column: 26
parent: ~
- kind:
TypingUnion: ~
location:
row: 51
column: 0
end_location:
row: 51
column: 36
fix:
content: int | float | str
location:
row: 51
column: 18
end_location:
row: 51
column: 35
parent: ~

0 comments on commit bd15992

Please sign in to comment.