Skip to content

Commit 93406e4

Browse files
committed
[pyupgrade]: Deprecate non-pep604-isinstance (UP038) (#16681)
## Summary This PR deprecates UP038. Using PEP 604 syntax in `isinstance` and `issubclass` calls isn't a recommended pattern (or community agreed best practice) and it negatively impacts performance. Resolves #7871 ## Test Plan I tested that selecting `UP038` results in a warning in no-preview mode and an error in preview mode
1 parent 35f76a9 commit 93406e4

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

crates/ruff_linter/src/codes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
532532
(Pyupgrade, "035") => (RuleGroup::Stable, rules::pyupgrade::rules::DeprecatedImport),
533533
(Pyupgrade, "036") => (RuleGroup::Stable, rules::pyupgrade::rules::OutdatedVersionBlock),
534534
(Pyupgrade, "037") => (RuleGroup::Stable, rules::pyupgrade::rules::QuotedAnnotation),
535-
(Pyupgrade, "038") => (RuleGroup::Stable, rules::pyupgrade::rules::NonPEP604Isinstance),
535+
(Pyupgrade, "038") => (RuleGroup::Deprecated, rules::pyupgrade::rules::NonPEP604Isinstance),
536536
(Pyupgrade, "039") => (RuleGroup::Stable, rules::pyupgrade::rules::UnnecessaryClassParentheses),
537537
(Pyupgrade, "040") => (RuleGroup::Stable, rules::pyupgrade::rules::NonPEP695TypeAlias),
538538
(Pyupgrade, "041") => (RuleGroup::Stable, rules::pyupgrade::rules::TimeoutErrorAlias),

crates/ruff_linter/src/rules/pyupgrade/rules/use_pep604_isinstance.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ impl CallKind {
3333
}
3434
}
3535

36+
/// ## Deprecation
37+
/// This rule was deprecated as using [PEP 604] syntax in `isinstance` and `issubclass` calls
38+
/// isn't recommended practice, and it incorrectly suggests that other typing syntaxes like [PEP 695]
39+
/// would be supported by `isinstance` and `issubclass`. Using the [PEP 604] syntax
40+
/// is also slightly slower.
41+
///
3642
/// ## What it does
3743
/// Checks for uses of `isinstance` and `issubclass` that take a tuple
3844
/// of types for comparison.
@@ -64,6 +70,7 @@ impl CallKind {
6470
/// - [Python documentation: `issubclass`](https://docs.python.org/3/library/functions.html#issubclass)
6571
///
6672
/// [PEP 604]: https://peps.python.org/pep-0604/
73+
/// [PEP 695]: https://peps.python.org/pep-0695/
6774
#[derive(ViolationMetadata)]
6875
pub(crate) struct NonPEP604Isinstance {
6976
kind: CallKind,

0 commit comments

Comments
 (0)