yapf icon indicating copy to clipboard operation
yapf copied to clipboard

Ignore imports

Open jirikuncar opened this issue 8 years ago • 17 comments

Is there a way to skip formatting (sorting) of imports or forward these lines to an external tool such as ISort?

jirikuncar avatar Jul 24 '17 08:07 jirikuncar

Is this in the pipeline at all? perhaps an import_penalty variable for any modifications made to import lines?

TortoiseHam avatar Sep 04 '19 17:09 TortoiseHam

I'm not 100% sure what @jirikuncar has in mind. (I'm sorry I missed this bug report at the time.) Is it simply to leave the imports alone and let ISort figure them out?

bwendling avatar Sep 07 '19 05:09 bwendling

From my perspective it'd be great if they were left alone. I'm using pycharm to do import sorting, and then yapf as an on-save action. The problem I'm facing is that there's then no way to make pycharm win the fight as to how imports should look.

TortoiseHam avatar Sep 08 '19 17:09 TortoiseHam

YAPF shouldn't be sorting import statements though, just reformatting them. Could you give an example of what YAPF's doing here?

bwendling avatar Sep 09 '19 07:09 bwendling

the issue is when there are many imports on a single line such that the max line length is exceeded. I have pycharm set to do something like:

from package import a,b,c,d,e, f,g,h

but yapf wants to make it either: from package import a, b, c, etc.

or

from package import a,b,c,d,e,f,g,h

TortoiseHam avatar Sep 12 '19 05:09 TortoiseHam

For function calls or lists etc, I prefer to have split_all_top_level_comma_separated_values=true but not for imports, on this, I would rather use isort's reformatting, but yapf will not like it, so there's no way to make both of them not to reformat imports after the other (i.e. they're fighting each other).

An option for yapf to ignore imports would prove useful.

hydrargyrum avatar Dec 13 '19 15:12 hydrargyrum

+1 on the above comment. I have an isort set up that seems to conflict with the yapf set up and can't seem to be able to make YAPF to not reformat isort format.

casassg avatar Jan 24 '20 21:01 casassg

Agreed, this would be awesome!

My .isort.cfg file is:

[settings]
multi_line_output=5

(to see what "5" looks like, see here)

and it seems this won't play nice with YAPF where my .style.yapf is:

[style]
based_on_style = google

Garrett-R avatar Mar 19 '20 07:03 Garrett-R

A knob BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES will be available in version 0.31.0 of YAPF

arareko avatar Aug 20 '20 04:08 arareko

any idea when 0.31.0 would be available ?

angiarao avatar Nov 11 '20 16:11 angiarao

any idea when 0.31.0 would be available ?

No idea about the release schedule for YAPF. Probably a question for @gwelymernans to answer.

arareko avatar Nov 11 '20 23:11 arareko

Any news on this? I'm using django profile for isort, but it clashes with Yapf's formatting for tuples. Not even setting include_trailing_comma = false and the same line length solves the problem.

Isort:

from rest_framework_simplejwt.serializers import (
    TokenObtainPairSerializer as ObtainPairSerializer,
    TokenRefreshSerializer as RefreshSerializer
)

Yapf:

from rest_framework_simplejwt.serializers import (
    TokenObtainPairSerializer as ObtainPairSerializer, TokenRefreshSerializer as
    RefreshSerializer
)

As Yapf does not recognize the A as B as a whole as isort does, it fills the line length as much as possible.

Yapf could either support that "Isort multi_line_output 5 style" or completely ignore import statements, even when containing tuples.


Update: I've tried setting split_penalty_import_names = 9999 but I get the same output.

LECbg avatar Jul 01 '21 11:07 LECbg

@LECbg Try with a trailing comma for each import, both isort and YAPF should respect that:

from rest_framework_simplejwt.serializers import (
    TokenObtainPairSerializer as ObtainPairSerializer,
    TokenRefreshSerializer as RefreshSerializer,
)

arareko avatar Jul 07 '21 20:07 arareko

@arareko I had to instruct isort not to put the last comma, so this:

from django.db import (
    DJANGO_VERSION_PICKLE_KEY, IntegrityError, NotSupportedError, connections,
    router, transaction,
)

wouldn't turn into this:

from django.db import (
    DJANGO_VERSION_PICKLE_KEY,
    IntegrityError,
    NotSupportedError,
    connections,
    router,
    transaction,
)

This is an example taken from Django itself, and it could be even worse, these 4 lines would turn into 13:

from django.db.utils import (
    DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, ConnectionHandler,
    ConnectionRouter, DatabaseError, DataError, Error, IntegrityError,
    InterfaceError, InternalError, NotSupportedError, OperationalError,
    ProgrammingError,
)

Maybe I sould've stated this in the first place to provide some context.

I could also disable Yapf for each import statement using parentheses, but I think that work overload is not the point of an auto-formatter...

So, without the last comma, Yapf respects isort formatting, except in this exact case where isort treats the A as B structure as a whole and Yapf doesn't. (Btw, I would like to use that comma, but I can't if I use Yapf as I've shown above).

Since Yapf is not meant to deal with imports, I understand it shouldn't care about this A as B structure, but, to let us developers to use another tool that cares about imports formatting, Yapf should completely ignore them.

LECbg avatar Jul 12 '21 22:07 LECbg

+1 to this, we use isort as well for imports. @arareko is there any plan on adding the config setting? I dont mind helping if given some pointers, because this is making us unable to use the awesome formatting powers of yapf.

manugarri avatar Jun 19 '23 08:06 manugarri

+1 Waiting for this feature.

bednarek-p avatar Oct 22 '23 19:10 bednarek-p

My initial thoughts on how to achieve this:

  1. create a new knob called something like DISABLE_ENDING_COMMA_HEURISTIC_FOR_IMPORTS,
  2. add a new "subtype" that identifies the list of imports as belonging to IMPORT_MODULE or something similar,
  3. look for DISABLE_ENDING_COMMA_HEURISTIC in pytree_unwrapper.py, and
  4. place a check in there for the IMPORT_MODULE subtype and the DISABLE_ENDING_COMMA_HEURISTIC style to prevent the comma splitting.

That would be the simplest way to deal with this I think.

bwendling avatar Oct 25 '23 17:10 bwendling