Giter Site home page Giter Site logo

Handle inline comments for imports about isort HOT 8 CLOSED

Bengt avatar Bengt commented on May 14, 2024
Handle inline comments for imports

from isort.

Comments (8)

timothycrosley avatar timothycrosley commented on May 14, 2024

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.

Bengt avatar Bengt commented on May 14, 2024

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.

timothycrosley avatar timothycrosley commented on May 14, 2024

@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.

timothycrosley avatar timothycrosley commented on May 14, 2024

Fixed in version 3.6.0 :)

from isort.

Bengt avatar Bengt commented on May 14, 2024

Thanks for implementing this. It works almost as expected. I opened an issue for the remaining detail. See #129.

from isort.

dandavison avatar dandavison commented on May 14, 2024

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 --force_single_line_imports then 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.

from isort.

dandavison avatar dandavison commented on May 14, 2024

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.

dandavison avatar dandavison commented on May 14, 2024

I've proposed a fix for the above issue in PR #179.

from isort.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.