summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--pylint/checkers/utils.py17
-rw-r--r--pylint/test/functional/import_error.py9
-rw-r--r--pylint/test/functional/import_error.txt2
-rw-r--r--pylint/test/functional/member_checks.py12
-rw-r--r--pylint/test/functional/member_checks.txt14
-rw-r--r--pylint/test/functional/no_name_in_module.py6
-rw-r--r--pylint/test/functional/no_name_in_module.txt3
-rw-r--r--pylint/test/functional/undefined_variable.py4
-rw-r--r--pylint/test/functional/undefined_variable.txt2
-rw-r--r--pylint/test/unittest_checkers_utils.py8
11 files changed, 35 insertions, 48 deletions
diff --git a/ChangeLog b/ChangeLog
index b012611b7..b8ac33f73 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,8 +14,12 @@ ChangeLog for Pylint
This is a step towards making pylint more sane, as
per the discussion from issue #746.
-
+ * Don't consider bare and broad except handlers as ignoring NameError,
+ AttributeError and similar exceptions, in the context of checkers for
+ these issues.
+
+ Closes issue #826
* Catch more cases as not proper iterables for __slots__ with
regard to invalid-slots pattern. Closes issue #775.
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py
index 60ed86583..93a91f46b 100644
--- a/pylint/checkers/utils.py
+++ b/pylint/checkers/utils.py
@@ -461,6 +461,10 @@ def error_of_type(handler, error_type):
if not isinstance(error_type, tuple):
error_type = (error_type, )
expected_errors = {error.__name__ for error in error_type}
+ if not handler.type:
+ # bare except. While this indeed catches anything, if the desired errors
+ # aren't specified directly, then we just ignore it.
+ return False
return handler.catch(expected_errors)
@@ -553,23 +557,16 @@ def unimplemented_abstract_methods(node, is_abstract_cb=None):
def node_ignores_exception(node, exception):
- """Check if the node is in a TryExcept which handles the given exception.
-
- This will also return ``True`` if the node is protected by an `except Exception`
- or by a bare except clause.
- """
+ """Check if the node is in a TryExcept which handles the given exception."""
current = node
ignores = (astroid.ExceptHandler, astroid.TryExcept)
while current and not isinstance(current.parent, ignores):
current = current.parent
func = functools.partial(error_of_type,
- error_type=(Exception, exception))
+ error_type=(exception, ))
if current and isinstance(current.parent, astroid.TryExcept):
- handles_errors = any(map(func, current.parent.handlers))
- empty_handlers = any(handler.type is None
- for handler in current.parent.handlers)
- if handles_errors or empty_handlers:
+ if any(map(func, current.parent.handlers)):
return True
return False
diff --git a/pylint/test/functional/import_error.py b/pylint/test/functional/import_error.py
index b71537761..de72e2fcd 100644
--- a/pylint/test/functional/import_error.py
+++ b/pylint/test/functional/import_error.py
@@ -17,15 +17,6 @@ try:
except ValueError:
maybe_missing_2 = None
-try:
- import dont_emit
-except Exception:
- pass
-
-try:
- import please_dont_emit
-except:
- pass
try:
if maybe_missing:
diff --git a/pylint/test/functional/import_error.txt b/pylint/test/functional/import_error.txt
index 82ca0d4e2..b8746e9d1 100644
--- a/pylint/test/functional/import_error.txt
+++ b/pylint/test/functional/import_error.txt
@@ -1,3 +1,3 @@
import-error:3::"Unable to import 'totally_missing'"
import-error:16::"Unable to import 'maybe_missing_2'"
-import-error:36::"Unable to import 'functional.collections'"
+import-error:27::"Unable to import 'functional.collections'"
diff --git a/pylint/test/functional/member_checks.py b/pylint/test/functional/member_checks.py
index 50a0d97c1..a998c5601 100644
--- a/pylint/test/functional/member_checks.py
+++ b/pylint/test/functional/member_checks.py
@@ -94,16 +94,6 @@ except AttributeError:
pass
try:
- Client().indeed()
-except Exception:
- pass
-
-try:
- Client().indeed()
-except:
- pass
-
-try:
Client().indeed() # [no-member]
except ImportError:
pass
@@ -131,7 +121,7 @@ try:
Client().indeed()
except AttributeError:
try:
- Client.missing()
+ Client.missing() # [no-member]
except Exception:
pass
diff --git a/pylint/test/functional/member_checks.txt b/pylint/test/functional/member_checks.txt
index 6e527bc45..82c54bc22 100644
--- a/pylint/test/functional/member_checks.txt
+++ b/pylint/test/functional/member_checks.txt
@@ -9,10 +9,10 @@ no-member:59:Client.test_bt_types:Instance of 'int' has no 'whatever' member:INF
no-member:65:Client.test_no_false_positives:Super of 'Client' has no 'misssing' member:INFERENCE
no-member:86::Instance of 'int' has no 'lower' member:INFERENCE_FAILURE
no-member:97::Instance of 'Client' has no 'indeed' member:INFERENCE
-no-member:107::Instance of 'Client' has no 'indeed' member:INFERENCE
-no-member:114::Class 'Client' has no 'missing' member:INFERENCE
-no-member:120::Class 'Client' has no 'missing' member:INFERENCE
-no-member:144::Class 'Client' has no 'ala' member:INFERENCE
-no-member:145::Class 'dict' has no 'bala' member:INFERENCE
-no-member:146::Class 'str' has no 'portocala' member:INFERENCE
-no-member:181:NoDunderNameInInstance.__init__:Instance of 'NoDunderNameInInstance' has no '__name__' member:INFERENCE \ No newline at end of file
+no-member:104::Class 'Client' has no 'missing' member:INFERENCE
+no-member:110::Class 'Client' has no 'missing' member:INFERENCE
+no-member:124::Class 'Client' has no 'missing' member:INFERENCE
+no-member:134::Class 'Client' has no 'ala' member:INFERENCE
+no-member:135::Class 'dict' has no 'bala' member:INFERENCE
+no-member:136::Class 'str' has no 'portocala' member:INFERENCE
+no-member:171:NoDunderNameInInstance.__init__:Instance of 'NoDunderNameInInstance' has no '__name__' member:INFERENCE \ No newline at end of file
diff --git a/pylint/test/functional/no_name_in_module.py b/pylint/test/functional/no_name_in_module.py
index 712611fd5..44bb04d37 100644
--- a/pylint/test/functional/no_name_in_module.py
+++ b/pylint/test/functional/no_name_in_module.py
@@ -46,18 +46,18 @@ except ValueError:
pass
try:
- import collections.dont_emit
+ import collections.emit # [no-name-in-module]
except Exception:
pass
try:
- import collections.please_dont_emit
+ import collections.emit1 # [no-name-in-module]
except:
pass
try:
if something:
- import collections.please_dont_emit
+ import collections.emit2 # [no-name-in-module]
except Exception:
pass
diff --git a/pylint/test/functional/no_name_in_module.txt b/pylint/test/functional/no_name_in_module.txt
index 72c4acb25..ad4b7bfce 100644
--- a/pylint/test/functional/no_name_in_module.txt
+++ b/pylint/test/functional/no_name_in_module.txt
@@ -9,5 +9,8 @@ no-name-in-module:23::No name 'findiiter' in module 're'
pointless-statement:26::Statement seems to have no effect
no-name-in-module:34::No name 'anything' in module 'collections'
no-name-in-module:44::No name 'indeed_missing' in module 'collections'
+no-name-in-module:49::No name 'emit' in module 'collections'
+no-name-in-module:54::No name 'emit1' in module 'collections'
+no-name-in-module:60::No name 'emit2' in module 'collections'
no-name-in-module:65::No name 'lala' in module 'functional.no_self_use'
no-name-in-module:66::No name 'bla' in module 'functional.no_self_use'
diff --git a/pylint/test/functional/undefined_variable.py b/pylint/test/functional/undefined_variable.py
index 44a06529f..d55cd3ba8 100644
--- a/pylint/test/functional/undefined_variable.py
+++ b/pylint/test/functional/undefined_variable.py
@@ -158,12 +158,12 @@ except NameError:
pass
try:
- unicode_2
+ unicode_2 # [undefined-variable]
except Exception:
pass
try:
- unicode_3
+ unicode_3 # [undefined-variable]
except:
pass
diff --git a/pylint/test/functional/undefined_variable.txt b/pylint/test/functional/undefined_variable.txt
index aaf7275d1..98c6bbedc 100644
--- a/pylint/test/functional/undefined_variable.txt
+++ b/pylint/test/functional/undefined_variable.txt
@@ -19,4 +19,6 @@ undefined-variable:130::Undefined variable 'BAT'
used-before-assignment:141:KeywordArgument.test1:Using variable 'enabled' before assignment
undefined-variable:144:KeywordArgument.test2:Undefined variable 'disabled'
undefined-variable:149:KeywordArgument.<lambda>:Undefined variable 'arg'
+undefined-variable:161::Undefined variable 'unicode_2'
+undefined-variable:166::Undefined variable 'unicode_3'
undefined-variable:171::Undefined variable 'unicode_4' \ No newline at end of file
diff --git a/pylint/test/unittest_checkers_utils.py b/pylint/test/unittest_checkers_utils.py
index 83ba5583f..cdc847af7 100644
--- a/pylint/test/unittest_checkers_utils.py
+++ b/pylint/test/unittest_checkers_utils.py
@@ -71,7 +71,7 @@ class UtilsTC(unittest.TestCase):
self.assertTrue(utils.error_of_type(nodes[0], (AttributeError, )))
self.assertFalse(utils.error_of_type(nodes[0], Exception))
self.assertTrue(utils.error_of_type(nodes[1], Exception))
- self.assertTrue(utils.error_of_type(nodes[2], ImportError))
+ self.assertFalse(utils.error_of_type(nodes[2], ImportError))
def test_node_ignores_exception(self):
nodes = test_utils.extract_node("""
@@ -84,7 +84,7 @@ class UtilsTC(unittest.TestCase):
except Exception:
pass
try:
- 1/0 #@
+ 2/0 #@
except:
pass
try:
@@ -93,8 +93,8 @@ class UtilsTC(unittest.TestCase):
pass
""")
self.assertTrue(utils.node_ignores_exception(nodes[0], ZeroDivisionError))
- self.assertTrue(utils.node_ignores_exception(nodes[1], ZeroDivisionError))
- self.assertTrue(utils.node_ignores_exception(nodes[2], ZeroDivisionError))
+ self.assertFalse(utils.node_ignores_exception(nodes[1], ZeroDivisionError))
+ self.assertFalse(utils.node_ignores_exception(nodes[2], ZeroDivisionError))
self.assertFalse(utils.node_ignores_exception(nodes[3], ZeroDivisionError))