Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Case: except* and except with py311 and higher"""

# SIM105
try:
pass
except* ValueError:
pass

# SIM105
try:
pass
except ValueError:
pass
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
handlers,
orelse,
finalbody,
is_star,
..
},
) => {
Expand Down Expand Up @@ -1358,7 +1359,7 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
}
if checker.is_rule_enabled(Rule::SuppressibleException) {
flake8_simplify::rules::suppressible_exception(
checker, stmt, body, handlers, orelse, finalbody,
checker, stmt, *is_star, body, handlers, orelse, finalbody,
);
}
if checker.is_rule_enabled(Rule::ReturnInTryExceptFinally) {
Expand Down
17 changes: 16 additions & 1 deletion crates/ruff_linter/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ mod tests {
use std::path::Path;

use anyhow::Result;
use ruff_python_ast::PythonVersion;
use test_case::test_case;

use crate::registry::Rule;
use crate::settings::LinterSettings;
use crate::settings::types::PreviewMode;
use crate::test::test_path;
use crate::{assert_diagnostics, settings};
use crate::{assert_diagnostics, assert_diagnostics_diff, settings};

#[test_case(Rule::DuplicateIsinstanceCall, Path::new("SIM101.py"))]
#[test_case(Rule::CollapsibleIf, Path::new("SIM102.py"))]
Expand Down Expand Up @@ -76,4 +77,18 @@ mod tests {
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}

#[test_case(Rule::SuppressibleException, Path::new("SIM105_5.py"))]
fn version_specific_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("diff_{}_{}", rule_code.noqa_code(), path.to_string_lossy());
assert_diagnostics_diff!(
snapshot,
Path::new("flake8_simplify").join(path).as_path(),
&settings::LinterSettings::for_rule(rule_code)
.with_target_version(PythonVersion::PY311),
&settings::LinterSettings::for_rule(rule_code)
.with_target_version(PythonVersion::PY312)
);
Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ruff_macros::{ViolationMetadata, derive_message_formats};
use ruff_python_ast::helpers;
use ruff_python_ast::name::UnqualifiedName;
use ruff_python_ast::{self as ast, ExceptHandler, Stmt};
use ruff_python_ast::{PythonVersion, helpers};
use ruff_source_file::LineRanges;
use ruff_text_size::Ranged;
use ruff_text_size::{TextLen, TextRange};
Expand Down Expand Up @@ -83,6 +83,7 @@ fn is_empty(body: &[Stmt]) -> bool {
pub(crate) fn suppressible_exception(
checker: &Checker,
stmt: &Stmt,
is_star: bool,
try_body: &[Stmt],
handlers: &[ExceptHandler],
orelse: &[Stmt],
Expand All @@ -101,6 +102,7 @@ pub(crate) fn suppressible_exception(
| Stmt::Pass(_)]
) || !orelse.is_empty()
|| !finalbody.is_empty()
|| (is_star && checker.target_version() <= PythonVersion::PY311)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
--- Linter settings ---
-linter.unresolved_target_version = 3.11
+linter.unresolved_target_version = 3.12

--- Summary ---
Removed: 0
Added: 1

--- Added ---
SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
--> SIM105_5.py:4:1
|
3 | # SIM105
4 | / try:
5 | | pass
6 | | except* ValueError:
7 | | pass
| |________^
8 |
9 | # SIM105
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1 | """Case: except* and except with py311 and higher"""
2 |
3 | # SIM105
- try:
- pass
- except* ValueError:
4 + import contextlib
5 + with contextlib.suppress(ValueError):
6 | pass
7 |
8 | # SIM105
note: This is an unsafe fix and may change runtime behavior
Loading