Skip to content

[ruff] Mark fixes for unsorted-dunder-all and unsorted-dunder-slots as unsafe when there are complex comments in the sequence (RUF022, RUF023)#14560

Merged
AlexWaygood merged 1 commit intomainfrom
alex/unsafe-sorting
Nov 24, 2024
Merged

[ruff] Mark fixes for unsorted-dunder-all and unsorted-dunder-slots as unsafe when there are complex comments in the sequence (RUF022, RUF023)#14560
AlexWaygood merged 1 commit intomainfrom
alex/unsafe-sorting

Conversation

@AlexWaygood
Copy link
Member

Summary

Fixes #14552

Mark fixes for RUF022 as unsafe if there are own-line comments in __all__, since these are often used to delimit categories within __all__:

__all__ = [
    # Core components
    "TaskManager",
    "TaskExecutor",
    # Models
    "TaskContext",
    "TaskProvider",
    "TaskResult",
    # Utilities
    "execute_concurrent",
]

Also mark the fix as unsafe if there are ambiguous cases like the following, where you can't say for sure which item the comment should be attached to when the items are reordered:

__all__ = [
    "a", "c",  # comment1
    "b", "d",  # comment2
]

Also apply the same changes to RUF023 (which sorts __slots__ rather than __all__, and adjust the documentation for both rules accordingly.

Test Plan

cargo test -p ruff_linter --lib.

I went through all the existing fixtures for these two rules. All the examples which I would expect to be marked as unsafe fixes are now marked accordingly. All the ones that I would expect to still be marked as safe are still marked as safe.

We already have very good fixture coverage for these rules, so I didn't add any new fixtures.

@AlexWaygood AlexWaygood added the fixes Related to suggested fixes for violations label Nov 23, 2024
@github-actions
Copy link
Contributor

github-actions bot commented Nov 23, 2024

ruff-ecosystem results

Linter (stable)

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

DisnakeDev/disnake (+0 -0 violations, +0 -2 fixes)

- disnake/interactions/application_command.py:15:11: RUF022 [*] `__all__` is not sorted
+ disnake/interactions/application_command.py:15:11: RUF022 `__all__` is not sorted

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

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

- airflow/__init__.py:63:11: RUF022 [*] `__all__` is not sorted
+ airflow/__init__.py:63:11: RUF022 `__all__` is not sorted

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

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

+ stubs/lupa/lupa/__init__.pyi:3:11: RUF022 `__all__` is not sorted
+ stubs/setuptools/setuptools/_distutils/command/__init__.pyi:22:11: RUF022 `__all__` is not sorted

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

- astropy/cosmology/__init__.py:30:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/__init__.py:30:11: RUF022 `__all__` is not sorted
- astropy/cosmology/flrw/__init__.py:4:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/flrw/__init__.py:4:11: RUF022 `__all__` is not sorted
- astropy/cosmology/realizations.py:9:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/realizations.py:9:11: RUF022 `__all__` is not sorted
- astropy/cosmology/units.py:19:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/units.py:19:11: RUF022 `__all__` is not sorted
- astropy/io/fits/hdu/base.py:32:11: RUF022 [*] `__all__` is not sorted
+ astropy/io/fits/hdu/base.py:32:11: RUF022 `__all__` is not sorted
- astropy/logger.py:32:11: RUF022 [*] `__all__` is not sorted
+ astropy/logger.py:32:11: RUF022 `__all__` is not sorted

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF022 18 2 0 0 16

Linter (preview)

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

DisnakeDev/disnake (+0 -0 violations, +0 -2 fixes)

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

- disnake/interactions/application_command.py:15:11: RUF022 [*] `__all__` is not sorted
+ disnake/interactions/application_command.py:15:11: RUF022 `__all__` is not sorted

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

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

- airflow/__init__.py:63:11: RUF022 [*] `__all__` is not sorted
+ airflow/__init__.py:63:11: RUF022 `__all__` is not sorted

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

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

+ pandas/api/typing/__init__.py:36:11: RUF022 `__all__` is not sorted

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

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

+ stubs/lupa/lupa/__init__.pyi:3:11: RUF022 `__all__` is not sorted
+ stubs/setuptools/distutils/command/__init__.pyi:22:11: RUF022 `__all__` is not sorted

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

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

- astropy/cosmology/__init__.py:30:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/__init__.py:30:11: RUF022 `__all__` is not sorted
- astropy/cosmology/flrw/__init__.py:4:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/flrw/__init__.py:4:11: RUF022 `__all__` is not sorted
- astropy/cosmology/realizations.py:9:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/realizations.py:9:11: RUF022 `__all__` is not sorted
- astropy/cosmology/units.py:19:11: RUF022 [*] `__all__` is not sorted
+ astropy/cosmology/units.py:19:11: RUF022 `__all__` is not sorted
- astropy/io/fits/hdu/base.py:32:11: RUF022 [*] `__all__` is not sorted
+ astropy/io/fits/hdu/base.py:32:11: RUF022 `__all__` is not sorted
- astropy/logger.py:32:11: RUF022 [*] `__all__` is not sorted
+ astropy/logger.py:32:11: RUF022 `__all__` is not sorted

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF022 19 3 0 0 16

@AlexWaygood
Copy link
Member Author

AlexWaygood commented Nov 23, 2024

The ecosystem changes look good to me.

…ots` as unsafe when there are complex comments in the sequence (`RUF022`, `RUF023`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RUF022 autofix should be marked unsafe if there are own-line comments between __all__ items

2 participants