Skip to content

Commit

Permalink
Make unidiomatic-typecheck check right-hand side too (#10225)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprabhat99 authored Feb 10, 2025
1 parent cf13baf commit a475485
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10217.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed unidiomatic-typecheck only checking left-hand side.

Closes #10217
4 changes: 4 additions & 0 deletions pylint/checkers/base/comparison_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ def _check_unidiomatic_typecheck(self, node: nodes.Compare) -> None:
left = node.left
if _is_one_arg_pos_call(left):
self._check_type_x_is_y(node, left, operator, right)
elif isinstance(left, nodes.Name) and _is_one_arg_pos_call(right):
self._check_type_x_is_y(
node=node, left=right, operator=operator, right=left
) # transforming Y == type(x) case to type(x) == Y

def _check_type_x_is_y(
self,
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/u/unidiomatic_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def simple_positives():
type(42) is not int # [unidiomatic-typecheck]
type(42) == int # [unidiomatic-typecheck]
type(42) != int # [unidiomatic-typecheck]
int is type(42) # [unidiomatic-typecheck]
int is not type(42) # [unidiomatic-typecheck]
int == type(42) # [unidiomatic-typecheck]
int != type(42) # [unidiomatic-typecheck]

def simple_inference_positives():
alias = type
Expand Down
20 changes: 12 additions & 8 deletions tests/functional/u/unidiomatic_typecheck.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ unidiomatic-typecheck:5:4:5:19:simple_positives:Use isinstance() rather than typ
unidiomatic-typecheck:6:4:6:23:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:7:4:7:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:8:4:8:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:12:4:12:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
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:9:4:9:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:10:4:10:23:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:11:4:11:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:12:4:12:19:simple_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:16:4:16:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:17:4:17:24:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:18:4:18:20:simple_inference_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:19:4:19:20:simple_inference_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:71:4:71:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:72:4:72: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

0 comments on commit a475485

Please sign in to comment.