diff --git a/README.md b/README.md index d5ad30342a..d58290b993 100644 --- a/README.md +++ b/README.md @@ -746,7 +746,7 @@ For more, see [pycodestyle](https://pypi.org/project/pycodestyle/) on PyPI. | E713 | not-in-test | Test for membership should be `not in` | 🛠 | | E714 | not-is-test | Test for object identity should be `is not` | 🛠 | | E721 | type-comparison | Do not compare types, use `isinstance()` | | -| E722 | bare-except | Do not use bare `except` | | +| E722 | [bare-except](https://github.com/charliermarsh/ruff/blob/main/docs/rules/bare-except.md) | Do not use bare `except` | | | E731 | lambda-assignment | Do not assign a `lambda` expression, use a `def` | 🛠 | | E741 | ambiguous-variable-name | Ambiguous variable name: `{name}` | | | E742 | ambiguous-class-name | Ambiguous class name: `{name}` | | diff --git a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs index 8049f608f0..cfcc59d9b4 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/bare_except.rs @@ -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 { diff --git a/docs/rules/bare-except.md b/docs/rules/bare-except.md new file mode 100644 index 0000000000..26427bbd77 --- /dev/null +++ b/docs/rules/bare-except.md @@ -0,0 +1,33 @@ +# bare-except (E722) + +Derived from the **pycodestyle** linter. + +## 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) \ No newline at end of file