Don't trigger E721 when comparing with None (#1356)

This commit is contained in:
Reiner Gerecke
2022-12-24 10:45:40 +01:00
committed by GitHub
parent 4ded155dc0
commit 32ebc1d227
3 changed files with 12 additions and 11 deletions

View File

@@ -15,7 +15,7 @@ import types
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
@@ -52,3 +52,5 @@ if isinstance(res, types.MethodType):
pass
if type(a) != type(b) or type(a) == type(ccc):
pass
assert type(res) == type(None)

View File

@@ -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;
@@ -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));
}
}

View File

@@ -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