Skip to content

Commit

Permalink
[REF] requirements: support both isort 4 and 5
Browse files Browse the repository at this point in the history
isort>5 is only compatible with py36 and above, therefore we use a ISortDriver to support both versions and that it's not necessary to pin only one version.
  • Loading branch information
fernandahf committed Jan 22, 2021
1 parent 2388d94 commit dd470b3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re

import astroid
import isort
import polib
from collections import defaultdict
from pylint.checkers import utils
Expand Down Expand Up @@ -429,8 +428,8 @@ def _check_imported_packages(self, node, module_name):
if self._is_module_name_in_whitelist(module_name):
# ignore whitelisted modules
return
isort_obj = isort.SortImports(file_contents='')
import_category = isort_obj.place_module(module_name)
isort_driver = misc.IsortDriver()
import_category = isort_driver.place_module(module_name)
if import_category not in ('FIRSTPARTY', 'THIRDPARTY'):
# skip if is not a external library or is a white list library
return
Expand Down
29 changes: 29 additions & 0 deletions pylint_odoo/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
except ImportError:
from whichcraft import which

try:
import isort.api

HAS_ISORT_5 = True
except ImportError: # isort < 5
import isort

HAS_ISORT_5 = False

DFTL_VALID_ODOO_VERSIONS = [
'4.2', '5.0', '6.0', '6.1', '7.0', '8.0', '9.0', '10.0', '11.0', '12.0',
'13.0', '14.0',
Expand Down Expand Up @@ -660,3 +669,23 @@ def parse_format(main_str, secondary_str):
# with the args and kwargs of the original string
# so it is a real error
raise StringParseError(repr(exc))


class IsortDriver:
"""
A wrapper around isort API that changed between versions 4 and 5.
Taken of https://git.io/Jt3dw
"""

def __init__(self):
if HAS_ISORT_5:
self.isort5_config = isort.api.Config()
else:
self.isort4_obj = isort.SortImports( # pylint: disable=no-member
file_contents=""
)

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)
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
docutils==0.16
isort==4.3.21
lxml>=4.2.3
polib==1.1.0
Pygments==2.2 ;python_version < '3'
Expand Down

0 comments on commit dd470b3

Please sign in to comment.