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

Fix LGTM recommendations #191

Merged
merged 3 commits into from
Oct 9, 2021
Merged

Fix LGTM recommendations #191

merged 3 commits into from
Oct 9, 2021

Conversation

DimitriPapadopoulos
Copy link
Contributor

@DimitriPapadopoulos DimitriPapadopoulos commented Oct 8, 2021

Description

Fixes most of the remaining LGTM recommendations:
https://lgtm.com/projects/g/pydicom/deid/alerts/?severity=recommendation

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • My code follows the style guidelines of this project

Open questions

The remaining recommendations should probably not be fixed.

Except block directly handles BaseException.
Import of 'remove_sequences' is not used.
The value assigned to local variable 'padded_expected_length' is never used.
@DimitriPapadopoulos
Copy link
Contributor Author

About Except block handles 'BaseException':

All exception classes in Python derive from BaseException. BaseException has three important subclasses, Exception from which all errors and normal exceptions derive, KeyboardInterrupt which is raised when the user interrupts the program from the keyboard and SystemExit which is raised by the sys.exit() function to terminate the program.

Since KeyboardInterrupt and SystemExit are special they should not be grouped together with other Exception classes.

Catching BaseException, rather than its subclasses may prevent proper handling of KeyboardInterrupt or SystemExit. It is easy to catch BaseException accidentally as it is caught implicitly by an empty except: statement.

@vsoch
Copy link
Member

vsoch commented Oct 8, 2021

@DimitriPapadopoulos a few questions / requests:

  1. Could we automate this?
  2. If we are replacing except with Exception, it would be ideal to catch what is actually expected there. The current change is just aesthetic.

@DimitriPapadopoulos
Copy link
Contributor Author

Actually the current change is not aesthetic: before the change the code catches everything including BaseException, after the change it catches only subclasses of Exception. The issue raised by LGTM.com is thus fixed:

Catching BaseException, rather than its subclasses may prevent proper handling of KeyboardInterrupt or SystemExit. It is easy to catch BaseException accidentally as it is caught implicitly by an empty except: statement.

That said, I totally agree with catching only the intended exceptions, but that can be quite difficult in Python after the fact. I'd rather do that in a different PR. This PR fixes the KeyboardInterrupt/SystemExit issue.

@DimitriPapadopoulos
Copy link
Contributor Author

I don't think it's possible to automate anything here. Tools like LGTM.com or DeepSource.io do assist in code reviewing, but we cannot let them decide blindly - yet?

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Oct 8, 2021

I must have misunderstood you. I believe you want automatic LGTM reporting (not automatically fixing alerts). Of course it is possible to have a CI job that checks new PRs and raises alerts. Here is the relevant page:

Integration with GitHub Apps

@vsoch vsoch merged commit 4568a29 into pydicom:master Oct 9, 2021
@vsoch
Copy link
Member

vsoch commented Oct 9, 2021

Thanks for the details! I will have to think about it - I'm usually pretty conservative about giving bots write access to PRs (or anything for that matter) and I'm not sure the benefits outweigh the risk. These are pretty small, trivial things.

image

@DimitriPapadopoulos DimitriPapadopoulos deleted the lgtm_recommendations branch October 10, 2021 08:55
@DimitriPapadopoulos
Copy link
Contributor Author

Yes, many alerts are trivial, but it does happen that they hide real issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants