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,25 @@
import warnings
import pytest


def raise_deprecation_warning(s):
warnings.warn(s, DeprecationWarning)
return s


def test_ok():
with pytest.deprecated_call():
raise_deprecation_warning("")


def test_error_trivial():
pytest.deprecated_call(raise_deprecation_warning, "deprecated")


def test_error_assign():
s = pytest.deprecated_call(raise_deprecation_warning, "deprecated")
print(s)


def test_error_lambda():
pytest.deprecated_call(lambda: warnings.warn("", DeprecationWarning))
40 changes: 40 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF061_raises.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest


def func(a, b):
return a / b


def test_ok():
with pytest.raises(ValueError):
raise ValueError


def test_ok_as():
with pytest.raises(ValueError) as excinfo:
raise ValueError


def test_error_trivial():
pytest.raises(ZeroDivisionError, func, 1, b=0)


def test_error_match():
pytest.raises(ZeroDivisionError, func, 1, b=0).match("division by zero")


def test_error_assign():
excinfo = pytest.raises(ZeroDivisionError, func, 1, b=0)


def test_error_kwargs():
pytest.raises(func=func, expected_exception=ZeroDivisionError)


def test_error_multi_statement():
excinfo = pytest.raises(ValueError, int, "hello")
assert excinfo.match("^invalid literal")


def test_error_lambda():
pytest.raises(ZeroDivisionError, lambda: 1 / 0)
25 changes: 25 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF061_warns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import warnings
import pytest


def raise_user_warning(s):
warnings.warn(s, UserWarning)
return s


def test_ok():
with pytest.warns(UserWarning):
raise_user_warning("")


def test_error_trivial():
pytest.warns(UserWarning, raise_user_warning, "warning")


def test_error_assign():
s = pytest.warns(UserWarning, raise_user_warning, "warning")
print(s)


def test_error_lambda():
pytest.warns(UserWarning, lambda: warnings.warn("", UserWarning))
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,9 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) {
]) {
flake8_pytest_style::rules::raises_call(checker, call);
}
if checker.enabled(Rule::LegacyFormPytestRaises) {
ruff::rules::legacy_raises_warns_deprecated_call(checker, call);
}
if checker.any_enabled(&[Rule::PytestWarnsWithoutWarning, Rule::PytestWarnsTooBroad]) {
flake8_pytest_style::rules::warns_call(checker, call);
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Ruff, "058") => (RuleGroup::Preview, rules::ruff::rules::StarmapZip),
(Ruff, "059") => (RuleGroup::Preview, rules::ruff::rules::UnusedUnpackedVariable),
(Ruff, "060") => (RuleGroup::Preview, rules::ruff::rules::InEmptyCollection),
(Ruff, "061") => (RuleGroup::Preview, rules::ruff::rules::LegacyFormPytestRaises),
(Ruff, "100") => (RuleGroup::Stable, rules::ruff::rules::UnusedNOQA),
(Ruff, "101") => (RuleGroup::Stable, rules::ruff::rules::RedirectedNOQA),
(Ruff, "102") => (RuleGroup::Preview, rules::ruff::rules::InvalidRuleCode),
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ mod tests {
#[test_case(Rule::UnusedUnpackedVariable, Path::new("RUF059_2.py"))]
#[test_case(Rule::UnusedUnpackedVariable, Path::new("RUF059_3.py"))]
#[test_case(Rule::InEmptyCollection, Path::new("RUF060.py"))]
#[test_case(Rule::LegacyFormPytestRaises, Path::new("RUF061_raises.py"))]
#[test_case(Rule::LegacyFormPytestRaises, Path::new("RUF061_warns.py"))]
#[test_case(Rule::LegacyFormPytestRaises, Path::new("RUF061_deprecated_call.py"))]
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_0.py"))]
#[test_case(Rule::RedirectedNOQA, Path::new("RUF101_1.py"))]
#[test_case(Rule::InvalidRuleCode, Path::new("RUF102.py"))]
Expand Down
Loading
Loading