Comments (8)
Agreed,
historically isort hasn't done this because it adds a lot more complexity.
For instance given:
from a import b # comment 1
from a import c # comment 2
Considering the imports end up as:
from a import b, c
Where should the comments go? And what about dealing with line length limits etc?
However, I think at this stage of the project - it's worth it to make sure this works as best as possible:
from a import b, c # comment 1, comment 2
is after all, much better then stripping potentially useful comments.
Thank you for bringing this up, will fix and then deploy a new release.
~Tim
from isort.
Glad you see it that way. I see the complexity of this, which gets worse with the various multi-line modes. I think though, comments in imports are fairly common, so handling them seems worth an effort.
Yes, concatenating comments seems to be the best possible solution. That way no comments get lost and the user can edit them manually, if necessary. Concatenating with a comma might lead to ambiguity when the comments already contain commas:
from a import b # bar, foos a foobar
from a import f # foo, bars a foobar
That would become:
from a import b, f # bar, foos a foobar, foo, bars a foobar
Maybe better use a semicolon, as it is far less commonly used:
from a import b, f # bar, foos a foobar; foo, bars a foobar
from isort.
@Bengt, you're right a comma is too overused in comments to use as a separator here. I'll switch to a semicolon as suggested, thanks for pointing this out!
~Tim
from isort.
Fixed in version 3.6.0 :)
from isort.
Thanks for implementing this. It works almost as expected. I opened an issue for the remaining detail. See #129.
from isort.
Going back to the original example,
from a import f as f_small # more space efficient implementation
from b import f as f_quick # more time efficient implementation
If the user has specified these lines should simply be reordered, maintaining comments. Currently however, the comments are completely lost. I'd like to suggest re-opening this issue to address that problem.--force_single_line_imports
then
from isort.
Sorry, the above comment was slightly inaccurate. In fact, this is not an issue with --force_single_line_imports
; this is an issue with the default behavior:
from a import b # b comment
from a import c # c comment
is left unchanged, whereas
from a import b as bb # b comment
from a import c as cc # c comment
has its comments stripped.
from isort.
I've proposed a fix for the above issue in PR #179.
from isort.
Related Issues (20)
- Non-idempotency with commented multiline imports
- [self tests] test_black_pyi_file fails with black 24.2.0
- [self tests] test_isort_should_warn_on_empty_custom_config_issue_1433 fails against Pytest 8
- `--skip-gitignore` still traverses files/directories in .gitignore'd directories HOT 2
- Sorting order is broken and not following standard sort order HOT 13
- Third-party incorrectly identify as First-party depending on the name of the root directory
- isort.core.process incorrectly returns False if only changes to lines before imports were made, with no changes to raw import section
- Ran into `ERROR: Unrecoverable exception thrown when parsing <filepath>`
- Feature Request: Improved Handling of `# type: ignore` Comments to Preserve Mypy Compatibility
- Different order on same file depending on where you execute isort v5.13.2 HOT 1
- THIRDPARTY module detected as FIRSTPARTY when import happens within subdir of same name
- Black line length config support when using `--profile black`? HOT 1
- isort does not recognize _collections_abc as part of the standard library
- Unrecoverable exception thrown using --sort-reexports
- Feature Request: Automatic Conversion of Relative Imports to Absolute Imports
- isort doesn't format the imports in PyCharm's Jupyter notebooks
- False order of local imports relative to standard library imports
- Circular Import Issue Due to Import Reordering in Python 3.8
- Bug : isort add empty lines before pylint comments
- Deprecate `setuptools` integration comand
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from isort.