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

Preserve trailing inline comments on import-from statements #12498

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Jul 24, 2024

Summary

Right now, in the isort comment model, there's nowhere for trailing comments on the statement to go, as in:

from mylib import (
    MyClient,
    MyMgmtClient,
)  # some comment

If the comment is on the alias, we do preserve it, because we attach it to the alias, as in:

from mylib import (
    MyClient,
    MyMgmtClient,  # some comment
)

Similarly, if the comment is trailing on an import statement (non-from), we again attach it to the alias, because it can't be parenthesized, as in:

import foo  # some comment

This PR adds logic to track and preserve those trailing comments.

We also no longer drop several other comments, like:

from mylib import (
    # some comment
    MyClient
)

Closes #12487.

@charliermarsh charliermarsh added bug Something isn't working isort Related to import sorting labels Jul 24, 2024
Copy link
Contributor

github-actions bot commented Jul 24, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

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

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

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

+ pandas/core/generic.py:3468:13: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:3629:9: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:7992:17: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:7995:17: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:7998:17: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:9235:13: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:9242:17: PLW0642 Invalid assignment to `self` argument in instance method
+ pandas/core/generic.py:9245:17: PLW0642 Invalid assignment to `self` argument in instance method

python/typeshed (+0 -9 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

- stdlib/pstats.pyi:17:13: PYI052 Need type annotation for `CALLS`
- stdlib/pstats.pyi:18:18: PYI052 Need type annotation for `CUMULATIVE`
- stdlib/pstats.pyi:19:16: PYI052 Need type annotation for `FILENAME`
- stdlib/pstats.pyi:20:12: PYI052 Need type annotation for `LINE`
- stdlib/pstats.pyi:21:12: PYI052 Need type annotation for `NAME`
- stdlib/pstats.pyi:22:11: PYI052 Need type annotation for `NFL`
- stdlib/pstats.pyi:23:14: PYI052 Need type annotation for `PCALLS`
- stdlib/pstats.pyi:24:15: PYI052 Need type annotation for `STDNAME`
- stdlib/pstats.pyi:25:12: PYI052 Need type annotation for `TIME`

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
PYI052 9 0 9 0 0
PLW0642 8 8 0 0 0

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.

Thanks

crates/ruff_linter/src/rules/isort/annotate.rs Outdated Show resolved Hide resolved
18 18 | MyClient,
19 |- MyMgmtClient,
20 |- # some comment
19 |+ MyMgmtClient, # some comment
Copy link
Member Author

Choose a reason for hiding this comment

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

Gonna fix this before merging (the comment should stay on its own line).

@charliermarsh charliermarsh force-pushed the charlie/trailing-comment branch 2 times, most recently from 86fe6f9 to 0c98f14 Compare July 25, 2024 20:49
@charliermarsh charliermarsh merged commit 7571da8 into main Jul 25, 2024
20 checks passed
@charliermarsh charliermarsh deleted the charlie/trailing-comment branch July 25, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working isort Related to import sorting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ruff check --fix isort rule I001 removes comments
2 participants