Skip to content

Fix subcommands accepting --version - #14231

Open
7576457 wants to merge 7 commits into
pypa:mainfrom
7576457:fix/9456
Open

Fix subcommands accepting --version#14231
7576457 wants to merge 7 commits into
pypa:mainfrom
7576457:fix/9456

Conversation

@7576457

@7576457 7576457 commented Aug 5, 2026

Copy link
Copy Markdown

What does this PR do?

This PR forbids using pip <command> --version, leaving the --version option only for pip. At the same time, if such command is still executed, an error will be raised: no such option: --version

Example:

[hehs@nixos:~/dev/pip]$ pip wheel . --version

Usage:   
  pip wheel [options] <requirement specifier> ...
  pip wheel [options] -r <requirements file> ...
  pip wheel [options] [-e] <vcs project url> ...
  pip wheel [options] [-e] <local project path> ...
  pip wheel [options] <archive url/path> ...

no such option: --version

PR Checklist:

  • I agree to follow the PSF Code of Conduct.
  • I have read and have followed the CONTRIBUTING.md file.
  • I have added a news file fragment (or this PR does not need one).
  • I have read and followed the AI_POLICY.md file, and if any AI tools were used, I have disclosed it below.

@7576457

7576457 commented Aug 5, 2026

Copy link
Copy Markdown
Author

Do we need a test here?

@7576457
7576457 marked this pull request as ready for review August 5, 2026 14:59

@2ykwang 2ykwang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the root cause seems to be that --version is defined in the shared options group

https://github.com/pypa/pip/blob/main/src/pip/_internal/cli/cmdoptions.py#L1355-L1385

Comment thread src/pip/_internal/cli/base_command.py Outdated
)
options.cache_dir = None

if options.version:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

even with this check, --version still shows up in the subcommand's --help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yes, it stays in subcommands since --version lives in general_group. I already tried removing it from there for the subcommand, but that broke some tests. Maybe there are other options

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done.
found a way to suppress the hint without removing it :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One thing that feels a bit off to me.

current implementation adds special-case handling just for --version. If possible, wouldn't it be better to avoid this kind of special case?

--version is only meaningful on the top-level pip command. So rather than keeping it in general_group, defining and handling it only in the top-level parser seems like a more natural structure to me.

That way:

  • --version no longer shows up in pip <subcommand> --help
  • there's no need for separate special-case handling anymore
  • and future changes related to --version can be handled consistently at the parser level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I already tried removing it from there for the subcommand, but that broke some tests. Maybe there are other options

My guess is the broken tests came from removing version from general_group. pip --version itself could no longer be handled. Looking at the code where pip processes the top-level command might help.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

One thing that feels a bit off to me.

current implementation adds special-case handling just for --version. If possible, wouldn't it be better to avoid this kind of special case?

--version is only meaningful on the top-level pip command. So rather than keeping it in general_group, defining and handling it only in the top-level parser seems like a more natural structure to me.

That way:

* `--version` no longer shows up in `pip <subcommand> --help`

* there's no need for separate special-case handling anymore

* and future changes related to `--version` can be handled consistently at the parser level.

Damn, yeah, agreed. Much better this way.
Found how to do it. I ll remove it from general_options and add it as an option for pip, instead

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

@2ykwang

2ykwang commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

If possible, it would be nice to have a regression test for pip <subcommand> --version.
Other than that, this looks good to me!

@7576457

7576457 commented Aug 7, 2026

Copy link
Copy Markdown
Author

If possible, it would be nice to have a regression test for pip <subcommand> --version. Other than that, this looks good to me!

done

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.

2 participants