isort ordering issues with python-future

Created on 29 Aug 2019  路  8Comments  路  Source: PyCQA/isort

Hi,

We are currently doing the Python2to3 migration using futurize which adds the future.standard_library. According to the docs the standard_library should be added after __future__ but before any other imports.

Below you will find current isort configuration and source code snippets before and after isort.

What I want is to have future import and standard_library below __future__, but isort doesn't like that as future is a 3rd party package.

What I have before running isort

from __future__ import absolute_import

from future import standard_library

standard_library.install_aliases()  

import os
import re
import time

from logging.handlers import SysLogHandler

from builtins import len, object, str

from katlogger import log_formatter, log_rollover

from .query_elastic import QueryElastic

What I have after running isort

from __future__ import absolute_import

# Python Standard Library
import os
import re
import time

from builtins import len, object, str
from logging.handlers import SysLogHandler

# Third Library
from future import standard_library

# Explicitly Local
from .query_elastic import QueryElastic

# CAM Packages
from katlogger import log_formatter, log_rollover

standard_library.install_aliases()  # noqa: E402

What I want after isort

from __future__ import absolute_import

from future import standard_library
standard_library.install_aliases() 

# Python Standard Library
import os
import re
import time

from builtins import len, object, str
from logging.handlers import SysLogHandler

# Explicitly Local
from .query_elastic import QueryElastic

# CAM Packages
from katlogger import log_formatter, log_rollover

Configuration

[tool.isort]
force_grid_wrap = 0
include_trailing_comma = true
indent = 4
line_length = 90
multi_line_output = 3
# Having imports glued together is physically painful. ;)
lines_between_types = 1
# Imports sections
sections = ['FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER']
# Imports heading
import_heading_stdlib = 'Python Standard Library'
import_heading_thirdparty = 'Third Library'
import_heading_firstparty = 'CAM Packages'
import_heading_localfolder = 'Explicitly Local'

# A list of known imports that will be forced to display within their specified category.
known_first_party = ['katlogger']
bug enhancement good first issue

All 8 comments

I tried known_future_library=future but that didn't work.

I tried known_future_library=future but that didn't work.

I tried defining a new section, that also didn't work.

Bump!

The core issue here, as I understand it, isn't the ordering or sectioning of the imports, it's the fact that isort is pushing the imports above the code standard_library.install_aliases() which needs to happen before other steps take place. This is why, in practice, things like known_future_library=future don't work. a related thing is that the __future__ section is referred to as FUTURE_LIBRARY so you may want to use FUTURE_THIRDPARTY section for the pypi
"future" library. Either way this is verified fixed in develop and will make its way out in the soon to be released 5.0.0 version.

Thanks!

~Timothy

Thank you Timothy.

Hey there, I tried to use develop to get the benefits of this issue, but I'm not seeing the correct result. Even when I run isort with the settings from the test and pass the exact test case contents, I get a different result:

pyproject.toml

[tool.isort]
force_grid_wrap=false
include_trailing_comma=true
indent=4
line_length=90
multi_line_output=3
lines_between_types=1
sections=[
"FUTURE_LIBRARY",
"FUTURE_THIRDPARTY",
"STDLIB",
"THIRDPARTY",
"FIRSTPARTY",
"LOCALFOLDER",
]
import_heading_stdlib="Python Standard Library"
import_heading_thirdparty="Third Library"
import_heading_firstparty="CAM Packages"
import_heading_localfolder="Explicitly Local"
known_first_party=["katlogger"]
known_future_thirdparty=["future"]

[build-system]
requires = ["setuptools>=40.8.0", "wheel"]
build-backend = "setuptools.build_meta:__legacy__"

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "2.7"

sample.py < IN

from __future__ import absolute_import

from future import standard_library

standard_library.install_aliases()

import os
import re
import time

from logging.handlers import SysLogHandler

from builtins import len, object, str

from katlogger import log_formatter, log_rollover

from .query_elastic import QueryElastic

sample.py > OUT

from __future__ import absolute_import

from future import standard_library

# Python Standard Library
import os
import re
import time

from builtins import len, object, str
from logging.handlers import SysLogHandler

# CAM Packages
from katlogger import log_formatter, log_rollover

# Explicitly Local
from .query_elastic import QueryElastic

standard_library.install_aliases()

Am I missing something?

As an additional point, when moving the standard_library.install_aliases() down in my original source file, I get about 8 extra blank lines between imports and the logger being defined

@tockards

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pradyunsg picture pradyunsg  路  3Comments

ionelmc picture ionelmc  路  3Comments

peteboothroyd picture peteboothroyd  路  3Comments

cjerdonek picture cjerdonek  路  3Comments

whg517 picture whg517  路  3Comments