Skip to content

7122: error on encountering invalid extras #12987

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions news/7122.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Print an error message (and exit with a non-zero exit code) when an invalid extra is requested
36 changes: 35 additions & 1 deletion src/pip/_internal/exceptions.py
Original file line number Diff line number Diff line change
@@ -13,7 +13,16 @@
import re
import sys
from itertools import chain, groupby, repeat
from typing import TYPE_CHECKING, Dict, Iterator, List, Literal, Optional, Union
from typing import (
TYPE_CHECKING,
Dict,
Iterable,
Iterator,
List,
Literal,
Optional,
Union,
)

from pip._vendor.rich.console import Console, ConsoleOptions, RenderResult
from pip._vendor.rich.markup import escape
@@ -22,6 +31,8 @@
if TYPE_CHECKING:
from hashlib import _Hash

from pip._vendor.packaging.utils import NormalizedName
from pip._vendor.packaging.version import Version
from pip._vendor.requests.models import Request, Response

from pip._internal.metadata import BaseDistribution
@@ -775,3 +786,26 @@ def __init__(self, *, distribution: "BaseDistribution") -> None:
),
hint_stmt=None,
)


class UnavailableExtra(InstallationError):
"""A requested extra is not available."""

def __init__(
self,
base: str,
version: "Version",
extra: str,
available_extras: Iterable["NormalizedName"],
):
self.base = base
self.version = version
self.extra = extra
self.available_extras = available_extras

def __str__(self) -> str:
nice_available = " ".join(f'"{e}", ' for e in self.available_extras)[:-2]
return (
f"{self.base} {self.version} does not provide the extra '{self.extra}'"
f"\n{self.base} provides extras: {nice_available}"
)
11 changes: 6 additions & 5 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
InstallationSubprocessError,
MetadataInconsistent,
MetadataInvalid,
UnavailableExtra,
)
from pip._internal.metadata import BaseDistribution
from pip._internal.models.link import Link, links_equivalent
@@ -508,11 +509,11 @@ def iter_dependencies(self, with_requires: bool) -> Iterable[Optional[Requiremen
valid_extras = self.extras.intersection(self.base.dist.iter_provided_extras())
invalid_extras = self.extras.difference(self.base.dist.iter_provided_extras())
for extra in sorted(invalid_extras):
logger.warning(
"%s %s does not provide the extra '%s'",
self.base.name,
self.version,
extra,
raise UnavailableExtra(
base=self.base.name,
version=self.version,
extra=extra,
available_extras=self.base.dist.iter_provided_extras(),
)

for r in self.base.dist.iter_dependencies(valid_extras):