From f4eeff2b4dd0b139f56ff0d407bfd16cf5ad85bb Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Tue, 6 May 2025 17:44:33 +0530 Subject: [PATCH 1/3] flag unidiomatic-typecheck for type(x) is type(y) --- pylint/checkers/base/comparison_checker.py | 11 ----------- tests/functional/u/unidiomatic_typecheck.py | 16 ++++++++-------- tests/functional/u/unidiomatic_typecheck.txt | 8 ++++++++ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/pylint/checkers/base/comparison_checker.py b/pylint/checkers/base/comparison_checker.py index a2c5b10f11..543c3bb403 100644 --- a/pylint/checkers/base/comparison_checker.py +++ b/pylint/checkers/base/comparison_checker.py @@ -338,15 +338,4 @@ def _check_type_x_is_y( ): return - if _is_one_arg_pos_call(right): - right_func = utils.safe_infer(right.func) - if ( - isinstance(right_func, nodes.ClassDef) - and right_func.qname() == TYPE_QNAME - ): - # type(x) == type(a) - right_arg = utils.safe_infer(right.args[0]) - if not isinstance(right_arg, LITERAL_NODE_TYPES): - # not e.g. type(x) == type([]) - return self.add_message("unidiomatic-typecheck", node=node) diff --git a/tests/functional/u/unidiomatic_typecheck.py b/tests/functional/u/unidiomatic_typecheck.py index 784668b8a6..a488e6dfad 100644 --- a/tests/functional/u/unidiomatic_typecheck.py +++ b/tests/functional/u/unidiomatic_typecheck.py @@ -63,14 +63,14 @@ def parameter_shadowing_inference_negatives(type): def deliberate_subclass_check_negatives(a, b): - type(42) is type(b) - type(42) is not type(b) - type(42) == type(b) - type(42) != type(b) - type(a) is type(b) - type(a) is not type(b) - type(a) == type(b) - type(a) != type(b) + type(42) is type(b) # [unidiomatic-typecheck] + type(42) is not type(b) # [unidiomatic-typecheck] + type(42) == type(b) # [unidiomatic-typecheck] + type(42) != type(b) # [unidiomatic-typecheck] + type(a) is type(b) # [unidiomatic-typecheck] + type(a) is not type(b) # [unidiomatic-typecheck] + type(a) == type(b) # [unidiomatic-typecheck] + type(a) != type(b) # [unidiomatic-typecheck] def type_of_literals_positives(a): diff --git a/tests/functional/u/unidiomatic_typecheck.txt b/tests/functional/u/unidiomatic_typecheck.txt index 76369310a6..1259c142ca 100644 --- a/tests/functional/u/unidiomatic_typecheck.txt +++ b/tests/functional/u/unidiomatic_typecheck.txt @@ -10,6 +10,14 @@ unidiomatic-typecheck:16:4:16:20:simple_inference_positives:Use isinstance() rat unidiomatic-typecheck:17:4:17:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:18:4:18:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:19:4:19:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:66:4:66:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:67:4:67:27:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:68:4:68:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:69:4:69:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:70:4:70:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:71:4:71:26:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:72:4:72:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:73:4:73:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:77:4:77:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:78:4:78:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:79:4:79:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED From ec62635b42d0b4d689616c60b0866ecd3ff2cfa6 Mon Sep 17 00:00:00 2001 From: alexprabhat99 Date: Tue, 6 May 2025 17:50:07 +0530 Subject: [PATCH 2/3] added changelog --- doc/whatsnew/fragments/10365.bugfix | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/whatsnew/fragments/10365.bugfix diff --git a/doc/whatsnew/fragments/10365.bugfix b/doc/whatsnew/fragments/10365.bugfix new file mode 100644 index 0000000000..8526b3e214 --- /dev/null +++ b/doc/whatsnew/fragments/10365.bugfix @@ -0,0 +1,3 @@ +Fixed unidiomatic-typecheck not flagged for type(x) is type(y). + +Closes #10365 \ No newline at end of file From a1e7e36d34edc4a2113c07e4b85905627915b7c4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 12:21:11 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/whatsnew/fragments/10365.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/10365.bugfix b/doc/whatsnew/fragments/10365.bugfix index 8526b3e214..d4c6621d89 100644 --- a/doc/whatsnew/fragments/10365.bugfix +++ b/doc/whatsnew/fragments/10365.bugfix @@ -1,3 +1,3 @@ Fixed unidiomatic-typecheck not flagged for type(x) is type(y). -Closes #10365 \ No newline at end of file +Closes #10365