Skip to content

Commit

Permalink
review comment: subclass instead of constructor flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderr committed Sep 6, 2023
1 parent 9041602 commit 4e73e3e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/pip/_internal/resolution/resolvelib/factory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import contextlib
import itertools
import functools
import logging
from typing import (
Expand Down Expand Up @@ -63,6 +62,7 @@
ExplicitRequirement,
RequiresPythonRequirement,
SpecifierRequirement,
SpecifierWithoutExtrasRequirement,
UnsatisfiableRequirement,
)

Expand Down Expand Up @@ -467,7 +467,7 @@ def _make_requirements_from_install_req(
yield from ()
elif not ireq.link:
if ireq.extras and ireq.req is not None and ireq.req.specifier:
yield SpecifierRequirement(ireq, drop_extras=True),
yield SpecifierWithoutExtrasRequirement(ireq),
yield SpecifierRequirement(ireq)
else:
self._fail_if_link_is_unsupported_wheel(ireq.link)
Expand Down
24 changes: 13 additions & 11 deletions src/pip/_internal/resolution/resolvelib/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,9 @@ def is_satisfied_by(self, candidate: Candidate) -> bool:


class SpecifierRequirement(Requirement):
def __init__(
self,
ireq: InstallRequirement,
*,
drop_extras: bool = False,
) -> None:
"""
:param drop_extras: Ignore any extras that are part of the install requirement,
making this a requirement on the base only.
"""
def __init__(self, ireq: InstallRequirement) -> None:
assert ireq.link is None, "This is a link, not a specifier"
self._ireq = ireq if not drop_extras else install_req_drop_extras(ireq)
self._ireq = ireq
self._extras = frozenset(self._ireq.extras)

def __str__(self) -> str:
Expand Down Expand Up @@ -102,6 +93,17 @@ def is_satisfied_by(self, candidate: Candidate) -> bool:
return spec.contains(candidate.version, prereleases=True)


class SpecifierWithoutExtrasRequirement(SpecifierRequirement):
"""
Requirement backed by an install requirement on a base package. Trims extras from its install requirement if there are any.
"""

def __init__(self, ireq: InstallRequirement) -> None:
assert ireq.link is None, "This is a link, not a specifier"
self._ireq = install_req_drop_extras(ireq)
self._extras = frozenset(self._ireq.extras)


class RequiresPythonRequirement(Requirement):
"""A requirement representing Requires-Python metadata."""

Expand Down

0 comments on commit 4e73e3e

Please sign in to comment.