pip: pip silently fails to install extras and returns error code

It seems that pip just returns success code on anything related to use of extras, commands like: pip install -e .[non_exiting_extra] reports success.

Even worse, with existing extras that may fail to install, the final result code is still a success, when in fact pip failed to install dependencies.

This is a critical bug because it directly affect CI usage where we can no longer trust result code from pip, bugs may go in unnoticed.

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 8
  • Comments: 52 (40 by maintainers)

Most upvoted comments

I don’t know a lot about the tradeoffs but I’ll add one more vote for changing this from a warning to an error. I probably should understand this better, but apparently if any transitive dependency is installed with an invalid extra you won’t be able to load entrypoints from an upstream package. e.g. Lets say package A depends on package B, and B requires C[invalid_extra], package A also has entrypoint "my_group": "my_extra=A.foo.Bar" Running

from pkg_resources import iter_entry_points
print([e.load() for e in iter_entry_points('my_group', 'my_extra')])

will give

pkg_resources.UnknownExtra: C X.X.X has no such extra feature 'invalid_extra'

This is a bug in package B, but it would be easy for it to slip through their CI unless someone happened to know this behavior (which is unexpected to me). Having pip error out on missing extras would make this easier to catch.

There’s an attribute user_supplied on the requirement object. https://github.com/pypa/pip/blob/main/src/pip/_internal/req/req_install.py#L157. That’s what I’d assume you should use - we definitely don’t want to end up with multiple different concepts of what’s a “top level” requirement…

Not fully ignore - I believe we trace a warning.

Returning non-zero would be backwards incompatible. I would be +1 on doing it (even aborting installation if we are not able to find an extra), but it would need to be changed over at least 1-2 releases. The first step would be to add to the existing warning to say that it’s unsupported and will fail in a future release. We’d also want to go through pypi and see how many packages make reference to nonexistent extras.

Just so I’m clear, “top-level requirement” means all the requirements collected before we enter the resolver? That is, for pip install, everything collected in reqs before here? https://github.com/pypa/pip/blob/main/src/pip/_internal/commands/install.py#L378

That is what I meant by this, yes.

Okay, to me, a reasonable compromise would be, if foo[extra]{specifier} is a top level requirement only (i.e. a user requirement) then add special logic that if pip can not find extra in foo for the first version it finds immediately error out with something like:

You selected foo[extra]{specifier} but the first version of foo that pip found {foo-version} does not have the extra extra, please specify a version of foo that has this extra

This could be added independently of whether pip treats extras as real requirements, and backtracks on them, and would handle the typo scenario.

P.S Sorry for the 3 posts in a row.

Your scenario is the same as mine, but made more complex to emphasise the problem it causes. My main point here is that someone gets inconvenienced no matter what we do, and complex build hierarchies with invalid extras embedded deep within them is a rare case, so I’d rather not optimise for that situation, but instead ensure that the common scenario (which frankly is pip install A[mistypd_extara]) is handled well. Downloading the whole history of A only to raise an error, is not (IMO) handling that situation well.

The problem with “extras should be treated as real requirements” is that it’s not even clear what that statement means. IMO the semantics of extras should be clarified at the standards level. Every tool needs to have a shared understanding of what depending on foo[extra] means, and I don’t think it helps users at all if (for example) pip and uv choose different answers to this question.

currently if you install foo[extra] you have no idea if you’re going to get [extra]

Hang on, that’s a bit of an extreme statement. If you install foo[extra] and foo provides extra, then it’ll be installed. The edge cases only arise when things aren’t that simple - either foo doesn’t provide extra, or it sometimes does and sometimes doesn’t, depending on version, or there’s an installed version of foo (where we have no way of knowing, because there’s no standard for recording it, whether extra was installed as well). Those are precisely the cases where I don’t think the standards give enough information on what foo[extra] means in the first place.

I’m not quite sure what you mean, if a user wants an error for foo 2.0 not having an extra, they should require foo[extra]==2.0 (or >= if they will accept newer versions).

As for how to implement this? I think you need to catch this exception in factory.py and skip it, similar to https://github.com/pypa/pip/pull/10625 or the reverse of https://github.com/pypa/pip/issues/10655 (there are other PRs that have implemented skips based on certain conditions, but I couldn’t quickly find them).

I would just like to say, I’m personally very supportive of this behavior change, I’ve always disliked that pip throws a warning but installs anyway. I’d always assumed this was a standard, but reading through this issue and carefully through the standards, it seems this was a mistaken assumption on my side.

I think there are two important things to understand:

  1. In general, pip will select a valid candidate given a requirement
  2. A user does not always get to control the requirements

For “1.” if foo has two published wheels, 2.0 and 1.0, and 2.0 is only available for Linux but the user is on Windows, and 1.0 is available for all platforms pip will select 1.0. Equally, if the user requires foo[extra] and 2.0 has no extra but 1.0 does, I would expect pip to choose 1.0.

For “2.”, a transitive dependency of a user’s requirement may require “foo[extra]” and so the requirement is out of control of the user. If it is a requirement that they must put transitive dependencies in their requirements file for pip not to throw an exception, e.g. “foo[extra]==1.0 # This is required for pip not to throw an error”, this is a bad user experience.

Came here to also say that it should be an error.

In “real world” scenarios, the amount of pip output is very often well more than a screen full so even noticing the “warning” is hard in those cases.

It seems very obvious to me that if I ask pip, “please install X with extra foo” and it can’t do that (because there’s no “foo” extra, or any other reason) then it is an error.

Compounding this is the fact that there’s no way to ASK what the extras even are.

Yesterday I found case where latest pip went into endless loop trying to resolve dependencies when told to use new resolver. I did not had time to build a reproducer yet but this made me think that maybe the new resolver is not yet ready for prime time.

A couple of months back I started to add PIP_USE_FEATURE=2020-resolver to CI pipelines (usually in tox.ini) for hardening testing. It did uncover conflicts quite often and I suspect that on the day this will become default we will have a huge number of confused users.

I would personally promote more early testing, especially on CI more before doing the big switch.