Skip to content

[ruff] Implement falsy-dict-get-fallback (RUF056)#15160

Merged
MichaReiser merged 9 commits intoastral-sh:mainfrom
guptaarnav:falsy-dict-get-fallback
Dec 31, 2024
Merged

[ruff] Implement falsy-dict-get-fallback (RUF056)#15160
MichaReiser merged 9 commits intoastral-sh:mainfrom
guptaarnav:falsy-dict-get-fallback

Conversation

@guptaarnav
Copy link
Contributor

@guptaarnav guptaarnav commented Dec 27, 2024

Summary

Closes #4934 by implementing a new RUF rule that checks for dict.get calls that include a falsy fallback used in the context of a boolean, and returns a safe fix that removes that falsy fallback (except for situations where there are comments between arguments, in which case an unsafe fix is returned).
Note, using a falsy fallback for dict.get is okay when the expression is not used as a boolean, e.g. when the the actual value of the fallback is used instead of being converted to a boolean.

Test Plan

A RUF056.py snapshot test file was created that includes valid and invalid code based on this lint rule.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 27, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

RasaHQ/rasa (+4 -0 violations, +0 -0 fixes)

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

+ rasa/shared/core/domain.py:1546:43: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ rasa/utils/tensorflow/rasa_layers.py:397:47: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ rasa/utils/tensorflow/rasa_layers.py:398:50: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ rasa/utils/tensorflow/rasa_layers.py:758:77: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

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

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

+ airflow/serialization/serialized_objects.py:1393:67: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ airflow/serialization/serialized_objects.py:1401:85: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ airflow/serialization/serialized_objects.py:1431:41: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ providers/src/airflow/providers/apache/hdfs/hooks/webhdfs.py:123:90: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ scripts/in_container/in_container_utils.py:48:74: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

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

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

+ superset/db_engine_specs/databricks.py:286:69: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

milvus-io/pymilvus (+24 -0 violations, +0 -0 fixes)

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

+ pymilvus/client/grpc_handler.py:1298:33: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:1528:33: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:2099:33: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:307:33: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:602:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:613:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:638:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:649:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:709:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:760:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:775:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:783:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/grpc_handler.py:794:37: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/prepare.py:413:42: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/client/prepare.py:414:35: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:581:33: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:654:60: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:796:63: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:814:59: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:940:63: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/collection.py:956:59: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/connections.py:413:41: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/index.py:77:41: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ pymilvus/orm/partition.py:50:41: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

zulip/zulip (+4 -0 violations, +0 -0 fixes)

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

+ zerver/actions/streams.py:297:66: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ zerver/lib/import_realm.py:903:54: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ zerver/lib/outgoing_webhook.py:87:55: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.
+ zerver/views/documentation.py:268:53: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

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

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

+ src/_pytest/_py/path.py:953:30: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

astropy/astropy (+1 -0 violations, +0 -0 fixes)

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

+ astropy/utils/masked/tests/test_function_helpers.py:1050:53: RUF056 [*] Avoid providing a falsy fallback to `dict.get()` in boolean test positions. The default fallback `None` is already falsy.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF056 40 40 0 0 0

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@guptaarnav guptaarnav marked this pull request as draft December 27, 2024 00:55
@guptaarnav guptaarnav marked this pull request as ready for review December 27, 2024 02:20
@guptaarnav
Copy link
Contributor Author

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

RasaHQ/rasa (+5 -0 violations, +0 -0 fixes)
apache/airflow (+3 -0 violations, +0 -0 fixes)
milvus-io/pymilvus (+26 -0 violations, +0 -0 fixes)
zulip/zulip (+4 -0 violations, +0 -0 fixes)
pytest-dev/pytest (+1 -0 violations, +0 -0 fixes)
astropy/astropy (+1 -0 violations, +0 -0 fixes)
Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF056 40 40 0 0 0

Went through all 40 ecosystem linter changes, all are as expected.

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Dec 30, 2024
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Nice thank you. I think we can reuse some existing helpers to remove the necessary code for this rule

@dhruvmanila dhruvmanila added the preview Related to preview mode features label Dec 31, 2024
@MichaReiser MichaReiser merged commit 3c9021f into astral-sh:main Dec 31, 2024
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.

Remove falsy dict.get() fallback in if statements

3 participants