Skip to content
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

Provide structured JSON output for pip index versions #13194

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

KrishanBhasin
Copy link

The first part of #13188, providing a structured JSON-formatted output for pip index versions command.

Usage:

❯ pip index versions build --json
{"name": "build", "versions": ["1.2.2.post1", "1.2.2", "1.2.1", "1.1.1", "1.0.3", "1.0.0", "0.10.0", "0.9.0", "0.8.0", "0.7.0", "0.6.0.post1", "0.5.1", "0.5.0", "0.4.0", "0.3.1.post1", "0.3.1", "0.3.0", "0.2.1", "0.2.0", "0.1.0", "0.0.4", "0.0.3.1", "0.0.2", "0.0.1"], "latest": "1.2.2.post1", "installed_version": "1.2.2.post1"}

which is properly formatted JSON - it prettifies to:

{
  "name": "build",
  "versions": [
    "1.2.2.post1",
    "1.2.2",
    "1.2.1",
    "1.1.1",
    "1.0.3",
    "1.0.0",
    "0.10.0",
    "0.9.0",
    "0.8.0",
    "0.7.0",
    "0.6.0.post1",
    "0.5.1",
    "0.5.0",
    "0.4.0",
    "0.3.1.post1",
    "0.3.1",
    "0.3.0",
    "0.2.1",
    "0.2.0",
    "0.1.0",
    "0.0.4",
    "0.0.3.1",
    "0.0.2",
    "0.0.1"
  ],
  "latest": "1.2.2.post1",
  "installed_version": "1.2.2.post1"
}

@KrishanBhasin KrishanBhasin changed the title Kb/f/pip index versions json Provide structured JSON output for pip index versions Jan 30, 2025
@KrishanBhasin KrishanBhasin marked this pull request as ready for review January 31, 2025 05:27

write_output(json.dumps(structured_output))
return

Copy link
Member

Choose a reason for hiding this comment

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

Can this be structured as follows:

if options.json:
    ...
else:
    ...

rather than

if options.json:
    ...
    return
...

as the former is (IMO) easier to read.

I'm also slightly uncomfortable with the fact that we're duplicating the lookup of dist here and in print_dist_installation_info. Ideally, I'd like to see this refactored to remove the duplication. But it's not a showstopper, and if you prefer it could be deferred to a follow-up PR.

Copy link
Author

Choose a reason for hiding this comment

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

I've done the first suggested change - I agree it reads nicer.

Regarding the second one - I also felt a bit uncomfortable with the duplicated lookup, but I wanted to keep the PR small

def print_dist_installation_info(name: str, latest: str) -> None:
env = get_default_environment()
dist = env.get_distribution(name)
def print_dist_installation_info_if_exists(
Copy link
Member

Choose a reason for hiding this comment

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

There's no real need to add _if_exists to the name - print_dist_installation_info(latest, dist) seems perfectly understandable to me. Apart from this minor point, this looks good, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants