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

Flag B018 for strings and f-strings which aren't docstrings #11302

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 6, 2024

Summary

This PR updates the B018 heuristic to consider standalone string literals, except for those used as docstring and attribute docstring, as useless expression.

fixes: #11292

Test Plan

Add a test file full of attribute docstring and verified the snapshot.

@dhruvmanila dhruvmanila added the bug Something isn't working label May 6, 2024
Copy link
Contributor

github-actions bot commented May 6, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

PostHog/HouseWatch (+1 -0 violations, +0 -0 fixes)

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

+ housewatch/async_migrations/runner.py:19:1: B018 Found useless expression. Either assign it to a variable or remove it.

PlasmaPy/PlasmaPy (+6 -0 violations, +0 -0 fixes)

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

+ docs/_cff_to_rst.py:196:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ src/plasmapy/formulary/dielectric.py:31:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py:162:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py:777:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py:891:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ tests/diagnostics/charged_particle_radiography/test_synthetic_radiography.py:939:5: B018 Found useless expression. Either assign it to a variable or remove it.

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

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

+ airflow/api/auth/backend/kerberos_auth.py:48:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/migrations/versions/0115_2_4_0_remove_smart_sensors.py:47:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/models/dag.py:4121:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/models/dagrun.py:574:9: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/amazon/aws/hooks/lambda_function.py:168:9: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/amazon/aws/sensors/s3.py:112:9: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/celery/executors/celery_executor.py:91:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/cncf/kubernetes/executors/kubernetes_executor_utils.py:214:9: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/fab/auth_manager/models/__init__.py:47:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ airflow/providers/fab/auth_manager/security_manager/override.py:1396:5: B018 Found useless expression. Either assign it to a variable or remove it.
... 36 additional changes omitted for project

bokeh/bokeh (+2 -0 violations, +0 -0 fixes)

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

+ tests/unit/bokeh/core/test_serialization.py:864:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ tests/unit/bokeh/embed/test_notebook__embed.py:42:1: B018 Found useless expression. Either assign it to a variable or remove it.

demisto/content (+3299 -0 violations, +0 -0 fixes)

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

+ Packs/AHA/Integrations/AHA/AHA.py:111:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AHA/Integrations/AHA/AHA.py:12:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AHA/Integrations/AHA/AHA.py:163:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AHA/Integrations/AHA/AHA.py:234:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AHA/Integrations/AHA/AHA.py:283:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AHA/Integrations/AHA/AHA.py:52:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/AMPv2/AMPv2.py:1164:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/AMPv2/AMPv2.py:14:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/AMPv2/AMPv2.py:3220:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/AMPv2/AMPv2.py:3703:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/AMPv2/AMPv2.py:3:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/CiscoAMPEventCollector/CiscoAMPEventCollector.py:194:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/CiscoAMPEventCollector/CiscoAMPEventCollector.py:236:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/AMP/Integrations/CiscoAMPEventCollector/CiscoAMPEventCollector.py:9:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:15:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:60:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:6:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:708:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ANYRUN/Integrations/ANYRUN/ANYRUN.py:904:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/APIVoid/Integrations/APIVoid/APIVoid.py:14:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/APIVoid/Integrations/APIVoid/APIVoid.py:6:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:115:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:1288:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:139:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:159:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:15:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:2047:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:2081:1: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:222:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:238:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:295:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:32:9: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:353:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:37:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:44:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:522:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:573:13: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:623:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:67:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:699:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:751:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:772:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:783:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:799:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:812:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:830:5: B018 Found useless expression. Either assign it to a variable or remove it.
+ Packs/ARIAPacketIntelligence/Integrations/ARIAPacketIntelligence/ARIAPacketIntelligence.py:845:5: B018 Found useless expression. Either assign it to a variable or remove it.
... 3252 additional changes omitted for project

... Truncated remaining completed project reports due to GitHub comment length restrictions

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
B018 3446 3446 0 0 0

@dhruvmanila
Copy link
Member Author

Going through the ecosystem changes, it seems like most of the violations is because the string is used as a docstring for a variable or a class attribute. While for demisto/content, at least the ones I opened, used strings as a header which should instead be a comment.

@dhruvmanila
Copy link
Member Author

Oh, I just understood what Charlie meant by #11292 (comment), I'll see on to avoid flagging these. I think one way would be to add a ATTRIBUTE_DOCSTRING in the semantic model.

@charliermarsh
Copy link
Member

Yeah we should not flag those. Thanks Dhruv!

@dhruvmanila dhruvmanila changed the base branch from main to dhruv/attribute-docstring May 7, 2024 03:14
@dhruvmanila dhruvmanila force-pushed the dhruv/b018-string branch 2 times, most recently from 49ecbb8 to f7ee407 Compare May 7, 2024 03:30
@dhruvmanila dhruvmanila marked this pull request as ready for review May 10, 2024 11:23
@dhruvmanila
Copy link
Member Author

Still quite a lot of usages of triple-quoted strings which could be comments instead. This makes me wonder if this should be behind preview?

@dhruvmanila dhruvmanila force-pushed the dhruv/b018-string branch 2 times, most recently from bb04bc8 to e606aee Compare May 10, 2024 14:51
Base automatically changed from dhruv/attribute-docstring to main May 10, 2024 14:57
dhruvmanila added a commit that referenced this pull request May 10, 2024
## Summary

This PR adds updates the semantic model to detect attribute docstring.

Refer to [PEP 258](https://peps.python.org/pep-0258/#attribute-docstrings) 
for the definition of an attribute docstring.

This PR doesn't add full support for it but only considers string
literals as attribute docstring for the following cases:
1. A string literal following an assignment statement in the **global
scope**.
2. A global class attribute

For an assignment statement, it's considered an attribute docstring only
if the target expression is a name expression (`x = 1`). So, chained
assignment, multiple assignment or unpacking, and starred expression,
which are all valid in the target position, aren't considered here.

In `__init__` method, an assignment to the `self` variable like `self.x = 1`
is also a candidate for an attribute docstring. **This PR does not
support this position.**

## Test Plan

I used the following source code along with a print statement to verify
that the attribute docstring detection is correct.

Refer to the PR description for the code snippet.

I'll add this in the follow-up PR
(#11302) which uses this method.
@charliermarsh
Copy link
Member

I do think it should probably be in preview.

@@ -69,15 +69,11 @@ impl Violation for UselessExpression {
/// B018
pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
// Ignore comparisons, as they're handled by `useless_comparison`.
if value.is_compare_expr() {
if matches!(value, Expr::Compare(_) | Expr::EllipsisLiteral(_)) {
Copy link
Member

Choose a reason for hiding this comment

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

By the way, we could flag EllipsisLiteral I think? As long as we ignore stub methods...

Copy link
Member

Choose a reason for hiding this comment

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

But lets ignore that for this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False negative with useless-expression on strings
2 participants