Skip to content

Flag unidiomatic-typecheck for type(x) is type(y) #10372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10365.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed unidiomatic-typecheck not flagged for type(x) is type(y).

Closes #10365
11 changes: 0 additions & 11 deletions pylint/checkers/base/comparison_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,4 @@ def _check_type_x_is_y(
):
return

if _is_one_arg_pos_call(right):
right_func = utils.safe_infer(right.func)
if (
isinstance(right_func, nodes.ClassDef)
and right_func.qname() == TYPE_QNAME
):
# type(x) == type(a)
right_arg = utils.safe_infer(right.args[0])
if not isinstance(right_arg, LITERAL_NODE_TYPES):
# not e.g. type(x) == type([])
return
self.add_message("unidiomatic-typecheck", node=node)
16 changes: 8 additions & 8 deletions tests/functional/u/unidiomatic_typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def parameter_shadowing_inference_negatives(type):


def deliberate_subclass_check_negatives(a, b):
type(42) is type(b)
type(42) is not type(b)
type(42) == type(b)
type(42) != type(b)
type(a) is type(b)
type(a) is not type(b)
type(a) == type(b)
type(a) != type(b)
type(42) is type(b) # [unidiomatic-typecheck]
type(42) is not type(b) # [unidiomatic-typecheck]
type(42) == type(b) # [unidiomatic-typecheck]
type(42) != type(b) # [unidiomatic-typecheck]
type(a) is type(b) # [unidiomatic-typecheck]
type(a) is not type(b) # [unidiomatic-typecheck]
type(a) == type(b) # [unidiomatic-typecheck]
type(a) != type(b) # [unidiomatic-typecheck]


def type_of_literals_positives(a):
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/u/unidiomatic_typecheck.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ unidiomatic-typecheck:16:4:16:20:simple_inference_positives:Use isinstance() rat
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:66:4:66:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:67:4:67:27:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:68:4:68:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:69:4:69:23:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:70:4:70:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:71:4:71:26:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:72:4:72:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:73:4:73:22:deliberate_subclass_check_negatives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:77:4:77:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:78:4:78:27:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
unidiomatic-typecheck:79:4:79:23:type_of_literals_positives:Use isinstance() rather than type() for a typecheck.:UNDEFINED
Expand Down