Skip to content

Commit f9773de

Browse files
jacobtylerwallsnedelcu-ioanPierre-Sassoulas
authored
Consistency between is/is not and ==/!= when comparing types for unidiomatic-typecheck (#10170) (#10366)
(cherry picked from commit d396616) Co-authored-by: Nedelcu Ioan-Andrei <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 0533111 commit f9773de

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Comparisons between two calls to `type()` won't raise an ``unidiomatic-typecheck`` warning anymore, consistent with the behavior applied only for ``==`` previously.
2+
3+
Closes #10161

pylint/checkers/base/comparison_checker.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,10 @@ def _check_unidiomatic_typecheck(self, node: nodes.Compare) -> None:
323323
if operator in TYPECHECK_COMPARISON_OPERATORS:
324324
left = node.left
325325
if _is_one_arg_pos_call(left):
326-
self._check_type_x_is_y(node, left, operator, right)
326+
self._check_type_x_is_y(node=node, left=left, right=right)
327327

328328
def _check_type_x_is_y(
329-
self,
330-
node: nodes.Compare,
331-
left: nodes.NodeNG,
332-
operator: str,
333-
right: nodes.NodeNG,
329+
self, node: nodes.Compare, left: nodes.NodeNG, right: nodes.NodeNG
334330
) -> None:
335331
"""Check for expressions like type(x) == Y."""
336332
left_func = utils.safe_infer(left.func)
@@ -339,7 +335,7 @@ def _check_type_x_is_y(
339335
):
340336
return
341337

342-
if operator in {"is", "is not"} and _is_one_arg_pos_call(right):
338+
if _is_one_arg_pos_call(right):
343339
right_func = utils.safe_infer(right.func)
344340
if (
345341
isinstance(right_func, nodes.ClassDef)

tests/functional/u/unidiomatic_typecheck.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,28 @@ def parameter_shadowing_inference_negatives(type):
5757
type(42) in [int]
5858
type(42) not in [int]
5959

60-
def deliberate_subclass_check_negatives(b):
60+
61+
def deliberate_subclass_check_negatives(a, b):
6162
type(42) is type(b)
6263
type(42) is not type(b)
64+
type(42) == type(b)
65+
type(42) != type(b)
66+
type(a) is type(b)
67+
type(a) is not type(b)
68+
type(a) == type(b)
69+
type(a) != type(b)
70+
6371

6472
def type_of_literals_positives(a):
65-
type(a) is type([]) # [unidiomatic-typecheck]
66-
type(a) is not type([]) # [unidiomatic-typecheck]
67-
type(a) is type({}) # [unidiomatic-typecheck]
68-
type(a) is not type({}) # [unidiomatic-typecheck]
69-
type(a) is type("") # [unidiomatic-typecheck]
70-
type(a) is not type("") # [unidiomatic-typecheck]
73+
type(a) is type([]) # [unidiomatic-typecheck]
74+
type(a) is not type([]) # [unidiomatic-typecheck]
75+
type(a) is type({}) # [unidiomatic-typecheck]
76+
type(a) is not type({}) # [unidiomatic-typecheck]
77+
type(a) is type("") # [unidiomatic-typecheck]
78+
type(a) is not type("") # [unidiomatic-typecheck]
79+
type(a) == type([]) # [unidiomatic-typecheck]
80+
type(a) != type([]) # [unidiomatic-typecheck]
81+
type(a) == type({}) # [unidiomatic-typecheck]
82+
type(a) != type({}) # [unidiomatic-typecheck]
83+
type(a) == type("") # [unidiomatic-typecheck]
84+
type(a) != type("") # [unidiomatic-typecheck]

tests/functional/u/unidiomatic_typecheck.txt

+12-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@ unidiomatic-typecheck:12:4:12:20:simple_inference_positives:Use isinstance() rat
66
unidiomatic-typecheck:13:4:13:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
77
unidiomatic-typecheck:14:4:14:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
88
unidiomatic-typecheck:15:4:15:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
9-
unidiomatic-typecheck:65:4:65:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
10-
unidiomatic-typecheck:66:4:66:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
11-
unidiomatic-typecheck:67:4:67:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
12-
unidiomatic-typecheck:68:4:68:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
13-
unidiomatic-typecheck:69:4:69:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
14-
unidiomatic-typecheck:70:4:70:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
9+
unidiomatic-typecheck:73:4:73:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
10+
unidiomatic-typecheck:74:4:74:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
11+
unidiomatic-typecheck:75:4:75:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
12+
unidiomatic-typecheck:76:4:76:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
13+
unidiomatic-typecheck:77:4:77:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
14+
unidiomatic-typecheck:78:4:78:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
15+
unidiomatic-typecheck:79:4:79:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
16+
unidiomatic-typecheck:80:4:80:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
17+
unidiomatic-typecheck:81:4:81:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
18+
unidiomatic-typecheck:82:4:82:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
19+
unidiomatic-typecheck:83:4:83:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
20+
unidiomatic-typecheck:84:4:84:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED

0 commit comments

Comments
 (0)