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

too-many-try-statements should exclude pass statements if required by Python #9418

Open
ColinKennedy opened this issue Feb 8, 2024 · 2 comments
Labels
Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@ColinKennedy
Copy link

Current problem

I have code like this

try:
    with open("blah.txt", "a", encoding="ascii"):
        pass
except exceptions_to_catch:
    # do stuff

The too-many-try-statements optional checker flags this as 2 statements, which is technically true, but the second statement, pass, is there only because the with block will cause SyntaxError without it.

Desired solution

It probably isn't a good idea to completely ignore any/all pass statements, though that would be the easiest way to solve this, but the check should at least ignore pass when it's required to by Python.

A more judicious approach would be "ignore the second statement if all of these are true:

  • The second statement is pass
  • The previous statement is a Python control flow statement like if ...:, with ...:, while ...:, or the like.
  • There are no other statements

Or something along those lines

Additional context

No response

@ColinKennedy ColinKennedy added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 8, 2024
@ColinKennedy ColinKennedy changed the title too-many-try-statements should exclude pass statements as required by Python too-many-try-statements should exclude pass statements if required by Python Feb 8, 2024
@jacobtylerwalls
Copy link
Member

Sounds reasonable, especially since the default for --max-try-statements is 1.

It probably isn't a good idea to completely ignore any/all pass statements

I'm not even opposed to that. Are there realistic cases of people using pass to do something other than complete a for / while / with / else?

@jacobtylerwalls jacobtylerwalls added Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Feb 10, 2024
@ColinKennedy
Copy link
Author

Are there realistic cases of people using pass to do something other than complete a for / while / with / else?

None that I can think of. There could be code like

pass
foo = 8

pass
pass

But that code would already be pretty weird

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

2 participants