pydicom / deid

best effort anonymization for medical images using python
https://pydicom.github.io/deid/
MIT License
140 stars 43 forks source link

Fix LGTM recommendations #191

Closed DimitriPapadopoulos closed 2 years ago

DimitriPapadopoulos commented 2 years ago

Description

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

Checklist

Open questions

The remaining recommendations should probably not be fixed.

DimitriPapadopoulos commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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