Skip to content

Commit

Permalink
Merge pull request #928 from di/use-pip-api
Browse files Browse the repository at this point in the history
Use pip-api instead of importing pip
  • Loading branch information
timothycrosley committed Apr 18, 2019
1 parent 4ba6380 commit 807943e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
14 changes: 4 additions & 10 deletions isort/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
pipreqs = None

try:
# pip>=10
from pip._internal.download import PipSession
from pip._internal.req import parse_requirements
from pip_api import parse_requirements
except ImportError:
try:
from pip.download import PipSession
from pip.req import parse_requirements
except ImportError:
parse_requirements = None
parse_requirements = None

try:
from requirementslib import Pipfile
Expand Down Expand Up @@ -321,8 +315,8 @@ def _get_names_cached(cls, path):
results = []

with chdir(os.path.dirname(path)):
requirements = parse_requirements(path, session=PipSession())
for req in requirements:
requirements = parse_requirements(path)
for req in requirements.values():
if req.name:
results.append(req.name)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
extras_require={
'pipfile': ['pipreqs', 'requirementslib'],
'pyproject': ['toml'],
'requirements': ['pip', 'pipreqs'],
'requirements': ['pip', 'pipreqs', 'pip-api'],
'xdg_home': ['appdirs>=1.4.0'],
},
install_requires=[
Expand Down
6 changes: 1 addition & 5 deletions test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2573,8 +2573,6 @@ def test_new_lines_are_preserved():
os.remove(n_newline.name)


@pytest.mark.skipif(not finders.RequirementsFinder.enabled, reason='RequirementsFinder not enabled (too old version of pip?)')
@pytest.mark.skipif(not finders.pipreqs, reason='pipreqs is missing')
def test_requirements_finder(tmpdir):
subdir = tmpdir.mkdir('subdir').join("lol.txt")
subdir.write("flask")
Expand All @@ -2594,7 +2592,7 @@ def test_requirements_finder(tmpdir):
files = list(finder._get_files())
assert len(files) == 1 # file finding
assert files[0].endswith('requirements.txt') # file finding
assert list(finder._get_names(str(req_file))) == ['Django', 'deal'] # file parsing
assert set(finder._get_names(str(req_file))) == {'Django', 'deal'} # file parsing

assert finder.find("django") == si.sections.THIRDPARTY # package in reqs
assert finder.find("flask") is None # package not in reqs
Expand Down Expand Up @@ -2651,8 +2649,6 @@ def test_forced_separate_is_deterministic_issue_774(tmpdir):
"""


@pytest.mark.skipif(not finders.PipfileFinder.enabled, reason='PipfileFinder not enabled (missing requirementslib?)')
@pytest.mark.skipif(not finders.pipreqs, reason='pipreqs is missing')
def test_pipfile_finder(tmpdir):
pipfile = tmpdir.join('Pipfile')
pipfile.write(PIPFILE)
Expand Down

0 comments on commit 807943e

Please sign in to comment.