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

Forbid to raise exceptions that are caught in except cases #742

Closed
sobolevn opened this issue Aug 2, 2019 · 1 comment
Closed

Forbid to raise exceptions that are caught in except cases #742

sobolevn opened this issue Aug 2, 2019 · 1 comment
Labels
help wanted Extra attention is needed rule request Adding a new rule

Comments

@sobolevn
Copy link
Member

sobolevn commented Aug 2, 2019

Rule request

Thesis

We need to disallow code like this:

try:
    print(1)
    raise ValueError()
except ValueError:
    print(2)

This is just a goto statement.

Related:

Finding except clauses that raise a different exception class than they catch:

$ astpath "//ExceptHandler[body//Raise/exc//Name and not(contains(body//Raise/exc//Name/@id, type/Name/@id))]" | head -5
./hashlib.py:144        >except ImportError:
./plistlib.py:89        >        except KeyError:
./plistlib.py:103       >        except KeyError:
./nntplib.py:868        >        except ValueError:
./argparse.py:1116      >        except KeyError:

From: https://github.com/hchasestevens/astpath

Reasoning

We do not need a goto in our code.

@sobolevn sobolevn added help wanted Extra attention is needed rule request Adding a new rule labels Aug 2, 2019
@sobolevn sobolevn added this to the Version 0.12.0 milestone Aug 2, 2019
@sobolevn
Copy link
Member Author

sobolevn commented Aug 5, 2019

Nope, this is not going to be implemented for two reasons:

  1. We still can't catch things like raise IndexError() and except LookupError: in one of the cases
  2. Sometimes this goto is useful

Sorry.

@sobolevn sobolevn closed this as completed Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed rule request Adding a new rule
Projects
None yet
Development

No branches or pull requests

1 participant