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

Opinion: except-pass conflicts with Pythonic EAFP #361

Closed
StefanRijnhart opened this issue Feb 1, 2022 · 4 comments
Closed

Opinion: except-pass conflicts with Pythonic EAFP #361

StefanRijnhart opened this issue Feb 1, 2022 · 4 comments
Labels

Comments

@StefanRijnhart
Copy link
Member

StefanRijnhart commented Feb 1, 2022

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 passed. 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.

@sbidoul
Copy link
Member

sbidoul commented Feb 1, 2022

@pedrobaeza
Copy link
Member

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.

@yajo
Copy link
Member

yajo commented Feb 2, 2022

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.

@StefanRijnhart
Copy link
Member Author

@yajo thanks, I can live with replacing my try/except/pass clauses with this.

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

No branches or pull requests

4 participants