summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2019-03-07 23:28:27 -0800
committerTimothy Crosley <timothy.crosley@gmail.com>2019-03-07 23:28:27 -0800
commit16025e8b23116ab697247f6ac33b05c8a6fdbc77 (patch)
treea6e5078e73a175da699034a7ede80832c0571e32
parent6a73db44c8bd4671b503be069a409d268efb10a2 (diff)
parent9bdd130d76bffeef06239fd9de90dfdab85d593e (diff)
downloadisort-16025e8b23116ab697247f6ac33b05c8a6fdbc77.tar.gz
Merge branch 'develop' of https://github.com/timothycrosley/isort into develop
-rw-r--r--CHANGELOG.md10
-rw-r--r--isort/__init__.py2
-rw-r--r--isort/finders.py31
-rw-r--r--isort/isort.py3
-rw-r--r--isort/main.py2
-rwxr-xr-xsetup.py2
-rw-r--r--test_isort.py16
7 files changed, 54 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d4b66cae..48c2d77e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
Changelog
=========
+
### 5.0.0 UNRELEASED
**Breaking changes:**
- isort now requires Python 3.4+ to run but continues to support formatting
@@ -11,7 +12,14 @@ Internal:
Planned:
- profile support for common project types (black, django, google, etc)
-### 4.3.11 - March 3, 2019 - hot fix release
+### 4.3.13 - March 6, 2019 - hot fix release
+- Fixed the inability to accurately determine import section when a mix of conda and virtual environments are used.
+- Fixed some output being printed even when --quiet mode is enabled.
+
+### 4.3.12 - March 6, 2019 - hot fix release
+- Fix error caused when virtual environment not detected
+
+### 4.3.11 - March 6, 2019 - hot fix release
- Fixed issue #876: confused by symlinks pointing to virtualenv gives FIRSTPARTY not THIRDPARTY
- Fixed issue #873: current version skips every file on travis
- Additional caching to reduce performance regression introduced in 4.3.5
diff --git a/isort/__init__.py b/isort/__init__.py
index e3af789b..342606e8 100644
--- a/isort/__init__.py
+++ b/isort/__init__.py
@@ -22,4 +22,4 @@ OTHER DEALINGS IN THE SOFTWARE.
from . import settings # noqa: F401
from .isort import SortImports # noqa: F401
-__version__ = "4.3.10"
+__version__ = "4.3.12"
diff --git a/isort/finders.py b/isort/finders.py
index 11f481ab..75fe79a3 100644
--- a/isort/finders.py
+++ b/isort/finders.py
@@ -118,17 +118,13 @@ class PathFinder(BaseFinder):
def __init__(self, config: Mapping[str, Any], sections: Any) -> None:
super().__init__(config, sections)
- # Use a copy of sys.path to avoid any unintended modifications
- # to it - e.g. `+=` used below will change paths in place and
- # if not copied, consequently sys.path, which will grow unbounded
- # with duplicates on every call to this method.
- self.paths = list(sys.path)
# restore the original import path (i.e. not the path to bin/isort)
- self.paths[0] = os.getcwd()
+ self.paths = [os.getcwd()]
# virtual env
self.virtual_env = self.config.get('virtual_env') or os.environ.get('VIRTUAL_ENV')
- self.virtual_env = os.path.realpath(self.virtual_env)
+ if self.virtual_env:
+ self.virtual_env = os.path.realpath(self.virtual_env)
self.virtual_env_src = ''
if self.virtual_env:
self.virtual_env_src = '{0}/src/'.format(self.virtual_env)
@@ -142,6 +138,17 @@ class PathFinder(BaseFinder):
if os.path.isdir(path):
self.paths.append(path)
+ # conda
+ self.conda_env = self.config.get('conda_env') or os.environ.get('CONDA_PREFIX') or ''
+ if self.conda_env:
+ self.conda_env = os.path.realpath(self.conda_env)
+ for path in glob('{0}/lib/python*/site-packages'.format(self.conda_env)):
+ if path not in self.paths:
+ self.paths.append(path)
+ for path in glob('{0}/lib/python*/*/site-packages'.format(self.conda_env)):
+ if path not in self.paths:
+ self.paths.append(path)
+
# handle case-insensitive paths on windows
self.stdlib_lib_prefix = os.path.normcase(sysconfig.get_paths()['stdlib'])
if self.stdlib_lib_prefix not in self.paths:
@@ -150,12 +157,18 @@ class PathFinder(BaseFinder):
# handle compiled libraries
self.ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so"
+ # add system paths
+ for path in sys.path[1:]:
+ if path not in self.paths:
+ self.paths.append(path)
+
def find(self, module_name: str) -> Optional[str]:
for prefix in self.paths:
package_path = "/".join((prefix, module_name.split(".")[0]))
is_module = (exists_case_sensitive(package_path + ".py") or
exists_case_sensitive(package_path + ".so") or
- exists_case_sensitive(package_path + self.ext_suffix))
+ exists_case_sensitive(package_path + self.ext_suffix) or
+ exists_case_sensitive(package_path + "/__init__.py"))
is_package = exists_case_sensitive(package_path) and os.path.isdir(package_path)
if is_module or is_package:
if 'site-packages' in prefix:
@@ -164,6 +177,8 @@ class PathFinder(BaseFinder):
return self.sections.THIRDPARTY
if self.virtual_env and self.virtual_env_src in prefix:
return self.sections.THIRDPARTY
+ if self.conda_env and self.conda_env in prefix:
+ return self.sections.THIRDPARTY
if os.path.normcase(prefix).startswith(self.stdlib_lib_prefix):
return self.sections.STDLIB
return self.config['default_section']
diff --git a/isort/isort.py b/isort/isort.py
index 646c9d0d..6b8a781e 100644
--- a/isort/isort.py
+++ b/isort/isort.py
@@ -220,7 +220,8 @@ class SortImports(object):
if answer in ('quit', 'q'):
sys.exit(1)
with open(self.file_path, 'w', encoding=self.file_encoding, newline='') as output_file:
- print("Fixing {0}".format(self.file_path))
+ if not self.config['quiet']:
+ print("Fixing {0}".format(self.file_path))
output_file.write(self.output)
@property
diff --git a/isort/main.py b/isort/main.py
index 3e7cbc6a..767927d4 100644
--- a/isort/main.py
+++ b/isort/main.py
@@ -276,6 +276,8 @@ def parse_args(argv: Optional[Sequence[str]] = None) -> Dict[str, Any]:
help='Shows verbose output, such as when files are skipped or when a check is successful.')
parser.add_argument('--virtual-env', dest='virtual_env',
help='Virtual environment to use for determining whether a package is third-party')
+ parser.add_argument('--conda-env', dest='conda_env',
+ help='Conda environment to use for determining whether a package is third-party')
parser.add_argument('-vn', '--version-number', action='version', version=__version__,
help='Returns just the current version number without the logo')
parser.add_argument('-w', '--line-width', help='The max length of an import line (used for wrapping long imports).',
diff --git a/setup.py b/setup.py
index 3a4360b7..4e798eed 100755
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@ with open('README.rst') as f:
readme = f.read()
setup(name='isort',
- version='4.3.10',
+ version='4.3.12',
description='A Python utility / library to sort Python imports.',
long_description=readme,
author='Timothy Crosley',
diff --git a/test_isort.py b/test_isort.py
index 570a7c50..9b3c1e42 100644
--- a/test_isort.py
+++ b/test_isort.py
@@ -2740,6 +2740,22 @@ def test_command_line(tmpdir, capfd, multiprocess):
assert str(tmpdir.join("file2.py")) in out
+@pytest.mark.parametrize("quiet", (False, True))
+def test_quiet(tmpdir, capfd, quiet):
+ if sys.platform.startswith("win"):
+ return
+ from isort.main import main
+ tmpdir.join("file1.py").write("import re\nimport os")
+ tmpdir.join("file2.py").write("")
+ arguments = ["-rc", str(tmpdir)]
+ if quiet:
+ arguments.append("-q")
+ main(arguments)
+ out, err = capfd.readouterr()
+ assert not err
+ assert bool(out) != quiet
+
+
@pytest.mark.parametrize('enabled', (False, True))
def test_safety_excludes(tmpdir, enabled):
tmpdir.join("victim.py").write("# ...")