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