Skip to content

Include editable locations in JSON output of list #7670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions news/7664.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Include ``location`` field for editable distribution entries in
``pip list --json``, so the fields outputted match the default "columns"
format.
3 changes: 2 additions & 1 deletion src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ def format_for_json(packages, options):
'name': dist.project_name,
'version': str(dist.version),
}
if options.verbose >= 1:
if options.verbose >= 1 or dist_is_editable(dist):
info['location'] = dist.location
if options.verbose >= 1:
info['installer'] = get_installer(dist)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not feel right to me to have the verbose flag influence the content of a json output.
What is also not completely right is to have the location field having two semantics: the editable project location and the installed location. These are two different things that should go in different fields IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The location semantic would also affect the column format. It has also existed for a while now so the compatibility issue would be important.

Regarding verbosity, I have no problem always outputting all fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the concern was backwards compatibility?

If this mode of output does not feel very reasonable, I don't mind changing it but I'm also not sure if there's a clear transition path to introduce these additional fields and who it might affect if we do so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, agreed. One solution I can think of is to introduce a --fields option that explicitly tells pip what fields to output (the default being to infer from verbosity).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think our tests breaking because of new fields does not necessarily indicate that there are people out there relying on the absence of such fields. So I'd still be inclined to add the fields unconditionally and adapting our tests.

if options.outdated:
info['latest_version'] = str(dist.latest_version)
Expand Down
48 changes: 34 additions & 14 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ def test_uptodate_flag(script, data):
'list', '-f', data.find_links, '--no-index', '--uptodate',
'--format=json',
)
assert {"name": "simple", "version": "1.0"} \
not in json.loads(result.stdout) # 3.0 is latest
assert {"name": "pip-test-package", "version": "0.1.1"} \
in json.loads(result.stdout) # editables included
assert {"name": "simple2", "version": "3.0"} in json.loads(result.stdout)
json_result = json.loads(result.stdout)

# 3.0 is latest
assert "simple" not in {d["name"] for d in json_result}
# editables included
assert {
"name": "pip-test-package",
"version": "0.1.1",
"location": os.path.normcase(
str(script.venv_path / "src" / "pip-test-package")
),
} in json_result
assert {"name": "simple2", "version": "3.0"} in json_result


@pytest.mark.network
Expand Down Expand Up @@ -212,15 +220,27 @@ def test_outdated_flag(script, data):
'list', '-f', data.find_links, '--no-index', '--outdated',
'--format=json',
)
assert {"name": "simple", "version": "1.0",
"latest_version": "3.0", "latest_filetype": "sdist"} \
in json.loads(result.stdout)
assert dict(name="simplewheel", version="1.0",
latest_version="2.0", latest_filetype="wheel") \
in json.loads(result.stdout)
assert dict(name="pip-test-package", version="0.1",
latest_version="0.1.1", latest_filetype="sdist") \
in json.loads(result.stdout)
assert {
"name": "simple",
"version": "1.0",
"latest_version": "3.0",
"latest_filetype": "sdist",
} in json.loads(result.stdout)
assert {
"name": "simplewheel",
"version": "1.0",
"latest_version": "2.0",
"latest_filetype": "wheel",
} in json.loads(result.stdout)
assert {
"name": "pip-test-package",
"version": "0.1",
"location": os.path.normcase(
str(script.venv_path / "src" / "pip-test-package")
),
"latest_version": "0.1.1",
"latest_filetype": "sdist",
} in json.loads(result.stdout)
assert "simple2" not in {p["name"] for p in json.loads(result.stdout)}


Expand Down
17 changes: 13 additions & 4 deletions tests/functional/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,14 @@ def test_uninstall_setuptools_develop_install(script, data):
script.run('python', 'setup.py', 'install',
expect_stderr=True, cwd=pkg_path)
list_result = script.pip('list', '--format=json')
assert {"name": os.path.normcase("FSPkg"), "version": "0.1.dev0"} \
in json.loads(list_result.stdout), str(list_result)
egg_name = "FSPkg-0.1.dev0-py{0}.{1}.egg".format(*sys.version_info)
assert {
"name": os.path.normcase("FSPkg"),
"version": "0.1.dev0",
"location": os.path.normcase(
str(script.site_packages_path / egg_name)
),
} in json.loads(list_result.stdout), str(list_result)
# Uninstall both develop and install
uninstall = script.pip('uninstall', 'FSPkg', '-y')
assert any(filename.endswith('.egg')
Expand All @@ -534,8 +540,11 @@ def test_uninstall_editable_and_pip_install(script, data):
script.pip('install', '--ignore-installed', '.',
expect_stderr=True, cwd=pkg_path)
list_result = script.pip('list', '--format=json')
assert {"name": "FSPkg", "version": "0.1.dev0"} \
in json.loads(list_result.stdout)
assert {
"name": "FSPkg",
"version": "0.1.dev0",
"location": os.path.normcase(str(script.site_packages_path)),
} in json.loads(list_result.stdout)
# Uninstall both develop and install
uninstall = script.pip('uninstall', 'FSPkg', '-y')
assert not any(filename.endswith('.egg-link')
Expand Down