diff --git a/news/6513.bugfix b/news/6513.bugfix new file mode 100644 index 00000000000..0e1757ae7b8 --- /dev/null +++ b/news/6513.bugfix @@ -0,0 +1,2 @@ +Include more details in the log message if ``pip freeze`` can't generate a +requirement string for a particular distribution. diff --git a/src/pip/_internal/operations/freeze.py b/src/pip/_internal/operations/freeze.py index c79a785c55b..03e8696bbb7 100644 --- a/src/pip/_internal/operations/freeze.py +++ b/src/pip/_internal/operations/freeze.py @@ -60,10 +60,14 @@ def freeze( user_only=user_only): try: req = FrozenRequirement.from_dist(dist) - except RequirementParseError: + except RequirementParseError as exc: + # We include dist rather than dist.project_name because the + # dist string includes more information, like the version and + # location. We also include the exception message to aid + # troubleshooting. logger.warning( - "Could not parse requirement: %s", - dist.project_name + 'Could not generate requirement for distribution %r: %s', + dist, exc ) continue if exclude_editable and req.editable: diff --git a/tests/functional/test_freeze.py b/tests/functional/test_freeze.py index 4016b5c8662..68625ff4338 100644 --- a/tests/functional/test_freeze.py +++ b/tests/functional/test_freeze.py @@ -108,12 +108,19 @@ def fake_install(pkgname, dest): '...{}==1.0...'.format(pkgname.replace('_', '-')) ) for pkgname in invalid_pkgnames: - _check_output( - result.stderr, - '...Could not parse requirement: {}\n...'.format( - pkgname.replace('_', '-') - ) + # Check that the full distribution repr is present. + dist_repr = '{} 1.0 ('.format(pkgname.replace('_', '-')) + expected = ( + '...Could not generate requirement for ' + 'distribution {}...'.format(dist_repr) ) + _check_output(result.stderr, expected) + + # Also check that the parse error details occur at least once. + # We only need to find one occurrence to know that exception details + # are logged. + expected = '...site-packages): Parse error at "...' + _check_output(result.stderr, expected) @pytest.mark.git