Skip to content

Commit a475485

Browse files
Make unidiomatic-typecheck check right-hand side too (#10225)
1 parent cf13baf commit a475485

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

doc/whatsnew/fragments/10217.bugfix

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed unidiomatic-typecheck only checking left-hand side.
2+
3+
Closes #10217

pylint/checkers/base/comparison_checker.py

+4
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ def _check_unidiomatic_typecheck(self, node: nodes.Compare) -> None:
324324
left = node.left
325325
if _is_one_arg_pos_call(left):
326326
self._check_type_x_is_y(node, left, operator, right)
327+
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
327331

328332
def _check_type_x_is_y(
329333
self,

tests/functional/u/unidiomatic_typecheck.py

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ def simple_positives():
66
type(42) is not int # [unidiomatic-typecheck]
77
type(42) == int # [unidiomatic-typecheck]
88
type(42) != int # [unidiomatic-typecheck]
9+
int is type(42) # [unidiomatic-typecheck]
10+
int is not type(42) # [unidiomatic-typecheck]
11+
int == type(42) # [unidiomatic-typecheck]
12+
int != type(42) # [unidiomatic-typecheck]
913

1014
def simple_inference_positives():
1115
alias = type

tests/functional/u/unidiomatic_typecheck.txt

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ unidiomatic-typecheck:5:4:5:19:simple_positives:Use isinstance() rather than typ
22
unidiomatic-typecheck:6:4:6:23:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
33
unidiomatic-typecheck:7:4:7:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
44
unidiomatic-typecheck:8:4:8:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
5-
unidiomatic-typecheck:12:4:12:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
6-
unidiomatic-typecheck:13:4:13:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
7-
unidiomatic-typecheck:14:4:14:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
8-
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
5+
unidiomatic-typecheck:9:4:9:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
6+
unidiomatic-typecheck:10:4:10:23:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
7+
unidiomatic-typecheck:11:4:11:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
8+
unidiomatic-typecheck:12:4:12:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
9+
unidiomatic-typecheck:16:4:16:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
10+
unidiomatic-typecheck:17:4:17:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
11+
unidiomatic-typecheck:18:4:18:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
12+
unidiomatic-typecheck:19:4:19:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
1313
unidiomatic-typecheck:69:4:69:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
1414
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)