Skip to content
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

[ADD] except-pass: Emit message If a except:pass is used (OCA/#252) #107

Merged
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
15 changes: 15 additions & 0 deletions pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@
'missing-manifest-dependency',
settings.DESC_DFLT
),
'W%d38' % settings.BASE_OMODULE_ID: (
'pass into block except. '
'If you really need to use the pass consider logging that exception',
'except-pass',
settings.DESC_DFLT
),
'W%d37' % settings.BASE_OMODULE_ID: (
'%s The xml attribute is missing the translation="off" tag %s',
'xml-attribute-translatable',
Expand Down Expand Up @@ -351,6 +357,15 @@ def visit_import(self, node):
if isinstance(node.scope(), astroid.Module):
self._check_imported_packages(node, name)

@utils.check_messages('except-pass')
def visit_tryexcept(self, node):
"""Visit block try except"""
for handler in node.handlers:
Copy link
Collaborator

@moylop260 moylop260 Jan 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you could validate if the except has a as in order to skip the check because the new variable could be used.
It this variable is not used then we will have a variable-unused from other check

This is the easiest way to skip the check using the @lasley recommendation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moylop260 @lasley
Done, Now we exclude if the exception is assigned to one variable

if (not handler.name and
len(handler.body) == 1 and
isinstance(handler.body[0], astroid.node_classes.Pass)):
self.add_message('except-pass', node=handler)

def _check_rst_syntax_error(self):
"""Check if rst file there is syntax error
:return: False if exists errors and
Expand Down
1 change: 1 addition & 0 deletions pylint_odoo/test/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
'wrong-tabs-instead-of-spaces': 2,
'eval-referenced': 5,
'xml-syntax-error': 2,
'except-pass': 3,
'attribute-string-redundant': 33,
'renamed-field-parameter': 2,
'xml-attribute-translatable': 1,
Expand Down
61 changes: 61 additions & 0 deletions pylint_odoo/test_repo/test_module/except_pass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# -*- coding: utf-8 -*-
"""Test Except Pass usage"""


class TestExceptPass(object):
"""Test Except Pass class """

def test_method(self):
try:
raise Exception('Exception')
except Exception: # except-pass
pass

def test_2_method(self):
"""This pass is skip for body of except has more than one line """
try:
raise Exception('Exception')
except Exception:
pass
print('Exception')

def test_3_method(self):
"""This pass is skip for the exception is assigned"""
try:
raise Exception('Exception')
except Exception as exception:
pass
if exception:
pass

def test_4_method(self):
try:
raise Exception('Exception')
except Exception, userError:
pass
if userError:
pass

def test_5_method(self):
try:
raise Exception('Exception')
except (Exception, IndexError) as exception:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the case where there are many Exceptions but without alias except (Exception, IndexError):

pass
if exception:
pass

def test_6_method(self):
try:
raise Exception('Exception')
except (Exception, IndexError): # except-pass
pass

def test_7_method(self):
try:
raise Exception('Exception')
except (Exception, IndexError, NameError), exception:
pass
except Exception: # except-pass
pass
if exception:
pass