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

skip statements #295

Closed
simonzack opened this issue Aug 7, 2015 · 30 comments
Closed

skip statements #295

simonzack opened this issue Aug 7, 2015 · 30 comments
Labels
enhancement New feature or request

Comments

@simonzack
Copy link

Some statements need to be nested within imports, is there a way for isort to ignore them?

Here's the motivating example:

import matplotlib
matplotlib.use('Agg')  # isort:skip
import matplotlib.pyplot as plt

the use Agg must be called before the plt import. isort:skip doesn't work here.

@simonzack
Copy link
Author

I'm not sure how this would be implemented though as isort would not be aware of the proper ordering unless it's specified somehow.

@timothycrosley timothycrosley added the enhancement New feature or request label Sep 1, 2015
@timothycrosley
Copy link
Member

hi @simonzack,

Thanks for submitting this issue! I think this is one people run into a lot and don't report, I certainly run into it myself, so it's great to have it documented. After putting some thought into it, I think one potential solution would be to add a list of 'stop_patterns' that when contained in a line isort stops looking for imports. This isn't ideal, but would let you group all your normal imports together (and automatically be sorted) and have all your magic imports below, manually sorted. Ideally, the imports that really on magic such as matlib use, and sys.path.append, would generally be minimal.

What do you think of this idea?

Thanks again!

~Timothy

@tino
Copy link

tino commented Nov 11, 2015

👍 for this as well, got the same problem with matplotlib.

Would this work?

import logging

# isort:maintain_block
import matplotlib
matplotlib.use('Agg')  
# isort:end_maintain_block

import matplotlib.pyplot as plt

Meaning that isort can shuffle above and below the maintain_block, but not over it?

@simonzack
Copy link
Author

@timothycrosley Sorry about the late reply, I've forgot about this issue :( That's an interesting idea but it wouldn't sort the imports below the block. The following still looks nicer:

import logging
import matplotlib
matplotlib.use('Agg')
import yaml

from yaml import load

than this:

import logging
import yaml

from yaml import load

import matplotlib
matplotlib.use('Agg')

I think @tino 's solution can fix this, although isort could also treat the block as a single item to sort, so in this case it still uses the line import matplotlib.

Perhaps it could also be done inline:

import logging
import matplotlib  # isort:block
matplotlib.use('Agg')  # isort:end_block
import matplotlib.pyplot as plt

@blueyed
Copy link
Contributor

blueyed commented Dec 11, 2015

I think for starters it would be good if isort:skip would be kept with the line above.

My use case is with django-configurations, which has this pattern:

 from configurations import importer
 importer.install()  # noqa  # isort:skip

@tino
Copy link

tino commented Dec 12, 2015

Hmm, that might actually be a simple starting point indeed. I think it would have to work with multiple lines as well though, as these three need to be in this order (now that would work because of the default sorting, but that might not always be the case):

import matplotlib 
matplotlib.use('Agg')  # noqa  # isort:skip
import matplotlib.pyplot as plt  # isort:skip

@blueyed
Copy link
Contributor

blueyed commented Dec 12, 2015

I think this block would also be covered by "stick lines with isort:skip to the one above".

Only import matplotlib would then be sorted, with the other lines below as-is.

@elbenfreund
Copy link

elbenfreund commented Jun 3, 2016

I just wanted to provide another common use case. Gtk will output warning if no specific minimal version is specified:

import gi
gi.require_version('Gdk', '3.0')  # NOQA
gi.require_version('Gtk', '3.0')  # NOQA
from gi.repository import Gdk, Gtk

@elbenfreund
Copy link

Is there by any change news on this? Or a viable workaround?

One way or the other: thanks for all your work! :)

@cas--
Copy link

cas-- commented Jul 15, 2018

Thought I would share a viable workaround for isort and flake8:

import gi  # isort:skip
gi.require_version('PangoCairo', '1.0')  # NOQA: E402

# isort:imports-thirdparty
from gi.repository import PangoCairo

# isort:imports-firstparty
from my.module import MyClass

#isort:import-localfolder
from .common import util

@petobens
Copy link

This vim plugin prevents this situation by avoiding moving imports from the original placement: https://github.com/tweekmonster/impsort.vim

Can something like that be implemented? Are there any plans to work on this (i.e since the original issue has over 3 years now). Thanks!!

@petobens
Copy link

Hi @jdufresne I've noticed that you've making several changes to the library recently... any plans to tackle this issue? Thanks!

@Hnasar
Copy link

Hnasar commented Aug 21, 2019

I thought this was implemented with #679 but PR #843 says

Expected behavior is now that all isort:skip and NOQA imports will be moved to bottom, unsorted.

