Opinion: except-pass conflicts with Pythonic EAFP
Closed this issue · 4 comments
Having a warning in pylint-odoo about using pass
after an exception (except-pass, added in #107) conflicts with the 'easier to ask forgiveness than to ask permission' approach (see https://docs.quantifiedcode.com/python-anti-patterns/readability/asking_for_permission_instead_of_forgiveness_when_working_with_files.html).
This approach typically catches specific exceptions which are then pass
ed. The pylint-odoo recommendation to add a log message can lead to substantial, and overall redundant logging.
Would there be interest to remove this check? If so, I could propose a code change.
👍 to remove it, at least from https://github.com/OCA/oca-addons-repo-template/blob/master/src/.pylintrc-mandatory.jinja
Yeah, I agree to remove it, but this will require an update of all repositories. I would take the occasion to update runboat and GH actions links with such update.
The reasoning is that, if there's an exception, something should have gone wrong. Thus, there has to be something to be done. Usually, at least, printing a log line explaining the something that is being skipped.
The modern pythonic way to avoid the lint error:
import contextlib
with contextlib.suppress(IndexError):
# the code that would produce the error and still be OK
recordset[0].unlink()
We can leave it in optional checks, as it is somewhat logic what the linter tries to say, although maybe too strict for a requirement.
@yajo thanks, I can live with replacing my try/except/pass clauses with this.