mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 06:41:23 +00:00
Add E722 bare-except documentation (#2796)
This commit is contained in:
parent
099d5414f2
commit
fda93c6245
3 changed files with 63 additions and 1 deletions
|
@ -7,6 +7,35 @@ use crate::source_code::Locator;
|
|||
use crate::violation::Violation;
|
||||
|
||||
define_violation!(
|
||||
/// ## What it does
|
||||
/// Checks for bare `except` catches in `try`-`except` statements.
|
||||
///
|
||||
/// ## Why is this bad?
|
||||
/// A bare `except` catches `BaseException` which includes
|
||||
/// `KeyboardInterrupt`, `SystemExit`, `Exception`, and others. Catching
|
||||
/// `BaseException` can make it hard to interrupt the program (e.g., with
|
||||
/// Ctrl-C) and disguise other problems.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```python
|
||||
/// try:
|
||||
/// raise(KeyboardInterrupt("You probably don't mean to break CTRL-C."))
|
||||
/// except:
|
||||
/// print("But a bare `except` will ignore keyboard interrupts.")
|
||||
/// ```
|
||||
///
|
||||
/// Use instead:
|
||||
/// ```python
|
||||
/// try:
|
||||
/// do_something_that_might_break()
|
||||
/// except MoreSpecificException as e:
|
||||
/// handle_error(e)
|
||||
/// ```
|
||||
///
|
||||
/// ## References
|
||||
/// - [PEP 8](https://www.python.org/dev/peps/pep-0008/#programming-recommendations)
|
||||
/// - [Python: "Exception hierarchy"](https://docs.python.org/3/library/exceptions.html#exception-hierarchy)
|
||||
/// - [Google Python Style Guide: "Exceptions"](https://google.github.io/styleguide/pyguide.html#24-exceptions)
|
||||
pub struct BareExcept;
|
||||
);
|
||||
impl Violation for BareExcept {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue