Skip to content

Commit d396616

Browse files
authored
Consistency between is/is not and ==/!= when comparing types for unidiomatic-typecheck (#10170)
1 parent 1812899 commit d396616

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed
Lines changed: 3 additions & 0 deletions
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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,13 @@ 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
elif isinstance(left, nodes.Name) and _is_one_arg_pos_call(right):
328-
self._check_type_x_is_y(
329-
node=node, left=right, operator=operator, right=left
330-
) # transforming Y == type(x) case to type(x) == Y
328+
# transforming Y == type(x) case to type(x) == Y
329+
self._check_type_x_is_y(node=node, left=right, right=left)
331330

332331
def _check_type_x_is_y(
333-
self,
334-
node: nodes.Compare,
335-
left: nodes.NodeNG,
336-
operator: str,
337-
right: nodes.NodeNG,
332+
self, node: nodes.Compare, left: nodes.NodeNG, right: nodes.NodeNG
338333
) -> None:
339334
"""Check for expressions like type(x) == Y."""
340335
left_func = utils.safe_infer(left.func)
@@ -343,7 +338,7 @@ def _check_type_x_is_y(
343338
):
344339
return
345340

346-
if operator in {"is", "is not"} and _is_one_arg_pos_call(right):
341+
if _is_one_arg_pos_call(right):
347342
right_func = utils.safe_infer(right.func)
348343
if (
349344
isinstance(right_func, nodes.ClassDef)

tests/functional/u/unidiomatic_typecheck.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,28 @@ def parameter_shadowing_inference_negatives(type):
6161
type(42) in [int]
6262
type(42) not in [int]
6363

64-
def deliberate_subclass_check_negatives(b):
64+
65+
def deliberate_subclass_check_negatives(a, b):
6566
type(42) is type(b)
6667
type(42) is not type(b)
68+
type(42) == type(b)
69+
type(42) != type(b)
70+
type(a) is type(b)
71+
type(a) is not type(b)
72+
type(a) == type(b)
73+
type(a) != type(b)
74+
6775

6876
def type_of_literals_positives(a):
69-
type(a) is type([]) # [unidiomatic-typecheck]
70-
type(a) is not type([]) # [unidiomatic-typecheck]
71-
type(a) is type({}) # [unidiomatic-typecheck]
72-
type(a) is not type({}) # [unidiomatic-typecheck]
73-
type(a) is type("") # [unidiomatic-typecheck]
74-
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) is type({}) # [unidiomatic-typecheck]
80+
type(a) is not type({}) # [unidiomatic-typecheck]
81+
type(a) is type("") # [unidiomatic-typecheck]
82+
type(a) is not type("") # [unidiomatic-typecheck]
83+
type(a) == type([]) # [unidiomatic-typecheck]
84+
type(a) != type([]) # [unidiomatic-typecheck]
85+
type(a) == type({}) # [unidiomatic-typecheck]
86+
type(a) != type({}) # [unidiomatic-typecheck]
87+
type(a) == type("") # [unidiomatic-typecheck]
88+
type(a) != type("") # [unidiomatic-typecheck]

tests/functional/u/unidiomatic_typecheck.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ unidiomatic-typecheck:16:4:16:20:simple_inference_positives:Use isinstance() rat
1010
unidiomatic-typecheck:17:4:17:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
1111
unidiomatic-typecheck:18:4:18:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
1212
unidiomatic-typecheck:19:4:19:20:simple_inference_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
15-
unidiomatic-typecheck:71:4:71:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
16-
unidiomatic-typecheck:72:4:72:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
17-
unidiomatic-typecheck:73:4:73:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
18-
unidiomatic-typecheck:74:4:74: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:27: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:27: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
21+
unidiomatic-typecheck:85:4:85:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
22+
unidiomatic-typecheck:86:4:86:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
23+
unidiomatic-typecheck:87:4:87:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
24+
unidiomatic-typecheck:88:4:88:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED

0 commit comments

Comments
 (0)