Commit 2ae46fa5 by Branch Vincent Committed by Bjorn Neergaard

chore(installation): remove type errors

parent e38c045c
...@@ -116,11 +116,6 @@ enable_error_code = ["ignore-without-code"] ...@@ -116,11 +116,6 @@ enable_error_code = ["ignore-without-code"]
[[tool.mypy.overrides]] [[tool.mypy.overrides]]
module = [ module = [
'poetry.installation.chef',
'poetry.installation.chooser',
'poetry.installation.executor',
'poetry.installation.installer',
'poetry.installation.pip_installer',
'poetry.utils.env', 'poetry.utils.env',
] ]
ignore_errors = true ignore_errors = true
...@@ -133,6 +128,7 @@ ignore_errors = true ...@@ -133,6 +128,7 @@ ignore_errors = true
# warning. # warning.
[[tool.mypy.overrides]] [[tool.mypy.overrides]]
module = [ module = [
'poetry.installation.executor',
'poetry.repositories.installed_repository', 'poetry.repositories.installed_repository',
] ]
warn_unused_ignores = false warn_unused_ignores = false
......
...@@ -51,7 +51,7 @@ class Chef: ...@@ -51,7 +51,7 @@ class Chef:
if not archives: if not archives:
return link return link
candidates = [] candidates: list[tuple[float | None, Link]] = []
for archive in archives: for archive in archives:
if not archive.is_wheel: if not archive.is_wheel:
candidates.append((float("inf"), archive)) candidates.append((float("inf"), archive))
......
...@@ -4,6 +4,7 @@ import logging ...@@ -4,6 +4,7 @@ import logging
import re import re
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Any
from packaging.tags import Tag from packaging.tags import Tag
...@@ -15,6 +16,7 @@ from poetry.utils.patterns import wheel_file_re ...@@ -15,6 +16,7 @@ from poetry.utils.patterns import wheel_file_re
if TYPE_CHECKING: if TYPE_CHECKING:
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 import Version
from poetry.repositories.pool import Pool from poetry.repositories.pool import Pool
from poetry.utils.env import Env from poetry.utils.env import Env
...@@ -144,7 +146,9 @@ class Chooser: ...@@ -144,7 +146,9 @@ class Chooser:
return selected_links return selected_links
def _sort_key(self, package: Package, link: Link) -> tuple: def _sort_key(
self, package: Package, link: Link
) -> tuple[int, int, int, Version, tuple[Any, ...], int]:
""" """
Function to pass as the `key` argument to a call to sorted() to sort Function to pass as the `key` argument to a call to sorted() to sort
InstallationCandidates by preference. InstallationCandidates by preference.
...@@ -168,7 +172,7 @@ class Chooser: ...@@ -168,7 +172,7 @@ class Chooser:
comparison operators, but then different sdist links comparison operators, but then different sdist links
with the same version, would have to be considered equal with the same version, would have to be considered equal
""" """
build_tag = () build_tag: tuple[Any, ...] = ()
binary_preference = 0 binary_preference = 0
if link.is_wheel: if link.is_wheel:
wheel = Wheel(link.filename) wheel = Wheel(link.filename)
...@@ -179,9 +183,11 @@ class Chooser: ...@@ -179,9 +183,11 @@ class Chooser:
) )
# TODO: Binary preference # TODO: Binary preference
pri = -(wheel.get_minimum_supported_index(self._env.supported_tags)) pri = -(wheel.get_minimum_supported_index(self._env.supported_tags) or 0)
if wheel.build_tag is not None: if wheel.build_tag is not None:
match = re.match(r"^(\d+)(.*)$", wheel.build_tag) match = re.match(r"^(\d+)(.*)$", wheel.build_tag)
if not match:
raise ValueError(f"Unable to parse build tag: {wheel.build_tag}")
build_tag_groups = match.groups() build_tag_groups = match.groups()
build_tag = (int(build_tag_groups[0]), build_tag_groups[1]) build_tag = (int(build_tag_groups[0]), build_tag_groups[1])
else: # sdist else: # sdist
......
...@@ -12,6 +12,7 @@ from pathlib import Path ...@@ -12,6 +12,7 @@ from pathlib import Path
from subprocess import CalledProcessError from subprocess import CalledProcessError
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import Any from typing import Any
from typing import cast
from cleo.io.null_io import NullIO from cleo.io.null_io import NullIO
from poetry.core.packages.file_dependency import FileDependency from poetry.core.packages.file_dependency import FileDependency
...@@ -21,6 +22,9 @@ from poetry.core.pyproject.toml import PyProjectTOML ...@@ -21,6 +22,9 @@ from poetry.core.pyproject.toml import PyProjectTOML
from poetry.installation.chef import Chef from poetry.installation.chef import Chef
from poetry.installation.chooser import Chooser from poetry.installation.chooser import Chooser
from poetry.installation.operations import Install
from poetry.installation.operations import Uninstall
from poetry.installation.operations import Update
from poetry.utils._compat import decode from poetry.utils._compat import decode
from poetry.utils.authenticator import Authenticator from poetry.utils.authenticator import Authenticator
from poetry.utils.env import EnvCommandError from poetry.utils.env import EnvCommandError
...@@ -31,12 +35,10 @@ from poetry.utils.pip import pip_install ...@@ -31,12 +35,10 @@ from poetry.utils.pip import pip_install
if TYPE_CHECKING: if TYPE_CHECKING:
from cleo.io.io import IO from cleo.io.io import IO
from cleo.io.outputs.section_output import SectionOutput
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.config.config import Config from poetry.config.config import Config
from poetry.installation.operations import Install
from poetry.installation.operations import Uninstall
from poetry.installation.operations import Update
from poetry.installation.operations.operation import Operation from poetry.installation.operations.operation import Operation
from poetry.repositories import Pool from poetry.repositories import Pool
from poetry.utils.env import Env from poetry.utils.env import Env
...@@ -49,7 +51,7 @@ class Executor: ...@@ -49,7 +51,7 @@ class Executor:
pool: Pool, pool: Pool,
config: Config, config: Config,
io: IO, io: IO,
parallel: bool = None, parallel: bool | None = None,
) -> None: ) -> None:
self._env = env self._env = env
self._io = io self._io = io
...@@ -75,7 +77,7 @@ class Executor: ...@@ -75,7 +77,7 @@ class Executor:
self._executed_operations = 0 self._executed_operations = 0
self._executed = {"install": 0, "update": 0, "uninstall": 0} self._executed = {"install": 0, "update": 0, "uninstall": 0}
self._skipped = {"install": 0, "update": 0, "uninstall": 0} self._skipped = {"install": 0, "update": 0, "uninstall": 0}
self._sections = {} self._sections: dict[int, SectionOutput] = {}
self._lock = threading.Lock() self._lock = threading.Lock()
self._shutdown = False self._shutdown = False
self._hashes: dict[str, str] = {} self._hashes: dict[str, str] = {}
...@@ -186,7 +188,7 @@ class Executor: ...@@ -186,7 +188,7 @@ class Executor:
# (it raises a NotImplementedError), so, in this case, we assume # (it raises a NotImplementedError), so, in this case, we assume
# that the system only has one CPU. # that the system only has one CPU.
try: try:
default_max_workers = os.cpu_count() + 4 default_max_workers = (os.cpu_count() or 1) + 4
except NotImplementedError: except NotImplementedError:
default_max_workers = 5 default_max_workers = 5
...@@ -312,7 +314,7 @@ class Executor: ...@@ -312,7 +314,7 @@ class Executor:
return 0 return 0
result = getattr(self, f"_execute_{method}")(operation) result: int = getattr(self, f"_execute_{method}")(operation)
if result != 0: if result != 0:
return result return result
...@@ -373,21 +375,21 @@ class Executor: ...@@ -373,21 +375,21 @@ class Executor:
source_operation_color += "_dark" source_operation_color += "_dark"
package_color += "_dark" package_color += "_dark"
if operation.job_type == "install": if isinstance(operation, Install):
return ( return (
f"<{base_tag}>Installing" f"<{base_tag}>Installing"
f" <{package_color}>{operation.package.name}</{package_color}>" f" <{package_color}>{operation.package.name}</{package_color}>"
f" (<{operation_color}>{operation.package.full_pretty_version}</>)</>" f" (<{operation_color}>{operation.package.full_pretty_version}</>)</>"
) )
if operation.job_type == "uninstall": if isinstance(operation, Uninstall):
return ( return (
f"<{base_tag}>Removing" f"<{base_tag}>Removing"
f" <{package_color}>{operation.package.name}</{package_color}>" f" <{package_color}>{operation.package.name}</{package_color}>"
f" (<{operation_color}>{operation.package.full_pretty_version}</>)</>" f" (<{operation_color}>{operation.package.full_pretty_version}</>)</>"
) )
if operation.job_type == "update": if isinstance(operation, Update):
return ( return (
f"<{base_tag}>Updating" f"<{base_tag}>Updating"
f" <{package_color}>{operation.initial_package.name}</{package_color}> " f" <{package_color}>{operation.initial_package.name}</{package_color}> "
...@@ -643,7 +645,7 @@ class Executor: ...@@ -643,7 +645,7 @@ class Executor:
package.name, package.name,
archive_path, archive_path,
) )
archive_hash = "sha256:" + file_dep.hash() archive_hash: str = "sha256:" + file_dep.hash()
known_hashes = {f["hash"] for f in package.files} known_hashes = {f["hash"] for f in package.files}
if archive_hash not in known_hashes: if archive_hash not in known_hashes:
...@@ -681,7 +683,7 @@ class Executor: ...@@ -681,7 +683,7 @@ class Executor:
progress.start() progress.start()
done = 0 done = 0
archive = self._chef.get_cache_directory_for_link(link) / link.filename archive: Path = self._chef.get_cache_directory_for_link(link) / link.filename
archive.parent.mkdir(parents=True, exist_ok=True) archive.parent.mkdir(parents=True, exist_ok=True)
with archive.open("wb") as f: with archive.open("wb") as f:
for chunk in response.iter_content(chunk_size=4096): for chunk in response.iter_content(chunk_size=4096):
...@@ -730,7 +732,7 @@ class Executor: ...@@ -730,7 +732,7 @@ class Executor:
direct_url_json.unlink() direct_url_json.unlink()
return return
url_reference = None url_reference: dict[str, Any] | None = None
if package.source_type == "git": if package.source_type == "git":
url_reference = self._create_git_url_reference(package) url_reference = self._create_git_url_reference(package)
...@@ -745,26 +747,16 @@ class Executor: ...@@ -745,26 +747,16 @@ class Executor:
for dist in self._env.site_packages.distributions( for dist in self._env.site_packages.distributions(
name=package.name, writable_only=True name=package.name, writable_only=True
): ):
dist._path.joinpath("direct_url.json").write_text( dist_path = cast(Path, dist._path) # type: ignore[attr-defined]
json.dumps(url_reference), url = dist_path / "direct_url.json"
encoding="utf-8", url.write_text(json.dumps(url_reference), encoding="utf-8")
)
record = dist._path.joinpath("RECORD") record = dist_path / "RECORD"
if record.exists(): if record.exists():
with record.open(mode="a", encoding="utf-8") as f: with record.open(mode="a", encoding="utf-8") as f:
writer = csv.writer(f) writer = csv.writer(f)
writer.writerow( path = url.relative_to(record.parent.parent)
[ writer.writerow([str(path), "", ""])
str(
dist._path.joinpath("direct_url.json").relative_to(
record.parent.parent
)
),
"",
"",
]
)
def _create_git_url_reference( def _create_git_url_reference(
self, package: Package self, package: Package
...@@ -800,24 +792,20 @@ class Executor: ...@@ -800,24 +792,20 @@ class Executor:
if package.name in self._hashes: if package.name in self._hashes:
archive_info["hash"] = self._hashes[package.name] archive_info["hash"] = self._hashes[package.name]
reference = { return {
"url": Path(package.source_url).as_uri(), "url": Path(package.source_url).as_uri(),
"archive_info": archive_info, "archive_info": archive_info,
} }
return reference
def _create_directory_url_reference( def _create_directory_url_reference(
self, package: Package self, package: Package
) -> dict[str, str | dict[str, str]]: ) -> dict[str, str | dict[str, bool]]:
dir_info = {} dir_info = {}
if package.develop: if package.develop:
dir_info["editable"] = True dir_info["editable"] = True
reference = { return {
"url": Path(package.source_url).as_uri(), "url": Path(package.source_url).as_uri(),
"dir_info": dir_info, "dir_info": dir_info,
} }
return reference
...@@ -58,9 +58,9 @@ class Installer: ...@@ -58,9 +58,9 @@ class Installer:
self._execute_operations = True self._execute_operations = True
self._lock = False self._lock = False
self._whitelist = [] self._whitelist: list[str] = []
self._extras = [] self._extras: list[str] = []
if executor is None: if executor is None:
executor = Executor(self._env, self._pool, config, self._io) executor = Executor(self._env, self._pool, config, self._io)
...@@ -171,7 +171,7 @@ class Installer: ...@@ -171,7 +171,7 @@ class Installer:
return self return self
def extras(self, extras: list) -> Installer: def extras(self, extras: list[str]) -> Installer:
self._extras = extras self._extras = extras
return self return self
...@@ -182,7 +182,7 @@ class Installer: ...@@ -182,7 +182,7 @@ class Installer:
return self return self
def _do_refresh(self) -> int: def _do_refresh(self) -> int:
from poetry.puzzle import Solver from poetry.puzzle.solver import Solver
# Checking extras # Checking extras
for extra in self._extras: for extra in self._extras:
...@@ -211,7 +211,7 @@ class Installer: ...@@ -211,7 +211,7 @@ class Installer:
return 0 return 0
def _do_install(self, local_repo: Repository) -> int: def _do_install(self, local_repo: Repository) -> int:
from poetry.puzzle import Solver from poetry.puzzle.solver import Solver
locked_repository = Repository() locked_repository = Repository()
if self._update: if self._update:
...@@ -475,9 +475,9 @@ class Installer: ...@@ -475,9 +475,9 @@ class Installer:
def _get_operations_from_lock( def _get_operations_from_lock(
self, locked_repository: Repository self, locked_repository: Repository
) -> Sequence[Operation]: ) -> list[Operation]:
installed_repo = self._installed_repository installed_repo = self._installed_repository
ops = [] ops: list[Operation] = []
extra_packages = self._get_extra_packages(locked_repository) extra_packages = self._get_extra_packages(locked_repository)
for locked in locked_repository.packages: for locked in locked_repository.packages:
......
...@@ -84,6 +84,7 @@ class PipInstaller(BaseInstaller): ...@@ -84,6 +84,7 @@ class PipInstaller(BaseInstaller):
if update: if update:
args.append("-U") args.append("-U")
req: str | list[str]
if package.files and not package.source_url: if package.files and not package.source_url:
# Format as a requirements.txt # Format as a requirements.txt
# We need to create a requirements.txt file # We need to create a requirements.txt file
...@@ -136,10 +137,10 @@ class PipInstaller(BaseInstaller): ...@@ -136,10 +137,10 @@ class PipInstaller(BaseInstaller):
if src_dir.exists(): if src_dir.exists():
remove_directory(src_dir, force=True) remove_directory(src_dir, force=True)
def run(self, *args: Any, **kwargs: Any) -> str: def run(self, *args: Any, **kwargs: Any) -> int | str:
return self._env.run_pip(*args, **kwargs) return self._env.run_pip(*args, **kwargs)
def requirement(self, package: Package, formatted: bool = False) -> str: def requirement(self, package: Package, formatted: bool = False) -> str | list[str]:
if formatted and not package.source_type: if formatted and not package.source_type:
req = f"{package.name}=={package.version}" req = f"{package.name}=={package.version}"
for f in package.files: for f in package.files:
...@@ -161,7 +162,7 @@ class PipInstaller(BaseInstaller): ...@@ -161,7 +162,7 @@ class PipInstaller(BaseInstaller):
req = os.path.realpath(package.source_url) req = os.path.realpath(package.source_url)
if package.develop and package.source_type == "directory": if package.develop and package.source_type == "directory":
req = ["-e", req] return ["-e", req]
return req return req
...@@ -172,7 +173,7 @@ class PipInstaller(BaseInstaller): ...@@ -172,7 +173,7 @@ class PipInstaller(BaseInstaller):
) )
if package.develop: if package.develop:
req = ["-e", req] return ["-e", req]
return req return req
...@@ -183,9 +184,12 @@ class PipInstaller(BaseInstaller): ...@@ -183,9 +184,12 @@ class PipInstaller(BaseInstaller):
def create_temporary_requirement(self, package: Package) -> str: def create_temporary_requirement(self, package: Package) -> str:
fd, name = tempfile.mkstemp("reqs.txt", f"{package.name}-{package.version}") fd, name = tempfile.mkstemp("reqs.txt", f"{package.name}-{package.version}")
req = self.requirement(package, formatted=True)
if isinstance(req, list):
req = " ".join(req)
try: try:
os.write(fd, encode(self.requirement(package, formatted=True))) os.write(fd, encode(req))
finally: finally:
os.close(fd) os.close(fd)
...@@ -237,7 +241,7 @@ class PipInstaller(BaseInstaller): ...@@ -237,7 +241,7 @@ class PipInstaller(BaseInstaller):
with builder.setup_py(): with builder.setup_py():
if package.develop: if package.develop:
return pip_install( return pip_install(
directory=req, path=req,
environment=self._env, environment=self._env,
upgrade=True, upgrade=True,
editable=True, editable=True,
...@@ -248,7 +252,7 @@ class PipInstaller(BaseInstaller): ...@@ -248,7 +252,7 @@ class PipInstaller(BaseInstaller):
if package.develop: if package.develop:
return pip_install( return pip_install(
directory=req, environment=self._env, upgrade=True, editable=True path=req, environment=self._env, upgrade=True, editable=True
) )
return pip_install(path=req, environment=self._env, deps=False, upgrade=True) return pip_install(path=req, environment=self._env, deps=False, upgrade=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