Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trigger E721 when comparing with None #1356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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