Skip to content

Commit

Permalink
Pick the latest value from the inferred values when looking for ``rai…
Browse files Browse the repository at this point in the history
…sing-non-exception``

Close #2431
  • Loading branch information
PCManticore committed Aug 22, 2018
1 parent cbc70dc commit 0ba4519
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ What's New in Pylint 2.2?

Release date: TBA

* Pick the latest value from the inferred values when looking for ``raising-non-exception``

Close #2431

* Extend the TYPE_CHECKING guard to TYPE_CHECKING name as well, not just the attribute

Close #2411
Expand Down
7 changes: 7 additions & 0 deletions doc/whatsnew/2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ Other Changes
* Fix false positive ``undefined-variable`` and ``used-before-assignment`` with nonlocal keyword usage.

* Fix exceptions being raised when one of the params is not a ClassDef for :func:`checkers.utils.is_subclass_of`.

* ``pylint`` now picks the latest value from the inferred values of the exception that gets
raised, when looking for ``raising-non-exception``. This helps when reusing a variable name
for multiple types, since ``pylint`` was picking just the first inferred value, leading
to spurious false positives.

Close #2431
14 changes: 7 additions & 7 deletions pylint/checkers/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,15 @@ def visit_raise(self, node):
self._check_bad_exception_context(node)

expr = node.exc
try:
inferred_value = next(expr.infer())
except astroid.InferenceError:
inferred_value = None

ExceptionRaiseRefVisitor(self, node).visit(expr)

if inferred_value:
ExceptionRaiseLeafVisitor(self, node).visit(inferred_value)
try:
inferred_value = expr.inferred()[-1]
except astroid.InferenceError:
pass
else:
if inferred_value:
ExceptionRaiseLeafVisitor(self, node).visit(inferred_value)

def _check_misplaced_bare_raise(self, node):
# Filter out if it's present in __exit__.
Expand Down
14 changes: 14 additions & 0 deletions pylint/test/functional/invalid_exceptions_raised.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,17 @@ def exception_instance_regression():
int("9a")
except ValueError as exc:
raise exc


def reusing_same_name_picks_the_latest_raised_value():
class Error(Exception):
"""some error"""

exceptions = tuple([ValueError, TypeError])
try:
raise ValueError
except exceptions as exc: # pylint: disable=catching-non-exception
# https://github.com/PyCQA/pylint/issues/1756
exc = Error(exc)
if exc:
raise exc

0 comments on commit 0ba4519

Please sign in to comment.