Skip to content

Commit

Permalink
Don't trigger E721 when comparing with None (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
squiddy committed Dec 24, 2022
1 parent 4ded155 commit 32ebc1d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
4 changes: 3 additions & 1 deletion resources/test/fixtures/pycodestyle/E721.py
Expand Up @@ -15,7 +15,7 @@
if type(res) is not types.ListType:
pass
#: E721
assert type(res) == type(False) or type(res) == type(None)
assert type(res) == type(False)
#: E721
assert type(res) == type([])
#: E721
Expand Down Expand Up @@ -52,3 +52,5 @@
pass
if type(a) != type(b) or type(a) == type(ccc):
pass

assert type(res) == type(None)
11 changes: 9 additions & 2 deletions src/pycodestyle/checks.rs
@@ -1,7 +1,7 @@
use itertools::izip;
use once_cell::sync::Lazy;
use regex::Regex;
use rustpython_ast::{Located, Location, Stmt, StmtKind};
use rustpython_ast::{Constant, Located, Location, Stmt, StmtKind};
use rustpython_parser::ast::{Cmpop, Expr, ExprKind};

use crate::ast::types::Range;
Expand Down Expand Up @@ -55,7 +55,14 @@ pub fn type_comparison(ops: &[Cmpop], comparators: &[Expr], location: Range) ->
if id == "type" {
if let Some(arg) = args.first() {
// Allow comparison for types which are not obvious.
if !matches!(arg.node, ExprKind::Name { .. }) {
if !matches!(
arg.node,
ExprKind::Name { .. }
| ExprKind::Constant {
value: Constant::None,
kind: None
}
) {
checks.push(Check::new(CheckKind::TypeComparison, location));
}
}
Expand Down
Expand Up @@ -42,14 +42,6 @@ expression: checks
row: 18
column: 31
fix: ~
- kind: TypeComparison
location:
row: 18
column: 35
end_location:
row: 18
column: 58
fix: ~
- kind: TypeComparison
location:
row: 20
Expand Down

0 comments on commit 32ebc1d

Please sign in to comment.