Skip to content

[ruff] Support slices in RUF005 #17078

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

Merged
merged 2 commits into from
Mar 31, 2025
Merged

[ruff] Support slices in RUF005 #17078

merged 2 commits into from
Mar 31, 2025

Conversation

akx
Copy link
Contributor

@akx akx commented Mar 31, 2025

Summary

Teaches RUF005 to also consider slices for concatenation. Other indexing (foo[0] + [7, 8, 9] + bar[1]) is explicitly not considered.

 foo = [4, 5, 6]
-bar = [1, 2, 3] + foo
-slicing1 = foo[:1] + [7, 8, 9]
-slicing2 = [7, 8, 9] + bar[1:]
-slicing3 = foo[:1] + [7, 8, 9] + bar[1:]
+bar = [1, 2, 3, *foo]
+slicing1 = [*foo[:1], 7, 8, 9]
+slicing2 = [7, 8, 9, *bar[1:]]
+slicing3 = [*foo[:1], 7, 8, 9, *bar[1:]]

Test Plan

Manually tested (diff above from ruff check --diff), snapshot updated.

Copy link
Contributor

github-actions bot commented Mar 31, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

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

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

+ airflow-core/src/airflow/io/path.py:108:20: RUF005 Consider `(parsed_url.geturl(), *args[1:])` instead of concatenation
+ providers/apache/spark/tests/unit/apache/spark/hooks/test_spark_jdbc_script.py:113:38: RUF005 Consider iterable unpacking instead of concatenation
+ providers/apache/spark/tests/unit/apache/spark/hooks/test_spark_jdbc_script.py:138:38: RUF005 Consider iterable unpacking instead of concatenation
+ providers/celery/src/airflow/providers/celery/executors/celery_executor.py:263:32: RUF005 Consider `(*task_tuple[:3], execute_command)` instead of concatenation
+ providers/google/tests/unit/google/cloud/hooks/test_cloud_storage_transfer_service.py:859:81: RUF005 Consider `[*pages_requests[1:], None]` instead of concatenation
+ providers/google/tests/unit/google/cloud/hooks/test_mlengine.py:178:81: RUF005 Consider `[*pages_requests[1:], None]` instead of concatenation
+ providers/google/tests/unit/google/cloud/hooks/test_mlengine.py:807:81: RUF005 Consider `[*pages_requests[1:], None]` instead of concatenation

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

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

+ superset/viz.py:2239:33: RUF005 Consider `[DTTM_ALIAS, *groups[:i]]` instead of concatenation

binary-husky/gpt_academic (+1 -0 violations, +0 -0 fixes)

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

+ toolbox.py:538:23: RUF005 Consider iterable unpacking instead of concatenation

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

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

+ examples/topics/pie/donut.py:41:14: RUF005 Consider `[0, *angles[:-1]]` instead of concatenation
+ src/bokeh/server/tornado.py:449:36: RUF005 Consider `(self._prefix + p[0], *p[1:], data)` instead of concatenation
+ src/bokeh/server/tornado.py:452:32: RUF005 Consider `(self._prefix + p[0], *p[1:])` instead of concatenation

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

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select E,F,FA,I,PYI,RUF,UP,W

+ tests/pyright_test.py:40:15: RUF005 Consider `[npx, f"pyright@{pyright_version}", *sys.argv[1:]]` instead of concatenation

python-poetry/poetry (+4 -0 violations, +0 -0 fixes)

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

+ src/poetry/inspection/lazy_wheel.py:115:25: RUF005 Consider `[start, *lslice[:1]]` instead of concatenation
+ src/poetry/inspection/lazy_wheel.py:116:19: RUF005 Consider `[end, *rslice[-1:]]` instead of concatenation
+ tests/console/commands/test_run.py:133:49: RUF005 Consider `[file, *args[1:]]` instead of concatenation
+ tests/console/commands/test_run.py:165:49: RUF005 Consider `[file, *args[1:]]` instead of concatenation

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

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

+ indico/modules/events/registration/fields/choices_test.py:149:16: RUF005 [*] Consider iterable unpacking instead of concatenation
+ indico/modules/events/registration/fields/choices_test.py:173:16: RUF005 [*] Consider `[old_choices[-1], *new_choices[:-1]]` instead of concatenation
+ indico/util/iterables.py:68:18: RUF005 [*] Consider `(*result[1:], elem)` instead of concatenation
+ indico/web/forms/util.py:60:24: RUF005 [*] Consider iterable unpacking instead of concatenation

wntrblm/nox (+1 -0 violations, +0 -0 fixes)

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

+ nox/_resolver.py:201:21: RUF005 Consider `[*walk_list[walk_list.index(node):], node]` instead of concatenation

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF005 22 22 0 0 0

@MichaReiser
Copy link
Member

MichaReiser commented Mar 31, 2025

Thank you. I think we should gate this change behind preview as it significantly expands the rule's scope (as demonstrated by the ecosystem results).

Did you review the results from the ecosystem check?

@ntBre do you want to review this change?

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Mar 31, 2025
@akx
Copy link
Contributor Author

akx commented Mar 31, 2025

@MichaReiser Sure, added a second commit to gate it as preview only for now. (If this is merged, the two commits should probably be squashed since the second commit reverts the snapshot + test case changes from the first one.)

To my eye, the ecosystem changes look alright.

@ntBre
Copy link
Contributor

ntBre commented Mar 31, 2025

@MichaReiser will do!

@akx we squash and merge through GitHub, so no worries about squashing manually!

Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks! I had one request about the test placement, but this otherwise looks good to me.

@akx akx requested a review from ntBre March 31, 2025 12:38
@ntBre ntBre added the preview Related to preview mode features label Mar 31, 2025
@ntBre ntBre changed the title Support slices in RUF005 [ruff] Support slices in RUF005 Mar 31, 2025
@ntBre ntBre merged commit 491a519 into astral-sh:main Mar 31, 2025
22 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.

3 participants