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

Conversation

JesusZapata
Copy link
Member

@JesusZapata JesusZapata commented Jan 18, 2017

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 occurred

If you really need to use the pass consider logging that exception

    try:
        sentences
    except:
        pass
       _logger.debug('Why the exception is safe....', exc_info=1))

fix OCA/maintainer-tools#252

@JesusZapata
Copy link
Member Author

@moylop260
What do you think about this PR?

def visit_tryexcept(self, node):
"""Visit block try except"""
for handler in node.handlers:
if handler.body:
Copy link
Collaborator

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)

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
Thanks!

def visit_tryexcept(self, node):
"""Visit block try except"""
for handler in node.handlers:
if handler.body and isinstance(handler.body[0],
Copy link
Collaborator

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

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
Done!

def visit_tryexcept(self, node):
"""Visit block try except"""
for handler in node.handlers:
if len(handler.body) == 1 and \
Copy link
Collaborator

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(...))

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
Done!
Thanks!

@moylop260
Copy link
Collaborator

Could you fix conflicts?

@JesusZapata
Copy link
Member Author

@moylop260
Done! I fixed this conflict!

@moylop260
Copy link
Collaborator

@lasley @pedrobaeza @dreispt @lmignon
your feedback is welcome

@lasley
Copy link
Contributor

lasley commented Jan 26, 2017

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:

err = None
for i in iter_of_callables:
    try:
        i()
    except IException as err:
        pass
if err:
    raise err

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.

@moylop260
Copy link
Collaborator

That is a good point.
@JesusZapata
Could you support that case, please?

@lasley
Copy link
Contributor

lasley commented Jan 27, 2017

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!

@dreispt
Copy link
Sponsor Member

dreispt commented Jan 27, 2017

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:
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

@moylop260
Copy link
Collaborator

@dreispt Than could be just a lint warning message, not breaking the build.

Yeah, from MQT we could enabled it like as a beta check

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,
Copy link
Collaborator

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]

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
Done, added another condition to if.

"""Test method """
try:
raise Exception('Exception')
except 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 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:

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
Done,
This cases work fine.

'except-pass',
settings.DESC_DFLT
),
'W%d38' % settings.BASE_OMODULE_ID: (
Copy link
Collaborator

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

@JesusZapata
Copy link
Member Author

@moylop260
I have attached more cases and now check if there are several handlers.

raise Exception('Exception')
except (Exception, IndexError, NameError), exception:
pass
except 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 a comment where the message should be emitted:
except Exception: # except-pass

def test_4_method(self):
try:
raise Exception('Exception')
except Exception, UserError:
Copy link
Collaborator

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:
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):

"""Visit block try except"""
for handler in node.handlers:
if (not handler.name and
len(handler.body) and
Copy link
Collaborator

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

@JesusZapata
Copy link
Member Author

@moylop260
I have done all changes
Thanks for you comments

try:
raise Exception('Exception')
except Exception:
print('Exception')
Copy link
Collaborator

@moylop260 moylop260 Jan 31, 2017

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)

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
Done,
I have validated the case of multiple handler.body

@JesusZapata JesusZapata changed the title [IMP] except-pass: Emit message If a except:pass is used (OCA/#252) [ADD] except-pass: Emit message If a except:pass is used (OCA/#252) Jan 31, 2017
@moylop260
Copy link
Collaborator

@lasley
Could you review the case requested, please?

@moylop260
Copy link
Collaborator

@JesusZapata
Could you squash all commits in one, please?

@JesusZapata JesusZapata force-pushed the oca-master-add-except-pass-checker-jesuszapata branch from 6a6eedc to 24fd5fb Compare January 31, 2017 20:51
[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
@JesusZapata JesusZapata force-pushed the oca-master-add-except-pass-checker-jesuszapata branch from 5531bd3 to daf67df Compare January 31, 2017 20:56
@JesusZapata
Copy link
Member Author

@moylop260
I made rebase.

@moylop260
Copy link
Collaborator

@lasley Do you have any comment about?

Copy link
Contributor

@lasley lasley left a 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. """

@JesusZapata
Copy link
Member Author

@lasley
Only documented when skip the except-pass

@moylop260 moylop260 merged commit bc1e6f4 into OCA:master Feb 7, 2017
@moylop260 moylop260 deleted the oca-master-add-except-pass-checker-jesuszapata branch February 7, 2017 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants