Skip to content
Open
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
30 changes: 30 additions & 0 deletions crates/ruff_linter/src/rules/flake8_simplify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod tests {
use std::path::Path;

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

use crate::registry::Rule;
Expand Down Expand Up @@ -76,4 +77,33 @@ mod tests {
assert_diagnostics!(snapshot, diagnostics);
Ok(())
}

#[test_case(
Rule::SuppressibleException,
Path::new("SIM105_5.py"),
PythonVersion::PY311
)]
#[test_case(
Rule::SuppressibleException,
Path::new("SIM105_5.py"),
PythonVersion::PY312
)]
fn version_specific_rules(
rule_code: Rule,
path: &Path,
py_version: PythonVersion,
) -> Result<()> {
let snapshot = format!(
"{}_{}_{}",
rule_code.noqa_code(),
path.to_string_lossy(),
py_version
);
let diagnostics = test_path(
Path::new("flake8_simplify").join(path).as_path(),
&settings::LinterSettings::for_rule(rule_code).with_target_version(py_version),
)?;
assert_diagnostics!(snapshot, diagnostics);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be a good use of our assert_diagnostics_diff! macro instead of two separate test_cases.

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 @@ -101,6 +101,8 @@ pub(crate) fn suppressible_exception(
| Stmt::Pass(_)]
) || !orelse.is_empty()
|| !finalbody.is_empty()
|| (stmt.as_try_stmt().is_some_and(|x| x.is_star)
&& checker.target_version() <= PythonVersion::PY311)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
SIM105 [*] Use `contextlib.suppress(ValueError)` instead of `try`-`except`-`pass`
--> SIM105_5.py:10:1
|
9 | # SIM105
10 | / try:
11 | | pass
12 | | except ValueError:
13 | | pass
| |________^
|
help: Replace `try`-`except`-`pass` with `with contextlib.suppress(ValueError): ...`
1 | """Case: except* and except with py311 and higher"""
2 |
3 | # SIM105
4 + import contextlib
5 | try:
6 | pass
7 | except* ValueError:
8 | pass
9 |
10 | # SIM105
- try:
- pass
- except ValueError:
11 + with contextlib.suppress(ValueError):
12 | pass
note: This is an unsafe fix and may change runtime behavior
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: crates/ruff_linter/src/rules/flake8_simplify/mod.rs
---
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

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