Commit 3ff5a39b by David Hotham Committed by GitHub

Misc cleanups (#6256)

No functional change or fixes here, just tidier code:
* prefer Path.exists()
* typechecking - NormalizedNames from poetry-core
* clean up some unnecessary duplication
* minor refactor
* gather the pyproject.toml parsing into one block, save a level of indentation
* remove more dead code
* simplify handling of extras at command line
* more abstract types 
parent 73e4bde8
...@@ -180,10 +180,7 @@ The add command adds required packages to your <comment>pyproject.toml</> and in ...@@ -180,10 +180,7 @@ The add command adds required packages to your <comment>pyproject.toml</> and in
if self.option("extras"): if self.option("extras"):
extras = [] extras = []
for extra in self.option("extras"): for extra in self.option("extras"):
if " " in extra: extras += extra.split()
extras += [e.strip() for e in extra.split(" ")]
else:
extras.append(extra)
constraint["extras"] = self.option("extras") constraint["extras"] = self.option("extras")
......
...@@ -46,7 +46,7 @@ class CacheClearCommand(Command): ...@@ -46,7 +46,7 @@ class CacheClearCommand(Command):
f"Add the --all option if you want to clear all {parts[0]} caches" f"Add the --all option if you want to clear all {parts[0]} caches"
) )
if not os.path.exists(cache_dir): if not cache_dir.exists():
self.line(f"No cache entries for {parts[0]}") self.line(f"No cache entries for {parts[0]}")
return 0 return 0
......
...@@ -65,10 +65,7 @@ class DebugResolveCommand(InitCommand): ...@@ -65,10 +65,7 @@ class DebugResolveCommand(InitCommand):
assert isinstance(name, str) assert isinstance(name, str)
extras = [] extras = []
for extra in self.option("extras"): for extra in self.option("extras"):
if " " in extra: extras += extra.split()
extras += [e.strip() for e in extra.split(" ")]
else:
extras.append(extra)
constraint["extras"] = extras constraint["extras"] = extras
......
...@@ -120,10 +120,7 @@ dependencies and not including the current project, run the command with the ...@@ -120,10 +120,7 @@ dependencies and not including the current project, run the command with the
else: else:
extras = [] extras = []
for extra in self.option("extras", []): for extra in self.option("extras", []):
if " " in extra: extras += extra.split()
extras += [e.strip() for e in extra.split(" ")]
else:
extras.append(extra)
self.installer.extras(extras) self.installer.extras(extras)
......
from __future__ import annotations from __future__ import annotations
import contextlib
import csv import csv
import itertools import itertools
import json import json
...@@ -542,7 +543,12 @@ class Executor: ...@@ -542,7 +543,12 @@ class Executor:
pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml")) pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml"))
package_poetry = None
if pyproject.is_poetry_project(): if pyproject.is_poetry_project():
with contextlib.suppress(RuntimeError):
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
if package_poetry is not None:
# Even if there is a build system specified # Even if there is a build system specified
# some versions of pip (< 19.0.0) don't understand it # some versions of pip (< 19.0.0) don't understand it
# so we need to check the version of pip to know # so we need to check the version of pip to know
...@@ -552,41 +558,29 @@ class Executor: ...@@ -552,41 +558,29 @@ class Executor:
< self._env.pip_version.__class__.from_parts(19, 0, 0) < self._env.pip_version.__class__.from_parts(19, 0, 0)
) )
try: builder: Builder
package_poetry = Factory().create_poetry(pyproject.file.path.parent) if package.develop and not package_poetry.package.build_script:
except RuntimeError: from poetry.masonry.builders.editable import EditableBuilder
package_poetry = None
if package_poetry is not None:
builder: Builder
if package.develop and not package_poetry.package.build_script:
from poetry.masonry.builders.editable import EditableBuilder
# This is a Poetry package in editable mode
# we can use the EditableBuilder without going through pip
# to install it, unless it has a build script.
builder = EditableBuilder(package_poetry, self._env, NullIO())
builder.build()
return 0
elif legacy_pip or package_poetry.package.build_script:
from poetry.core.masonry.builders.sdist import SdistBuilder
# We need to rely on creating a temporary setup.py
# file since the version of pip does not support
# build-systems
# We also need it for non-PEP-517 packages
builder = SdistBuilder(package_poetry)
with builder.setup_py():
if package.develop:
return self.pip_install(req, upgrade=True, editable=True)
return self.pip_install(req, upgrade=True)
if package.develop: # This is a Poetry package in editable mode
return self.pip_install(req, upgrade=True, editable=True) # we can use the EditableBuilder without going through pip
# to install it, unless it has a build script.
builder = EditableBuilder(package_poetry, self._env, NullIO())
builder.build()
return self.pip_install(req, upgrade=True) return 0
elif legacy_pip or package_poetry.package.build_script:
from poetry.core.masonry.builders.sdist import SdistBuilder
# We need to rely on creating a temporary setup.py
# file since the version of pip does not support
# build-systems
# We also need it for non-PEP-517 packages
builder = SdistBuilder(package_poetry)
with builder.setup_py():
return self.pip_install(req, upgrade=True, editable=package.develop)
return self.pip_install(req, upgrade=True, editable=package.develop)
def _install_git(self, operation: Install | Update) -> int: def _install_git(self, operation: Install | Update) -> int:
from poetry.vcs.git import Git from poetry.vcs.git import Git
......
...@@ -20,7 +20,6 @@ from poetry.utils.helpers import pluralize ...@@ -20,7 +20,6 @@ from poetry.utils.helpers import pluralize
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Iterable from collections.abc import Iterable
from collections.abc import Sequence
from cleo.io.io import IO from cleo.io.io import IO
from poetry.core.packages.project_package import ProjectPackage from poetry.core.packages.project_package import ProjectPackage
...@@ -465,7 +464,7 @@ class Installer: ...@@ -465,7 +464,7 @@ class Installer:
self._installer.remove(operation.package) self._installer.remove(operation.package)
def _populate_lockfile_repo( def _populate_lockfile_repo(
self, repo: LockfileRepository, ops: Sequence[Operation] self, repo: LockfileRepository, ops: Iterable[Operation]
) -> None: ) -> None:
for op in ops: for op in ops:
if isinstance(op, Uninstall): if isinstance(op, Uninstall):
...@@ -509,7 +508,7 @@ class Installer: ...@@ -509,7 +508,7 @@ class Installer:
return ops return ops
def _filter_operations(self, ops: Sequence[Operation], repo: Repository) -> None: def _filter_operations(self, ops: Iterable[Operation], repo: Repository) -> None:
extra_packages = self._get_extra_packages(repo) extra_packages = self._get_extra_packages(repo)
for op in ops: for op in ops:
if isinstance(op, Update): if isinstance(op, Update):
......
from __future__ import annotations from __future__ import annotations
import contextlib
import os import os
import tempfile import tempfile
import urllib.parse import urllib.parse
...@@ -225,56 +226,49 @@ class PipInstaller(BaseInstaller): ...@@ -225,56 +226,49 @@ class PipInstaller(BaseInstaller):
pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml")) pyproject = PyProjectTOML(os.path.join(req, "pyproject.toml"))
package_poetry = None
if pyproject.is_poetry_project(): if pyproject.is_poetry_project():
with contextlib.suppress(RuntimeError):
package_poetry = Factory().create_poetry(pyproject.file.path.parent)
if package_poetry is not None:
# Even if there is a build system specified # Even if there is a build system specified
# some versions of pip (< 19.0.0) don't understand it # some versions of pip (< 19.0.0) don't understand it
# so we need to check the version of pip to know # so we need to check the version of pip to know
# if we can rely on the build system # if we can rely on the build system
legacy_pip = self._env.pip_version < Version.from_parts(19, 0, 0) legacy_pip = self._env.pip_version < Version.from_parts(19, 0, 0)
try: builder: Builder
package_poetry = Factory().create_poetry(pyproject.file.path.parent) if package.develop and not package_poetry.package.build_script:
except RuntimeError: from poetry.masonry.builders.editable import EditableBuilder
package_poetry = None
# This is a Poetry package in editable mode
if package_poetry is not None: # we can use the EditableBuilder without going through pip
builder: Builder # to install it, unless it has a build script.
if package.develop and not package_poetry.package.build_script: builder = EditableBuilder(package_poetry, self._env, NullIO())
from poetry.masonry.builders.editable import EditableBuilder builder.build()
# This is a Poetry package in editable mode return 0
# we can use the EditableBuilder without going through pip elif legacy_pip or package_poetry.package.build_script:
# to install it, unless it has a build script. from poetry.core.masonry.builders.sdist import SdistBuilder
builder = EditableBuilder(package_poetry, self._env, NullIO())
builder.build() # We need to rely on creating a temporary setup.py
# file since the version of pip does not support
return 0 # build-systems
elif legacy_pip or package_poetry.package.build_script: # We also need it for non-PEP-517 packages
from poetry.core.masonry.builders.sdist import SdistBuilder builder = SdistBuilder(package_poetry)
# We need to rely on creating a temporary setup.py with builder.setup_py():
# file since the version of pip does not support return pip_install(
# build-systems path=req,
# We also need it for non-PEP-517 packages environment=self._env,
builder = SdistBuilder(package_poetry) upgrade=True,
editable=package.develop,
with builder.setup_py(): )
if package.develop:
return pip_install( return pip_install(
path=req, path=req, environment=self._env, upgrade=True, editable=package.develop
environment=self._env, )
upgrade=True,
editable=True,
)
return pip_install(
path=req, environment=self._env, deps=False, upgrade=True
)
if package.develop:
return pip_install(
path=req, environment=self._env, upgrade=True, editable=True
)
return pip_install(path=req, environment=self._env, deps=False, upgrade=True)
def install_git(self, package: Package) -> None: def install_git(self, package: Package) -> None:
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
......
...@@ -7,7 +7,7 @@ from poetry.packages.dependency_package import DependencyPackage ...@@ -7,7 +7,7 @@ from poetry.packages.dependency_package import DependencyPackage
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Sequence from collections.abc import Iterable
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
...@@ -17,13 +17,10 @@ class PackageCollection(List[DependencyPackage]): ...@@ -17,13 +17,10 @@ class PackageCollection(List[DependencyPackage]):
def __init__( def __init__(
self, self,
dependency: Dependency, dependency: Dependency,
packages: Sequence[Package | DependencyPackage] | None = None, packages: Iterable[Package | DependencyPackage] = (),
) -> None: ) -> None:
self._dependency = dependency self._dependency = dependency
if packages is None:
packages = []
super().__init__() super().__init__()
for package in packages: for package in packages:
......
...@@ -5,9 +5,7 @@ import logging ...@@ -5,9 +5,7 @@ import logging
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from packaging.utils import canonicalize_name from packaging.utils import canonicalize_name
from poetry.core.semver.helpers import parse_constraint
from poetry.core.semver.version import Version from poetry.core.semver.version import Version
from poetry.core.semver.version_constraint import VersionConstraint
from poetry.core.semver.version_range import VersionRange from poetry.core.semver.version_range import VersionRange
from poetry.repositories.exceptions import PackageNotFound from poetry.repositories.exceptions import PackageNotFound
...@@ -18,6 +16,7 @@ if TYPE_CHECKING: ...@@ -18,6 +16,7 @@ if TYPE_CHECKING:
from poetry.core.packages.dependency import Dependency from poetry.core.packages.dependency import Dependency
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.core.packages.utils.link import Link from poetry.core.packages.utils.link import Link
from poetry.core.semver.version_constraint import VersionConstraint
class Repository: class Repository:
...@@ -103,11 +102,6 @@ class Repository: ...@@ -103,11 +102,6 @@ class Repository:
dependency: Dependency, dependency: Dependency,
) -> tuple[VersionConstraint, bool]: ) -> tuple[VersionConstraint, bool]:
constraint = dependency.constraint constraint = dependency.constraint
if constraint is None:
constraint = "*"
if not isinstance(constraint, VersionConstraint):
constraint = parse_constraint(constraint)
allow_prereleases = dependency.allows_prereleases() allow_prereleases = dependency.allows_prereleases()
if isinstance(constraint, VersionRange) and ( if isinstance(constraint, VersionRange) and (
......
...@@ -4,9 +4,9 @@ from typing import TYPE_CHECKING ...@@ -4,9 +4,9 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Collection
from collections.abc import Iterable from collections.abc import Iterable
from collections.abc import Iterator from collections.abc import Iterator
from collections.abc import Sequence
from typing import Mapping from typing import Mapping
from packaging.utils import NormalizedName from packaging.utils import NormalizedName
...@@ -14,10 +14,10 @@ if TYPE_CHECKING: ...@@ -14,10 +14,10 @@ if TYPE_CHECKING:
def get_extra_package_names( def get_extra_package_names(
packages: Sequence[Package], packages: Iterable[Package],
extras: Mapping[str, list[str]], extras: Mapping[str, list[str]],
extra_names: Sequence[str], extra_names: Collection[str],
) -> Iterable[str]: ) -> Iterable[NormalizedName]:
""" """
Returns all package names required by the given extras. Returns all package names required by the given extras.
......
...@@ -296,5 +296,5 @@ def test_install_directory_fallback_on_poetry_create_error( ...@@ -296,5 +296,5 @@ def test_install_directory_fallback_on_poetry_create_error(
assert mock_sdist_builder.call_count == 0 assert mock_sdist_builder.call_count == 0
assert mock_editable_builder.call_count == 0 assert mock_editable_builder.call_count == 0
assert mock_pip_install.call_count == 1 assert mock_pip_install.call_count == 1
assert mock_pip_install.call_args[1].get("deps") is False assert mock_pip_install.call_args[1].get("deps") is None
assert mock_pip_install.call_args[1].get("upgrade") is True assert mock_pip_install.call_args[1].get("upgrade") is True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment