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
Comments
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. |
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 |
👍 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 |
@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:
than this:
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 Perhaps it could also be done inline:
|
I think for starters it would be good if My use case is with django-configurations, which has this pattern:
|
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):
|
I think this block would also be covered by "stick lines with Only |
I just wanted to provide another common use case. Gtk will output warning if no specific minimal version is specified:
|
Is there by any change news on this? Or a viable workaround? One way or the other: thanks for all your work! :) |
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 |
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!! |
Hi @jdufresne I've noticed that you've making several changes to the library recently... any plans to tackle this issue? Thanks! |
I thought this was implemented with #679 but PR #843 says
:( Edit: There are a few other bugs which make me think the skip code probably needs an overhaul
#!/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. |
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. |
Hi @timothycrosley can you point to some examples showing how will this work? Thanks |
@timothycrosley Hi, I was wondering if it were possible to skip rearrangement of certain blocks but still 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 |
@timothycrosley when will this version be released? |
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. |
I'm running version 4.3.21 and it always wants to move the import marked as |
@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. |
See latest release: https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0/ |
For anyone running isort < version 5, this works for me:
|
For those looking for the official docs: https://pycqa.github.io/isort/#skip-processing-of-imports-outside-of-configuration |
I still don't understand how 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 (I am using isort@5.8.0) |
Not sure if this will be helpful to anyone but: I came here thinking I was facing the same issue until I realized that
Might be worth noting I'm using VS Code. |
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 |
Just to complete that section: how to combine import a # noqa isort:skip or import a # noqa # isort: skip ? |
Just add a import a # noqa: <Error>, isort: skip |
Worked! Thanks~ But the way, the following lines not work as expected:
|
Some statements need to be nested within imports, is there a way for isort to ignore them?
Here's the motivating example:
the use Agg must be called before the plt import.
isort:skip
doesn't work here.The text was updated successfully, but these errors were encountered: