summaryrefslogtreecommitdiff
path: root/pylint/utils/utils.py
diff options
context:
space:
mode:
authorDamien Baty <damien.baty@polyconseil.fr>2020-07-05 23:06:04 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2020-08-18 09:12:33 +0200
commit9bc9bdf533213ba967b5199aa9f3246f08f8999c (patch)
treeed2ae7b060b5af1f051af393c7a6f1e9f4c500e7 /pylint/utils/utils.py
parent707fc4603b9d505bda68cf6adf9805882b99d73c (diff)
downloadpylint-git-9bc9bdf533213ba967b5199aa9f3246f08f8999c.tar.gz
Support both isort 4 and isort 5
The API of isort 5 (released on 2020-07-04) is completely different. We must still support isort 4 because isort 5 dropped the compatibility with Python 3.5, which pylint still supports. Note about the `known-standard-library` option: it has been included in pylint for years. Until now, it was mapped with the option of the same name in isort. However, isort 5 has changed the meaning of this option (see https://timothycrosley.github.io/isort/docs/upgrade_guides/5.0.0/#known_standard_library). Most users of pylint want the meaning of the new `extra-standard-library` option. To avoid a breaking change in pylint, the `known-standard-library` pylint option is now mapped to `known-standard-library` in isort 4, and `extra-standard-library` in isort 5. Users that really want the _new_ meaning of `known-standard-library` in isort 4 must disable the `wrong-import-order` check in pylint and run isort manually, outside of pylint. Fix #3722.
Diffstat (limited to 'pylint/utils/utils.py')
-rw-r--r--pylint/utils/utils.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/pylint/utils/utils.py b/pylint/utils/utils.py
index 21dd8d251..d7f1bf052 100644
--- a/pylint/utils/utils.py
+++ b/pylint/utils/utils.py
@@ -1,6 +1,15 @@
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
+try:
+ import isort.api
+
+ HAS_ISORT_5 = True
+except ImportError: # isort < 5
+ import isort
+
+ HAS_ISORT_5 = False
+
import codecs
import os
import re
@@ -398,3 +407,29 @@ def _ini_format(stream, options):
# remove trailing ',' from last element of the list
value = value[:-1]
print("%s=%s" % (optname, value), file=stream)
+
+
+class IsortDriver:
+ """A wrapper around isort API that changed between versions 4 and 5."""
+
+ def __init__(self, config):
+ if HAS_ISORT_5:
+ self.isort5_config = isort.api.Config(
+ # There is not typo here. EXTRA_standard_library is
+ # what most users want. The option has been named
+ # KNOWN_standard_library for ages in pylint and we
+ # don't want to break compatibility.
+ extra_standard_library=config.known_standard_library,
+ known_third_party=config.known_third_party,
+ )
+ else:
+ self.isort4_obj = isort.SortImports( # pylint: disable=no-member
+ file_contents="",
+ known_standard_library=config.known_standard_library,
+ known_third_party=config.known_third_party,
+ )
+
+ def place_module(self, package):
+ if HAS_ISORT_5:
+ return isort.api.place_module(package, self.isort5_config)
+ return self.isort4_obj.place_module(package)