Skip to content

Commit

Permalink
Added clearer error reporting when skipping pre-releases (#655)
Browse files Browse the repository at this point in the history
* Added clearer error reporting when skipping pre-releases

* Added correct pre/non-pre report and added tests for NoCandidateFound
  • Loading branch information
wolph authored Apr 26, 2018
1 parent 3d9e0ec commit 8d8d4ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
34 changes: 28 additions & 6 deletions piptools/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,44 @@ class PipToolsError(Exception):


class NoCandidateFound(PipToolsError):
def __init__(self, ireq, candidates_tried, index_urls):
def __init__(self, ireq, candidates_tried, finder):
self.ireq = ireq
self.candidates_tried = candidates_tried
self.index_urls = index_urls
self.finder = finder

def __str__(self):
sorted_versions = sorted(c.version for c in self.candidates_tried)
versions = []
pre_versions = []

for candidate in sorted(self.candidates_tried):
version = str(candidate.version)
if candidate.version.is_prerelease:
pre_versions.append(version)
else:
versions.append(version)

lines = [
'Could not find a version that matches {}'.format(self.ireq),
'Tried: {}'.format(', '.join(str(version) for version in sorted_versions) or '(no version found at all)')
]
if sorted_versions:

if versions:
lines.append('Tried: {}'.format(', '.join(versions)))

if pre_versions:
if self.finder.allow_all_prereleases:
line = 'Tried'
else:
line = 'Skipped'

line += ' pre-versions: {}'.format(', '.join(pre_versions))
lines.append(line)

if versions or pre_versions:
lines.append('There are incompatible versions in the resolved dependencies.')
else:
lines.append('No versions found')
lines.append('{} {} reachable?'.format(
'Were' if len(self.index_urls) > 1 else 'Was', ' or '.join(self.index_urls))
'Were' if len(self.finder.index_urls) > 1 else 'Was', ' or '.join(self.finder.index_urls))
)
return '\n'.join(lines)

Expand Down
2 changes: 1 addition & 1 deletion piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def find_best_match(self, ireq, prereleases=None):
# Reuses pip's internal candidate sort key to sort
matching_candidates = [candidates_by_version[ver] for ver in matching_versions]
if not matching_candidates:
raise NoCandidateFound(ireq, all_candidates, self.finder.index_urls)
raise NoCandidateFound(ireq, all_candidates, self.finder)
best_candidate = max(matching_candidates, key=self.finder._candidate_sort_key)

# Turn the candidate into a pinned InstallRequirement
Expand Down
24 changes: 24 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,27 @@ def test_filter_pip_markes():
assert '--output-file requirements.txt' in out.output
assert 'six==1.10.0' in out.output
assert 'unknown_package' not in out.output


def test_no_candidates():
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write('six>1.0b0,<1.0b0')

out = runner.invoke(cli, ['-n', 'requirements'])

assert out.exit_code == 2
assert 'Skipped pre-versions:' in out.output


def test_no_candidates_pre():
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements', 'w') as req_in:
req_in.write('six>1.0b0,<1.0b0')

out = runner.invoke(cli, ['-n', 'requirements', '--pre'])

assert out.exit_code == 2
assert 'Tried pre-versions:' in out.output
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ envlist =
py{27,34,35,36,py}
flake8
readme
skip_missing_interpreters = True

[testenv]
deps =
Expand Down

0 comments on commit 8d8d4ea

Please sign in to comment.