Skip to content

Commit aa5c53b

Browse files
Remove 'non-obvious' allowance for E721 (#12300)
## Summary I don't fully understand the purpose of this. In #7905, it was just copied over from the previous non-preview implementation. But it means that (e.g.) we don't treat `type(self.foo)` as a type -- which is wrong. Closes #12290.
1 parent 4e6ecb2 commit aa5c53b

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

crates/ruff_linter/src/rules/pycodestyle/rules/type_comparison.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use crate::checkers::ast::Checker;
1616
/// Unlike a direct type comparison, `isinstance` will also check if an object
1717
/// is an instance of a class or a subclass thereof.
1818
///
19-
/// Under [preview mode](https://docs.astral.sh/ruff/preview), this rule also
20-
/// allows for direct type comparisons using `is` and `is not`, to check for
21-
/// exact type equality (while still forbidding comparisons using `==` and
22-
/// `!=`).
19+
/// If you want to check for an exact type match, use `is` or `is not`.
2320
///
2421
/// ## Example
2522
/// ```python
@@ -74,18 +71,7 @@ pub(crate) fn type_comparison(checker: &mut Checker, compare: &ast::ExprCompare)
7471
/// Returns `true` if the [`Expr`] is known to evaluate to a type (e.g., `int`, or `type(1)`).
7572
fn is_type(expr: &Expr, semantic: &SemanticModel) -> bool {
7673
match expr {
77-
Expr::Call(ast::ExprCall {
78-
func, arguments, ..
79-
}) => {
80-
// Allow comparison for types which are not obvious.
81-
if !arguments
82-
.args
83-
.first()
84-
.is_some_and(|arg| !arg.is_name_expr() && !arg.is_none_literal_expr())
85-
{
86-
return false;
87-
}
88-
74+
Expr::Call(ast::ExprCall { func, .. }) => {
8975
// Ex) `type(obj) == type(1)`
9076
semantic.match_builtin_expr(func, "type")
9177
}

crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E721_E721.py.snap

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ E721.py:21:8: E721 Use `is` and `is not` for type comparisons, or `isinstance()`
4040
23 | assert type(res) == type([])
4141
|
4242

43+
E721.py:21:36: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
44+
|
45+
19 | pass
46+
20 | #: E721
47+
21 | assert type(res) == type(False) or type(res) == type(None)
48+
| ^^^^^^^^^^^^^^^^^^^^^^^ E721
49+
22 | #: E721
50+
23 | assert type(res) == type([])
51+
|
52+
4353
E721.py:23:8: E721 Use `is` and `is not` for type comparisons, or `isinstance()` for isinstance checks
4454
|
4555
21 | assert type(res) == type(False) or type(res) == type(None)

0 commit comments

Comments
 (0)