From ecc5bdaa75abb904e492242db8668b721beb2974 Mon Sep 17 00:00:00 2001 From: Alex Chen Date: Thu, 16 Jul 2026 16:24:07 +0000 Subject: [PATCH] E721: report reversed type comparisons E721 only flagged `type(x) == T` and missed `T == type(x)` because the allow-path inspected only the first regex capture group. Always report when COMPARE_TYPE_REGEX matches so both orders are covered. Fixes #1187 Signed-off-by: Alex Chen --- CHANGES.txt | 8 ++++++++ pycodestyle.py | 6 +++--- testing/data/E72.py | 9 +++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index aef35485..86a76dba 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,6 +1,14 @@ Changelog ========= +Unreleased +---------- + +Changes: + +* E721: also report reversed comparisons like ``int == type(obj)``. + Issue #1187. + 2.14.0 (2025-06-20) ------------------- diff --git a/pycodestyle.py b/pycodestyle.py index 868e79d5..d2f0a2b2 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1498,12 +1498,12 @@ def comparison_type(logical_line, noqa): Okay: if isinstance(obj, int): Okay: if type(obj) is int: E721: if type(obj) == type(1): + E721: if type(obj) == int: + E721: if int == type(obj): """ match = COMPARE_TYPE_REGEX.search(logical_line) if match and not noqa: - inst = match.group(1) - if inst and inst.isidentifier() and inst not in SINGLETONS: - return # Allow comparison for types which are not obvious + # Flag both type(x) == T and T == type(x) (and !=). yield ( match.start(), "E721 do not compare types, for exact checks use `is` / `is not`, " diff --git a/testing/data/E72.py b/testing/data/E72.py index 5d1046cb..9b092311 100644 --- a/testing/data/E72.py +++ b/testing/data/E72.py @@ -90,3 +90,12 @@ def func_histype(a, b, c): if compute_type(foo) == 5: pass +#: E721 +if type(obj) == int: + pass +#: E721 +if int == type(obj): + pass +#: E721 +if int != type(obj): + pass