Ignore imports
Is there a way to skip formatting (sorting) of imports or forward these lines to an external tool such as ISort?
Is this in the pipeline at all? perhaps an import_penalty variable for any modifications made to import lines?
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?
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.
YAPF shouldn't be sorting import statements though, just reformatting them. Could you give an example of what YAPF's doing here?
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
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.
+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.
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
A knob BLANK_LINES_BETWEEN_TOP_LEVEL_IMPORTS_AND_VARIABLES will be available in version 0.31.0 of YAPF
any idea when 0.31.0 would be available ?
any idea when 0.31.0 would be available ?
No idea about the release schedule for YAPF. Probably a question for @gwelymernans to answer.
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 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 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.
+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.
+1 Waiting for this feature.
My initial thoughts on how to achieve this:
- create a new knob called something like
DISABLE_ENDING_COMMA_HEURISTIC_FOR_IMPORTS, - add a new "subtype" that identifies the list of imports as belonging to
IMPORT_MODULEor something similar, - look for
DISABLE_ENDING_COMMA_HEURISTICinpytree_unwrapper.py, and - place a check in there for the
IMPORT_MODULEsubtype and theDISABLE_ENDING_COMMA_HEURISTICstyle to prevent the comma splitting.
That would be the simplest way to deal with this I think.