Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B901 catches yields in subexprs and more sensitive B901 #15101

Closed
wants to merge 1 commit into from

Conversation

guptaarnav
Copy link
Contributor

@guptaarnav guptaarnav commented Dec 22, 2024

Summary

Closes #14453 by updating the implementation of the B901 lint rule. The changes ensure that:
1. The visitor now traverses all expressions within statements, ensuring yield expressions embedded are correctly flagged even if they are not the expression of an ExprStmt.
2. The rule now correctly identifies yield and yield from expressions when they appear as the right-hand side of assignment statements (e.g., x = yield or x = yield from []); this catches the case mentioned in the original issue by @AlexWaygood.
3. We still do not recurse into lambda expressions or nested function defs as these are evaluated separately.

Test Plan

I added the tests mentioned by @MichaReiser and @AlexWaygood in the original issue into test/fixtures/flake8_bugbear/B901.py and updated the related snapshot.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+32 -0 violations, +0 -0 fixes in 2 projects; 53 projects unchanged)

python-trio/trio (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/trio/_core/_traps.py:63:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior

pytest-dev/pytest (+31 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

+ src/_pytest/assertion/__init__.py:188:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/cacheprovider.py:298:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/cacheprovider.py:419:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/cacheprovider.py:461:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/capture.py:872:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/capture.py:877:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/capture.py:882:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/capture.py:887:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/config/__init__.py:1435:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/debugging.py:303:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/faulthandler.py:88:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/helpconfig.py:133:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/logging.py:780:17: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/logging.py:788:17: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/logging.py:801:17: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/logging.py:869:17: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/pytester.py:171:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/setuponly.py:36:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/skipping.py:257:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/skipping.py:292:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/terminal.py:683:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/terminal.py:914:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/terminal.py:925:13: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/tmpdir.py:313:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/unittest.py:428:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/warnings.py:110:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/warnings.py:119:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/warnings.py:129:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/warnings.py:90:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ src/_pytest/warnings.py:99:9: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior
+ testing/conftest.py:89:5: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B901 32 32 0 0 0

@guptaarnav guptaarnav closed this Dec 22, 2024
@guptaarnav
Copy link
Contributor Author

guptaarnav commented Dec 22, 2024

Saw a large number of violations from the automated ruff-ecosystem check. primarily seems to be from code that says return (yield) which is valid. rethinking this PR, closing for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

B901: Misses yield sub-expresisons
1 participant