From 32ebc1d22728b105dcdc432943e266f950b2f152 Mon Sep 17 00:00:00 2001 From: Reiner Gerecke Date: Sat, 24 Dec 2022 10:45:40 +0100 Subject: [PATCH] Don't trigger E721 when comparing with None (#1356) --- resources/test/fixtures/pycodestyle/E721.py | 4 +++- src/pycodestyle/checks.rs | 11 +++++++++-- .../ruff__pycodestyle__tests__E721_E721.py.snap | 8 -------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/resources/test/fixtures/pycodestyle/E721.py b/resources/test/fixtures/pycodestyle/E721.py index f06bed4e386fe..acb92fab29c9f 100644 --- a/resources/test/fixtures/pycodestyle/E721.py +++ b/resources/test/fixtures/pycodestyle/E721.py @@ -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 @@ -52,3 +52,5 @@ pass if type(a) != type(b) or type(a) == type(ccc): pass + +assert type(res) == type(None) diff --git a/src/pycodestyle/checks.rs b/src/pycodestyle/checks.rs index 58e76c611871d..8fa411f3b563c 100644 --- a/src/pycodestyle/checks.rs +++ b/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; @@ -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)); } } diff --git a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E721_E721.py.snap b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E721_E721.py.snap index 969af13bdae6c..713b30b9a81db 100644 --- a/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E721_E721.py.snap +++ b/src/pycodestyle/snapshots/ruff__pycodestyle__tests__E721_E721.py.snap @@ -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