Skip to content

Commit 4448a72

Browse files
committed
Show constraint in error message
1 parent 03d5f56 commit 4448a72

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

src/pip/_internal/resolution/resolvelib/factory.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,26 @@ def _report_requires_python_error(
403403
)
404404
return UnsupportedPythonVersion(message)
405405

406-
def get_installation_error(self, e):
407-
# type: (ResolutionImpossible) -> InstallationError
406+
def _report_single_requirement_conflict(self, req, parent):
407+
# type: (Requirement, Candidate) -> DistributionNotFound
408+
if parent is None:
409+
req_disp = str(req)
410+
else:
411+
req_disp = '{} (from {})'.format(req, parent.name)
412+
logger.critical(
413+
"Could not find a version that satisfies the "
414+
"requirement %s", req_disp,
415+
)
416+
return DistributionNotFound(
417+
'No matching distribution found for {}'.format(req)
418+
)
419+
420+
def get_installation_error(
421+
self,
422+
e, # type: ResolutionImpossible
423+
constraints, # type: Dict[str, Constraint]
424+
):
425+
# type: (...) -> InstallationError
408426

409427
assert e.causes, "Installation error reported with no cause"
410428

@@ -424,17 +442,8 @@ def get_installation_error(self, e):
424442
# satisfied. We just report that case.
425443
if len(e.causes) == 1:
426444
req, parent = e.causes[0]
427-
if parent is None:
428-
req_disp = str(req)
429-
else:
430-
req_disp = '{} (from {})'.format(req, parent.name)
431-
logger.critical(
432-
"Could not find a version that satisfies the requirement %s",
433-
req_disp,
434-
)
435-
return DistributionNotFound(
436-
'No matching distribution found for {}'.format(req)
437-
)
445+
if req.name not in constraints:
446+
return self._report_single_requirement_conflict(req, parent)
438447

439448
# OK, we now have a list of requirements that can't all be
440449
# satisfied at once.
@@ -474,7 +483,11 @@ def describe_trigger(parent):
474483
"have conflicting dependencies.".format(info)
475484
logger.critical(msg)
476485
msg = "\nThe conflict is caused by:"
486+
487+
relevant_constraints = set()
477488
for req, parent in e.causes:
489+
if req.name in constraints:
490+
relevant_constraints.add(req.name)
478491
msg = msg + "\n "
479492
if parent:
480493
msg = msg + "{} {} depends on ".format(
@@ -484,6 +497,10 @@ def describe_trigger(parent):
484497
else:
485498
msg = msg + "The user requested "
486499
msg = msg + req.format_for_error()
500+
for key in relevant_constraints:
501+
msg += "\n The user requested (constraint) {}{}".format(
502+
key, constraints[key].specifier,
503+
)
487504

488505
msg = msg + "\n\n" + \
489506
"To fix this you could try to:\n" + \

src/pip/_internal/resolution/resolvelib/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def resolve(self, root_reqs, check_supported_wheels):
123123
)
124124

125125
except ResolutionImpossible as e:
126-
error = self.factory.get_installation_error(e)
126+
error = self.factory.get_installation_error(e, constraints)
127127
six.raise_from(error, e)
128128

129129
req_set = RequirementSet(check_supported_wheels=check_supported_wheels)

tests/functional/test_install_reqs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def test_constraints_local_editable_install_causes_error(
358358
assert 'Could not satisfy constraints' in result.stderr, str(result)
359359
else:
360360
# Because singlemodule only has 0.0.1 available.
361-
assert 'No matching distribution found' in result.stderr, str(result)
361+
assert 'Cannot install singlemodule 0.0.1' in result.stderr, str(result)
362362

363363

364364
@pytest.mark.network
@@ -387,7 +387,7 @@ def test_constraints_local_install_causes_error(
387387
assert 'Could not satisfy constraints' in result.stderr, str(result)
388388
else:
389389
# Because singlemodule only has 0.0.1 available.
390-
assert 'No matching distribution found' in result.stderr, str(result)
390+
assert 'Cannot install singlemodule 0.0.1' in result.stderr, str(result)
391391

392392

393393
def test_constraints_constrain_to_local_editable(

tests/functional/test_new_resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def test_new_resolver_constraint_on_dependency(script):
715715
@pytest.mark.parametrize(
716716
"constraint_version, expect_error, message",
717717
[
718-
("1.0", True, "ERROR: No matching distribution found for foo 2.0"),
718+
("1.0", True, "Cannot install foo 2.0"),
719719
("2.0", False, "Successfully installed foo-2.0"),
720720
],
721721
)

0 commit comments

Comments
 (0)