:(

Edit: There are a few other bugs which make me think the skip code probably needs an overhaul
:

  1. isort:skip comments and __future__ imports #455
  2. noqa needs to be in CAPS or else it won't be detected
  3. another weird bug when constants are mixed in
#!/usr/bin/python

"""Docstring."""

import os

import requests

constant = 5

import zed  # isort:skip
import ujson  # isort:skip

turns into

#!/usr/bin/python

import ujson  # isort:skip
import zed  # isort:skip
"""Docstring."""

import os

import requests

constant = 5

with version 4.3.21.

@timothycrosley
Copy link
Member

closing as the latest development branch (which will be release 5.0.0) includes extensive support for skipping statements as well as simply turning isort on and off over sections of code as needed.

@petobens
Copy link

petobens commented Feb 10, 2020

Hi @timothycrosley can you point to some examples showing how will this work? Thanks

@Mattwmaster58
Copy link

Mattwmaster58 commented Apr 24, 2020

@timothycrosley Hi, I was wondering if it were possible to skip rearrangement of certain blocks but still
rearrange within the block as someone in this thread has already mentioned?

eg: current behaviour:

import b

import c
import a

becomes

import a
import b
import c

Where a grouping comment would come in handy a work:

import b

# isort:block
import c
import a
# isort:block

becomes

import b

# isort:block
import a
import c
# isort: block

@PetterS
Copy link

PetterS commented May 6, 2020

@timothycrosley when will this version be released?

@sfdye
Copy link

sfdye commented May 14, 2020

Can we re-open this? It seems the latest released version does not allow you to skip a code statement and and version 5.0.0 is not released yet.

@duanyutong
Copy link

I'm running version 4.3.21 and it always wants to move the import marked as isort:skip to the end of all imports, instead of leaving it alone in place. Seems to be a related issue.

@Mattwmaster58
Copy link

@duanyutong one could interpret that as sort moving the imports after your skips to before.

@duanyutong
Copy link

@duanyutong one could interpret that as sort moving the imports after your skips to before.

Yes, either way, the same end result produced, not leaving skipped imports in place.

@thommiano
Copy link

@thommiano
Copy link

thommiano commented Sep 25, 2020

For anyone running isort < version 5, this works for me:

import matplotlib  # isort:skip

matplotlib.use("agg")  # noqa: E402
import matplotlib.pyplot as plt  # isort:skip

python --version

Python 3.7.0

isort --version

VERSION 4.3.21

flake8 --version

3.7.8 (mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.7.0 on Darwin

@exhuma
Copy link

exhuma commented Mar 22, 2021

For those looking for the official docs: https://pycqa.github.io/isort/#skip-processing-of-imports-outside-of-configuration

@earshinov
Copy link

earshinov commented Apr 2, 2021

I still don't understand how isort:skip is supposed to work. My code:

from __future__ import annotations

# noinspection PyUnresolvedReferences
from . import global_config  # isort:skip

from typing import List, NamedTuple

import psycopg2
import psycopg2.extras

becomes

from __future__ import annotations

from typing import List, NamedTuple

import psycopg2
import psycopg2.extras

# noinspection PyUnresolvedReferences
from . import global_config  # isort:skip

From the docs: "To make isort ignore a single import simply add a comment at the end of the import line containing the text isort:skip". Apparently me and isort have different ideas about what the verb "ignore" means in this case. Is there a way to prevent isort from moving the global_config import below the others other than disabling isort for the entire file?

(I am using isort@5.8.0)

@NoahCardoza
Copy link

Not sure if this will be helpful to anyone but:

I came here thinking I was facing the same issue until I realized that autopep8 will also try to move imports to the top of the file. If you happen to have pep8 installed try adding a .pep8 config file at the root of your project:

.pep8:

[pycodestyle]
ignore = E402

Might be worth noting I'm using VS Code.

@timothycrosley
Copy link
Member

Sorry everyone! I have a hard time keeping closed issues updated in response to comments. However, if anyone is wondering how to do the kind of "skip" that results in the import staying exactly where it is, (resulting in 2 import sections surrounding it), isort 5 has support for this using the "# isort: split" action comment. See the complete guide here: https://pycqa.github.io/isort/docs/configuration/action_comments.html

@ax3l
Copy link

ax3l commented Dec 17, 2021

Just to complete that section: how to combine noqa with isort: skip in a line?

import a  # noqa isort:skip

or

import a  # noqa  # isort: skip

?

@DhruvKoolRajamani
Copy link

Just add a , to separate the too.

import a # noqa: <Error>, isort: skip

@waketzheng
Copy link

Just add a , to separate the too.

import a # noqa: <Error>, isort: skip

Worked! Thanks~

But the way, the following lines not work as expected:

import a # isort: skip,noqa: <Error>    # Note: noqa: must be in front of isort:
import a # type:ignore,noqa: <Error>,isort: skip    # mypy/flake8 not work, isort work
import a # noqa: <Error>,isort: skip,type:ignore    # mypy not work, isort/flake8 work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests