Skip to content

Commit f30417c

Browse files
NedelcuPierre-Sassoulas
Nedelcu
authored andcommitted
(bugfix) Issue 10161: Incorrect scenario of unidiomatic-typecheck.
1 parent 77481a6 commit f30417c

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a false positive for `unidiomatic-typecheck` when comparing two direct types.
2+
3+
Closes #10161

pylint/checkers/base/comparison_checker.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,12 @@ def _check_type_x_is_y(
343343
):
344344
return
345345

346-
if operator in {"is", "is not"} and _is_one_arg_pos_call(right):
346+
if operator in {"!=", "==", "is", "is not"} and _is_one_arg_pos_call(right):
347347
right_func = utils.safe_infer(right.func)
348348
if (
349349
isinstance(right_func, nodes.ClassDef)
350350
and right_func.qname() == TYPE_QNAME
351351
):
352352
# type(x) == type(a)
353-
right_arg = utils.safe_infer(right.args[0])
354-
if not isinstance(right_arg, LITERAL_NODE_TYPES):
355-
# not e.g. type(x) == type([])
356-
return
353+
return
357354
self.add_message("unidiomatic-typecheck", node=node)

tests/functional/u/unidiomatic_typecheck.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,22 @@ def deliberate_subclass_check_negatives(b):
6565
type(42) is type(b)
6666
type(42) is not type(b)
6767

68-
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]
68+
def type_of_literals_negatives(a):
69+
type(a) is type([])
70+
type(a) is not type([])
71+
type(a) is type({})
72+
type(a) is not type({})
73+
type(a) is type("")
74+
type(a) is not type("")
75+
type(a) == type([])
76+
type(a) != type([])
77+
type(a) == type({})
78+
type(a) != type({})
79+
type(a) == type("")
80+
type(a) != type("")
81+
82+
def double_type_check_negatives(a, b):
83+
type(a) == type(b)
84+
type(a) != type(b)
85+
type(a) is type(b)
86+
type(a) is not type(b)

tests/functional/u/unidiomatic_typecheck.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ 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

0 commit comments

Comments
 (0)