Skip to content

Commit

Permalink
Allow isinstance-second-argument-not-valid-type to catch more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
PCManticore committed Feb 13, 2020
1 parent 88895ea commit 0504213
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
5 changes: 2 additions & 3 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ What's New in Pylint 2.5.0?

Release date: TBA

* Add a check for cases where the second argument to `isinstance`
is not a type.
* Add a check for cases where the second argument to `isinstance` is not a type.

Close #3308
Close #3308

* Add 'notes-rgx' option, to be used for fixme check.

Expand Down
24 changes: 14 additions & 10 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,19 @@ def _is_c_extension(module_node):
)


def _is_invalid_isinstance_type(arg):
# Return True if we are sure that arg is not a type
inferred = utils.safe_infer(arg)
if not inferred:
# Cannot infer it so skip it.
return False
if isinstance(inferred, astroid.Tuple):
return any(_is_invalid_isinstance_type(elt) for elt in inferred.elts)
if isinstance(inferred, astroid.ClassDef):
return False
return True


class TypeChecker(BaseChecker):
"""try to find bugs in the code using type inference
"""
Expand Down Expand Up @@ -1177,17 +1190,8 @@ def _check_isinstance_args(self, node):
# isinstance called with wrong number of args
return

def is_not_type(arg):
# Return True if we are sure that arg is not a type
if isinstance(utils.safe_infer(arg), astroid.FunctionDef):
return True
if isinstance(arg, astroid.Tuple):
return any([is_not_type(elt) for elt in arg.elts])

return False

second_arg = node.args[1]
if is_not_type(second_arg):
if _is_invalid_isinstance_type(second_arg):
self.add_message("isinstance-second-argument-not-valid-type", node=node)

# pylint: disable=too-many-branches,too-many-locals
Expand Down
1 change: 1 addition & 0 deletions tests/functional/i/isinstance_second_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ class B(A):
isinstance(64, hex) # [isinstance-second-argument-not-valid-type]
isinstance({b: 100}, (hash, dict)) # [isinstance-second-argument-not-valid-type]
isinstance("string", ((dict, iter), str, (int, bool))) # [isinstance-second-argument-not-valid-type]
isinstance(int, 1) # [isinstance-second-argument-not-valid-type]
1 change: 1 addition & 0 deletions tests/functional/i/isinstance_second_argument.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ isinstance-second-argument-not-valid-type:25::Second argument of isinstance is n
isinstance-second-argument-not-valid-type:26::Second argument of isinstance is not a type
isinstance-second-argument-not-valid-type:27::Second argument of isinstance is not a type
isinstance-second-argument-not-valid-type:28::Second argument of isinstance is not a type
isinstance-second-argument-not-valid-type:29::Second argument of isinstance is not a type

0 comments on commit 0504213

Please sign in to comment.