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

[flake8-bugbear] Implement return-in-generator (B901) #11644

Merged

Conversation

tobb10001
Copy link
Contributor

@tobb10001 tobb10001 commented May 31, 2024

Summary

This PR implements the rule B901, which is part of the opinionated rules of flake8-bugbear.

This rule seems to be desired in ruff as per #3758 and #2954 (comment).

Test Plan

As this PR was made closely following the CONTRIBUTING.md, it tests using the snapshot approach, that is described there.

Sources

The implementation is inspired by the original implementation in the flake8-bugbear repository. The error message and test file where also copied from there.

The documentation I came up with on my own and needs improvement. Maybe the example given in #2954 (comment) could be used, but maybe they are too complex, I'm not sure.

Open Questions

  • Documentation. (See above.)

  • Can I access the parent in a visitor?

    The original implementation references the yield statement's parent to check if it is an expression statement. I didn't find a way to do this in ruff and used the is_expresssion_statement field on the visitor instead. What are your thoughts on this? Is it possible and / or desired to access the parent node here?

  • Is Option::is_some(...) -> ...unwrap() the right thing to do?

    Referring to this piece of code. From my understanding, the .unwrap() is safe, because it is checked that return_ is not None. However, I feel like I missed a more elegant solution that does both in one.

Other

I don't know a lot about this rule, I just implemented it because I found it in a good first issue Good for newcomers .

I'm new to Rust, so any constructive critisism is appreciated.

Copy link
Contributor

github-actions bot commented May 31, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

apache/airflow (+1 -0 violations, +0 -0 fixes)

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

+ tests/models/test_taskinstance.py:2421:17: B901 Using `yield` and `return {value}` in a generator function can lead to confusing behavior

reflex-dev/reflex (+1 -0 violations, +0 -0 fixes)

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

+ integration/test_server_side_event.py:24:13: 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 2 2 0 0 0

}
}
_ => {
self.in_expr_statement = stmt.is_expr_stmt();
Copy link
Member

Choose a reason for hiding this comment

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

I think I would do this by checking Stmt::Expr here, and then just introspecting the value directly to see if the value is Expr::Yield.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I updated the implementation to this.

@charliermarsh charliermarsh self-assigned this May 31, 2024
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule preview Related to preview mode features labels May 31, 2024
@charliermarsh
Copy link
Member

Thanks! Two small comments based on your questions.

@charliermarsh charliermarsh force-pushed the b901-return-x-generator-function branch from b88f550 to a335dea Compare May 31, 2024 21:39
Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks!

@charliermarsh charliermarsh changed the title [flake8-bugbear] Implement return-x-in-generator (B901) [flake8-bugbear] Implement return-in-generator (B901) May 31, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) May 31, 2024 21:40
@charliermarsh charliermarsh force-pushed the b901-return-x-generator-function branch from a335dea to 253e157 Compare May 31, 2024 21:44
@charliermarsh charliermarsh merged commit 312f664 into astral-sh:main May 31, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants