diff --git a/doc/whatsnew/fragments/10161.false_positive b/doc/whatsnew/fragments/10161.false_positive new file mode 100644 index 0000000000..dc343e05ae --- /dev/null +++ b/doc/whatsnew/fragments/10161.false_positive @@ -0,0 +1,3 @@ +Comparisons between two calls to `type()` won't raise an ``unidiomatic-typecheck`` warning anymore, consistent with the behavior applied only for ``==`` previously. + +Closes #10161 diff --git a/pylint/checkers/base/comparison_checker.py b/pylint/checkers/base/comparison_checker.py index 6fb053e2e1..091c8e9db3 100644 --- a/pylint/checkers/base/comparison_checker.py +++ b/pylint/checkers/base/comparison_checker.py @@ -323,14 +323,10 @@ def _check_unidiomatic_typecheck(self, node: nodes.Compare) -> None: if operator in TYPECHECK_COMPARISON_OPERATORS: left = node.left if _is_one_arg_pos_call(left): - self._check_type_x_is_y(node, left, operator, right) + self._check_type_x_is_y(node=node, left=left, right=right) def _check_type_x_is_y( - self, - node: nodes.Compare, - left: nodes.NodeNG, - operator: str, - right: nodes.NodeNG, + self, node: nodes.Compare, left: nodes.NodeNG, right: nodes.NodeNG ) -> None: """Check for expressions like type(x) == Y.""" left_func = utils.safe_infer(left.func) @@ -339,7 +335,7 @@ def _check_type_x_is_y( ): return - if operator in {"is", "is not"} and _is_one_arg_pos_call(right): + if _is_one_arg_pos_call(right): right_func = utils.safe_infer(right.func) if ( isinstance(right_func, nodes.ClassDef) diff --git a/tests/functional/u/unidiomatic_typecheck.py b/tests/functional/u/unidiomatic_typecheck.py index 2a1957d75e..5e96a2745a 100644 --- a/tests/functional/u/unidiomatic_typecheck.py +++ b/tests/functional/u/unidiomatic_typecheck.py @@ -57,14 +57,28 @@ def parameter_shadowing_inference_negatives(type): type(42) in [int] type(42) not in [int] -def deliberate_subclass_check_negatives(b): + +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) + def type_of_literals_positives(a): - type(a) is type([]) # [unidiomatic-typecheck] - type(a) is not type([]) # [unidiomatic-typecheck] - type(a) is type({}) # [unidiomatic-typecheck] - type(a) is not type({}) # [unidiomatic-typecheck] - type(a) is type("") # [unidiomatic-typecheck] - type(a) is not type("") # [unidiomatic-typecheck] + type(a) is type([]) # [unidiomatic-typecheck] + type(a) is not type([]) # [unidiomatic-typecheck] + type(a) is type({}) # [unidiomatic-typecheck] + type(a) is not type({}) # [unidiomatic-typecheck] + type(a) is type("") # [unidiomatic-typecheck] + type(a) is not type("") # [unidiomatic-typecheck] + type(a) == type([]) # [unidiomatic-typecheck] + type(a) != type([]) # [unidiomatic-typecheck] + type(a) == type({}) # [unidiomatic-typecheck] + type(a) != type({}) # [unidiomatic-typecheck] + type(a) == type("") # [unidiomatic-typecheck] + type(a) != type("") # [unidiomatic-typecheck] diff --git a/tests/functional/u/unidiomatic_typecheck.txt b/tests/functional/u/unidiomatic_typecheck.txt index 84f5021d96..cc7e1902cc 100644 --- a/tests/functional/u/unidiomatic_typecheck.txt +++ b/tests/functional/u/unidiomatic_typecheck.txt @@ -6,9 +6,15 @@ unidiomatic-typecheck:12:4:12:20:simple_inference_positives:Use isinstance() rat unidiomatic-typecheck:13:4:13:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:14:4:14:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED unidiomatic-typecheck:15:4:15:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:65:4:65:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:66:4:66:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:67:4:67:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:68:4:68:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:69:4:69:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED -unidiomatic-typecheck:70:4:70:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:73:4:73:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:74:4:74:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:75:4:75:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:76:4:76:27:type_of_literals_positives: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 +unidiomatic-typecheck:80:4:80:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:81:4:81:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:82:4:82:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:83:4:83:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED +unidiomatic-typecheck:84:4:84:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED