-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
[ADD] except-pass: Emit message If a except:pass is used (OCA/#252) #107
Conversation
@moylop260 |
pylint_odoo/checkers/modules_odoo.py
Outdated
def visit_tryexcept(self, node): | ||
"""Visit block try except""" | ||
for handler in node.handlers: | ||
if handler.body: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use and
instead of nested if
(in order to reduce mccabe rate)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Thanks!
pylint_odoo/checkers/modules_odoo.py
Outdated
def visit_tryexcept(self, node): | ||
"""Visit block try except""" | ||
for handler in node.handlers: | ||
if handler.body and isinstance(handler.body[0], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And len(handler.body) == 1
because we need validate just that unique sentence.
try:
code
except:
pass
logger # this case is valid, is other error but is valid for this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Done!
pylint_odoo/checkers/modules_odoo.py
Outdated
def visit_tryexcept(self, node): | ||
"""Visit block try except""" | ||
for handler in node.handlers: | ||
if len(handler.body) == 1 and \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid use \
better use (
if (len(handler.body) == 1 and
isinstace(...))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Done!
Thanks!
Could you fix conflicts? |
@moylop260 |
@lasley @pedrobaeza @dreispt @lmignon |
Hrmmmm I'm torn on this one. Forgive my ignorance in pylint, but is it possible to check if the exception is used slightly later in code? I use a strategy similar to below fairly often:
Context for this weirdness is in some of my connector modules, where you want to show the error after the fact but still forcefully continue with the queued ops. |
That is a good point. |
Yeah maybe we just leverage the pre-existing check to see whether a variable is used in the namespace? If the exception is assigned to a var, and the var is used, we can assume the exception is handled properly - or following the Zen of an explicit silence. Thanks for taking this into consideration! |
Than could be just a lint warning message, not breaking the build. |
@utils.check_messages('except-pass') | ||
def visit_tryexcept(self, node): | ||
"""Visit block try except""" | ||
for handler in node.handlers: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Yeah, from MQT we could enabled it like as a beta check |
pylint_odoo/checkers/modules_odoo.py
Outdated
for handler in node.handlers: | ||
if (len(handler.body) == 1 and | ||
isinstance(handler.body[0], astroid.node_classes.Pass) and | ||
not isinstance(node.handlers[0].name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and node.handlers and
in order to avoid IndexError using node.handlers[0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Done, added another condition to if.
"""Test method """ | ||
try: | ||
raise Exception('Exception') | ||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add other test case when there aren't handlers.
try:
raise Exception('Exception')
except Exception:
and other ones using many handlers
try:
raise Exception('Exception')
except Exception, UserError:
try:
raise Exception('Exception')
except (Exception, UserError) as exception:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Done,
This cases work fine.
pylint_odoo/checkers/modules_odoo.py
Outdated
'except-pass', | ||
settings.DESC_DFLT | ||
), | ||
'W%d38' % settings.BASE_OMODULE_ID: ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The except-pass
should be 38 not 37
@moylop260 |
raise Exception('Exception') | ||
except (Exception, IndexError, NameError), exception: | ||
pass | ||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment where the message should be emitted:
except Exception: # except-pass
def test_4_method(self): | ||
try: | ||
raise Exception('Exception') | ||
except Exception, UserError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use snake_name for UserError
def test_5_method(self): | ||
try: | ||
raise Exception('Exception') | ||
except (Exception, IndexError) as exception: |
There was a problem hiding this comment.
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):
pylint_odoo/checkers/modules_odoo.py
Outdated
"""Visit block try except""" | ||
for handler in node.handlers: | ||
if (not handler.name and | ||
len(handler.body) and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/len(handler.body)/handler.body/g
@moylop260 |
try: | ||
raise Exception('Exception') | ||
except Exception: | ||
print('Exception') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use pass
before of print
(this case shouldn't emit error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moylop260
Done,
I have validated the case of multiple handler.body
@lasley |
@JesusZapata |
6a6eedc
to
24fd5fb
Compare
[FIX] pylint-odoo: Best use of if clause [FIX] pylint-odoo: Validate the body contain only one line [FIX] pylint-odoo: Avoid use \ better use ( [REF] incoherent-interpreter-exec-perm: Better message (Vauxoo#106) [ADD] xml-attribute-translatable: Check XML attribute without translation parameter (Vauxoo#105) Close OCA#104 [REF] javascript-lint: Use eslint instead of jshint (Vauxoo#97) [ADD] renamed-field-parameter: Detect deprecated field values (digits_compute, select) (Vauxoo#99) [FIX] Pep8 check and bad index for ODOO_MSGS [ADD] attribute-string-redundant: Check if "string" parameter is equal to variable name (Vauxoo#100) [FIX] attribute-string-redundant: Add "isinstance" validation for nodes Fix OCA#109 [IMP] Exclude exception when use as assignation [FIX] Pep8 check local variable 'exception' [FIX] Pep8 blank line at end of file [FIX] Adding more cases of test [REF] Supporting more cases of exception [FIX] Modify file main.py for adding new case of except-pass [IMP] Adding another tests case [FIX] Adding another test case and better validation [FIX] Delete unnecessary condition of if [FIX] Fix comment line
5531bd3
to
daf67df
Compare
@moylop260 |
@lasley Do you have any comment about? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, this covers my reservations fully - thank you.
I'm kind of unclear on the test naming/comments though. Could we add some sort of indicator as to which is pass/fail, and why? SOmething like:
def test_valid_1(self):
""" It should not fail if the exception is assigned & used. """
@lasley |
The
pass
into block except is not a good practice!By including the
pass
we assume that our algorithm can continue to function after the exception occurredIf you really need to use the
pass
consider logging that exceptionfix OCA/maintainer-tools#252