Skip to content

Commit

Permalink
Merge pull request #8758 from uranusjr/new-resolver-requires-python-w…
Browse files Browse the repository at this point in the history
…hen-no-deps
  • Loading branch information
pradyunsg authored Sep 2, 2020
2 parents 0321dbc + 5401cc6 commit 567630b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
2 changes: 2 additions & 0 deletions news/8758.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
New resolver: Correctly respect ``Requires-Python`` metadata to reject
incompatible packages in ``--no-deps`` mode.
24 changes: 9 additions & 15 deletions src/pip/_internal/resolution/resolvelib/candidates.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,31 +230,25 @@ def dist(self):
self._prepare()
return self._dist

def _get_requires_python_specifier(self):
# type: () -> Optional[SpecifierSet]
def _get_requires_python_dependency(self):
# type: () -> Optional[Requirement]
requires_python = get_requires_python(self.dist)
if requires_python is None:
return None
try:
spec = SpecifierSet(requires_python)
except InvalidSpecifier as e:
logger.warning(
"Package %r has an invalid Requires-Python: %s", self.name, e,
)
message = "Package %r has an invalid Requires-Python: %s"
logger.warning(message, self.name, e)
return None
return spec
return self._factory.make_requires_python_requirement(spec)

def iter_dependencies(self, with_requires):
# type: (bool) -> Iterable[Optional[Requirement]]
if not with_requires:
return
for r in self.dist.requires():
requires = self.dist.requires() if with_requires else ()
for r in requires:
yield self._factory.make_requirement_from_spec(str(r), self._ireq)
python_dep = self._factory.make_requires_python_requirement(
self._get_requires_python_specifier(),
)
if python_dep:
yield python_dep
yield self._get_requires_python_dependency()

def get_install_requirement(self):
# type: () -> Optional[InstallRequirement]
Expand Down Expand Up @@ -285,7 +279,7 @@ def __init__(
wheel = Wheel(ireq.link.filename)
wheel_name = canonicalize_name(wheel.name)
assert name == wheel_name, (
"{!r} != {!r} for wheel".format(name, wheel_name)
"{!r} != {!r} for wheel".format(name, wheel_name)
)
# Version may not be present for PEP 508 direct URLs
if version is not None:
Expand Down
32 changes: 32 additions & 0 deletions tests/functional/test_new_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,3 +988,35 @@ def test_new_resolver_local_and_req(script):
source_dir, "pkg!=0.1.0",
expect_error=True,
)


def test_new_resolver_no_deps_checks_requires_python(script):
create_basic_wheel_for_package(
script,
"base",
"0.1.0",
depends=["dep"],
requires_python="<2", # Something that always fails.
)
create_basic_wheel_for_package(
script,
"dep",
"0.2.0",
)

result = script.pip(
"install",
"--use-feature=2020-resolver",
"--no-cache-dir",
"--no-index",
"--no-deps",
"--find-links", script.scratch_path,
"base",
expect_error=True,
)

message = (
"Package 'base' requires a different Python: "
"{}.{}.{} not in '<2'".format(*sys.version_info[:3])
)
assert message in result.stderr

0 comments on commit 567630b

Please sign in to comment.