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
40 changes: 40 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B901.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,43 @@ async def broken6():
async def broken7():
yield 1
return [1, 2, 3]


import pytest


@pytest.hookimpl(wrapper=True)
def pytest_runtest_makereport():
result = yield
return result


@pytest.hookimpl(wrapper=True)
def pytest_fixture_setup():
result = yield
result.some_attr = "modified"
return result


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call():
result = yield
return result


@pytest.hookimpl()
def pytest_configure():
yield
return "should error"


@pytest.hookimpl(wrapper=False)
def pytest_unconfigure():
yield
return "should error"


@pytest.fixture()
def my_fixture():
yield
return "should error"
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ruff_text_size::TextRange;

use crate::Violation;
use crate::checkers::ast::Checker;
use crate::rules::flake8_pytest_style::helpers::is_pytest_hookimpl_wrapper;

/// ## What it does
/// Checks for `return {value}` statements in functions that also contain `yield`
Expand Down Expand Up @@ -100,6 +101,14 @@ pub(crate) fn return_in_generator(checker: &Checker, function_def: &StmtFunction
return;
}

if function_def
.decorator_list
.iter()
.any(|decorator| is_pytest_hookimpl_wrapper(decorator, checker.semantic()))
{
return;
}

let mut visitor = ReturnInGeneratorVisitor::default();
visitor.visit_body(&function_def.body);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,30 @@ B901 Using `yield` and `return {value}` in a generator function can lead to conf
88 | return [1, 2, 3]
| ^^^^^^^^^^^^^^^^
|

B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
--> B901.py:116:5
|
114 | def pytest_configure():
115 | yield
116 | return "should error"
| ^^^^^^^^^^^^^^^^^^^^^
|

B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
--> B901.py:122:5
|
120 | def pytest_unconfigure():
121 | yield
122 | return "should error"
| ^^^^^^^^^^^^^^^^^^^^^
|

B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
--> B901.py:128:5
|
126 | def my_fixture():
127 | yield
128 | return "should error"
| ^^^^^^^^^^^^^^^^^^^^^
|
29 changes: 28 additions & 1 deletion crates/ruff_linter/src/rules/flake8_pytest_style/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use ruff_python_ast::helpers::map_callable;
use ruff_python_ast::helpers::{is_const_true, map_callable};
use ruff_python_ast::{self as ast, Decorator, Expr, ExprCall, Keyword, Stmt, StmtFunctionDef};
use ruff_python_semantic::analyze::visibility;
use ruff_python_semantic::{ScopeKind, SemanticModel};
Expand Down Expand Up @@ -50,6 +50,33 @@ pub(super) fn is_pytest_parametrize(call: &ExprCall, semantic: &SemanticModel) -
})
}

/// Returns `true` if the decorator is `@pytest.hookimpl(wrapper=True)` or
/// `@pytest.hookimpl(hookwrapper=True)`.
///
/// These hook wrappers intentionally use `return` in generator functions as part of the
/// pytest hook wrapper protocol.
///
/// See: <https://docs.pytest.org/en/stable/how-to/writing_hook_functions.html#hook-wrappers-executing-around-other-hooks>
pub(crate) fn is_pytest_hookimpl_wrapper(decorator: &Decorator, semantic: &SemanticModel) -> bool {
let Expr::Call(call) = &decorator.expression else {
return false;
};

// Check if it's pytest.hookimpl
let is_hookimpl = semantic
.resolve_qualified_name(&call.func)
.is_some_and(|name| matches!(name.segments(), ["pytest", "hookimpl"]));

if !is_hookimpl {
return false;
}

let wrapper = call.arguments.find_argument_value("wrapper", 6);
let hookwrapper = call.arguments.find_argument_value("hookwrapper", 1);

wrapper.or(hookwrapper).is_some_and(is_const_true)
}

/// Whether the currently checked `func` is likely to be a Pytest test.
///
/// A normal Pytest test function is one whose name starts with `test` and is either:
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/flake8_pytest_style/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Rules from [flake8-pytest-style](https://pypi.org/project/flake8-pytest-style/).
mod helpers;
pub(crate) mod helpers;
pub(crate) mod rules;
pub mod settings;
pub mod types;
Expand Down