From 861c9a40fda1bf5094dc4229d0b00ec7e9ac8765 Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Sun, 12 Dec 2021 08:28:59 +0000 Subject: Add a failing test for a custom types module case Any `types.*Type*` matches incorrectly as a `type(...)` comparison; the regex `COMPARE_TYPE_REGEX` seems a bit too complicated for what should be a simple comparison case. Ref: https://github.com/PyCQA/pycodestyle/blob/main/pycodestyle.py#L147-L148 This reproduces the case in #830 --- testsuite/E72.py | 5 +++++ testsuite/custom_types.py | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 testsuite/custom_types.py diff --git a/testsuite/E72.py b/testsuite/E72.py index a60d892..e789d82 100644 --- a/testsuite/E72.py +++ b/testsuite/E72.py @@ -80,3 +80,8 @@ try: pass except Exception: pass +#: Okay +from . import custom_types as types + +red = types.ColorTypeRED +red is types.ColorType.RED diff --git a/testsuite/custom_types.py b/testsuite/custom_types.py new file mode 100644 index 0000000..43daf02 --- /dev/null +++ b/testsuite/custom_types.py @@ -0,0 +1,7 @@ +import enum + + +class ColorType(enum.Enum): + RED = 1 + GREEN = 2 + BLUE = 3 -- cgit v1.2.1 From f3ee72b75a4df32221ab08d315e0614a59c17f26 Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Sun, 12 Dec 2021 09:16:36 +0000 Subject: Correct regex to only match type(...) comparisons --- pycodestyle.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index b839e35..c1ec98d 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -144,8 +144,10 @@ COMPARE_SINGLETON_REGEX = re.compile(r'(\bNone|\bFalse|\bTrue)?\s*([=!]=)' r'\s*(?(1)|(None|False|True))\b') COMPARE_NEGATIVE_REGEX = re.compile(r'\b(?%&^]+|:=)(\s*)') LAMBDA_REGEX = re.compile(r'\blambda\b') -- cgit v1.2.1 From dcedb98f17514cc3174313fd0f009d460fa7babb Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Sun, 12 Dec 2021 09:24:39 +0000 Subject: Correct test assertions for E721 * `type(a) is type(b)` should still fail * same for `type(a) != type(b) or type(a) == type(ccc)` * We cannot assume `res == types.IntType` is wrong as the identity of the objects is not known at check time, either way it shouldn't be a E721 as it doesn't involve type(...) function as described in PEP8 --- pycodestyle.py | 1 - testsuite/E72.py | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index c1ec98d..c71071e 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1465,7 +1465,6 @@ def comparison_type(logical_line, noqa): common base class, basestring, so you can do: Okay: if isinstance(obj, basestring): - Okay: if type(a1) is type(b1): """ match = COMPARE_TYPE_REGEX.search(logical_line) if match and not noqa: diff --git a/testsuite/E72.py b/testsuite/E72.py index e789d82..bba55f5 100644 --- a/testsuite/E72.py +++ b/testsuite/E72.py @@ -4,7 +4,7 @@ if type(res) == type(42): #: E721 if type(res) != type(""): pass -#: E721 +#: Okay import types if res == types.IntType: @@ -47,8 +47,6 @@ if isinstance(res, str): pass if isinstance(res, types.MethodType): pass -if type(a) != type(b) or type(a) == type(ccc): - pass #: Okay def func_histype(a, b, c): pass -- cgit v1.2.1 From 5df259bf8d602168e5f29d4bc26ac6b34798e83b Mon Sep 17 00:00:00 2001 From: Pavel Savchenko Date: Mon, 13 Dec 2021 09:14:40 +0000 Subject: Remove unused module --- testsuite/custom_types.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 testsuite/custom_types.py diff --git a/testsuite/custom_types.py b/testsuite/custom_types.py deleted file mode 100644 index 43daf02..0000000 --- a/testsuite/custom_types.py +++ /dev/null @@ -1,7 +0,0 @@ -import enum - - -class ColorType(enum.Enum): - RED = 1 - GREEN = 2 - BLUE = 3 -- cgit v1.2.1