Commit 03e9c2b0 by David Hotham Committed by GitHub

more typechecking (#5635)

parent 3349ba97
...@@ -154,6 +154,9 @@ class PackageInfo: ...@@ -154,6 +154,9 @@ class PackageInfo:
""" """
name = name or self.name name = name or self.name
if not name:
raise RuntimeError("Unable to create package with no name")
if not self.version: if not self.version:
# The version could not be determined, so we raise an error since it is # The version could not be determined, so we raise an error since it is
# mandatory. # mandatory.
...@@ -166,7 +169,8 @@ class PackageInfo: ...@@ -166,7 +169,8 @@ class PackageInfo:
source_url=self._source_url, source_url=self._source_url,
source_reference=self._source_reference, source_reference=self._source_reference,
) )
package.description = self.summary if self.summary is not None:
package.description = self.summary
package.root_dir = root_dir package.root_dir = root_dir
package.python_versions = self.requires_python or "*" package.python_versions = self.requires_python or "*"
package.files = self.files package.files = self.files
......
...@@ -41,7 +41,7 @@ class Chef: ...@@ -41,7 +41,7 @@ class Chef:
def is_wheel(self, archive: Path) -> bool: def is_wheel(self, archive: Path) -> bool:
return archive.suffix == ".whl" return archive.suffix == ".whl"
def get_cached_archive_for_link(self, link: Link) -> Link | None: def get_cached_archive_for_link(self, link: Link) -> Link:
# If the archive is already a wheel, there is no need to cache it. # If the archive is already a wheel, there is no need to cache it.
if link.is_wheel: if link.is_wheel:
return link return link
......
...@@ -109,6 +109,7 @@ class Chooser: ...@@ -109,6 +109,7 @@ class Chooser:
def _get_links(self, package: Package) -> list[Link]: def _get_links(self, package: Package) -> list[Link]:
if package.source_type: if package.source_type:
assert package.source_reference is not None
repository = self._pool.repository(package.source_reference) repository = self._pool.repository(package.source_reference)
elif not self._pool.has_repository("pypi"): elif not self._pool.has_repository("pypi"):
...@@ -127,6 +128,7 @@ class Chooser: ...@@ -127,6 +128,7 @@ class Chooser:
selected_links.append(link) selected_links.append(link)
continue continue
assert link.hash_name is not None
h = link.hash_name + ":" + link.hash h = link.hash_name + ":" + link.hash
if h not in hashes: if h not in hashes:
logger.debug( logger.debug(
...@@ -212,6 +214,7 @@ class Chooser: ...@@ -212,6 +214,7 @@ class Chooser:
if not link.hash: if not link.hash:
return True return True
assert link.hash_name is not None
h = link.hash_name + ":" + link.hash h = link.hash_name + ":" + link.hash
return h in {f["hash"] for f in package.files} return h in {f["hash"] for f in package.files}
...@@ -36,6 +36,7 @@ from poetry.utils.pip import pip_install ...@@ -36,6 +36,7 @@ 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 cleo.io.outputs.section_output import SectionOutput
from poetry.core.masonry.builders.builder import Builder
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
...@@ -462,9 +463,11 @@ class Executor: ...@@ -462,9 +463,11 @@ class Executor:
if package.source_type == "git": if package.source_type == "git":
return self._install_git(operation) return self._install_git(operation)
archive: Link | Path
if package.source_type == "file": if package.source_type == "file":
archive = self._prepare_file(operation) archive = self._prepare_file(operation)
elif package.source_type == "url": elif package.source_type == "url":
assert package.source_url is not None
archive = self._download_link(operation, Link(package.source_url)) archive = self._download_link(operation, Link(package.source_url))
else: else:
archive = self._download(operation) archive = self._download(operation)
...@@ -507,6 +510,7 @@ class Executor: ...@@ -507,6 +510,7 @@ class Executor:
) )
self._write(operation, message) self._write(operation, message)
assert package.source_url is not None
archive = Path(package.source_url) archive = Path(package.source_url)
if not Path(package.source_url).is_absolute() and package.root_dir: if not Path(package.source_url).is_absolute() and package.root_dir:
archive = package.root_dir / archive archive = package.root_dir / archive
...@@ -527,6 +531,7 @@ class Executor: ...@@ -527,6 +531,7 @@ class Executor:
) )
self._write(operation, message) self._write(operation, message)
assert package.source_url is not None
if package.root_dir: if package.root_dir:
req = package.root_dir / package.source_url req = package.root_dir / package.source_url
else: else:
...@@ -545,6 +550,7 @@ class Executor: ...@@ -545,6 +550,7 @@ class Executor:
) )
package_poetry = Factory().create_poetry(pyproject.file.path.parent) package_poetry = Factory().create_poetry(pyproject.file.path.parent)
builder: Builder
if package.develop and not package_poetry.package.build_script: if package.develop and not package_poetry.package.build_script:
from poetry.masonry.builders.editable import EditableBuilder from poetry.masonry.builders.editable import EditableBuilder
...@@ -585,6 +591,7 @@ class Executor: ...@@ -585,6 +591,7 @@ class Executor:
) )
self._write(operation, message) self._write(operation, message)
assert package.source_url is not None
source = Git.clone( source = Git.clone(
url=package.source_url, url=package.source_url,
source_root=self._env.path / "src", source_root=self._env.path / "src",
...@@ -601,14 +608,15 @@ class Executor: ...@@ -601,14 +608,15 @@ class Executor:
return status_code return status_code
def _download(self, operation: Install | Update) -> Link: def _download(self, operation: Install | Update) -> Link | Path:
link = self._chooser.choose_for(operation.package) link = self._chooser.choose_for(operation.package)
return self._download_link(operation, link) return self._download_link(operation, link)
def _download_link(self, operation: Install | Update, link: Link) -> Link: def _download_link(self, operation: Install | Update, link: Link) -> Link | Path:
package = operation.package package = operation.package
archive: Link | Path
archive = self._chef.get_cached_archive_for_link(link) archive = self._chef.get_cached_archive_for_link(link)
if archive is link: if archive is link:
# No cached distributions was found, so we download and prepare it # No cached distributions was found, so we download and prepare it
...@@ -758,9 +766,7 @@ class Executor: ...@@ -758,9 +766,7 @@ class Executor:
path = url.relative_to(record.parent.parent) path = url.relative_to(record.parent.parent)
writer.writerow([str(path), "", ""]) writer.writerow([str(path), "", ""])
def _create_git_url_reference( def _create_git_url_reference(self, package: Package) -> dict[str, Any]:
self, package: Package
) -> dict[str, str | dict[str, str]]:
reference = { reference = {
"url": package.source_url, "url": package.source_url,
"vcs_info": { "vcs_info": {
...@@ -772,9 +778,7 @@ class Executor: ...@@ -772,9 +778,7 @@ class Executor:
return reference return reference
def _create_url_url_reference( def _create_url_url_reference(self, package: Package) -> dict[str, Any]:
self, package: Package
) -> dict[str, str | dict[str, str]]:
archive_info = {} archive_info = {}
if package.name in self._hashes: if package.name in self._hashes:
...@@ -784,27 +788,25 @@ class Executor: ...@@ -784,27 +788,25 @@ class Executor:
return reference return reference
def _create_file_url_reference( def _create_file_url_reference(self, package: Package) -> dict[str, Any]:
self, package: Package
) -> dict[str, str | dict[str, str]]:
archive_info = {} archive_info = {}
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]
assert package.source_url is not None
return { return {
"url": Path(package.source_url).as_uri(), "url": Path(package.source_url).as_uri(),
"archive_info": archive_info, "archive_info": archive_info,
} }
def _create_directory_url_reference( def _create_directory_url_reference(self, package: Package) -> dict[str, Any]:
self, package: Package
) -> dict[str, str | dict[str, bool]]:
dir_info = {} dir_info = {}
if package.develop: if package.develop:
dir_info["editable"] = True dir_info["editable"] = True
assert package.source_url is not None
return { return {
"url": Path(package.source_url).as_uri(), "url": Path(package.source_url).as_uri(),
"dir_info": dir_info, "dir_info": dir_info,
......
...@@ -522,8 +522,8 @@ class Installer: ...@@ -522,8 +522,8 @@ class Installer:
if self._update: if self._update:
extras = {} extras = {}
for extra, deps in self._package.extras.items(): for extra, dependencies in self._package.extras.items():
extras[extra] = [dep.name for dep in deps] extras[extra] = [dependency.name for dependency in dependencies]
else: else:
extras = {} extras = {}
for extra, deps in self._locker.lock_data.get("extras", {}).items(): for extra, deps in self._locker.lock_data.get("extras", {}).items():
......
...@@ -10,6 +10,7 @@ from typing import TYPE_CHECKING ...@@ -10,6 +10,7 @@ from typing import TYPE_CHECKING
from typing import Any from typing import Any
from poetry.core.pyproject.toml import PyProjectTOML from poetry.core.pyproject.toml import PyProjectTOML
from poetry.core.semver.version import Version
from poetry.installation.base_installer import BaseInstaller from poetry.installation.base_installer import BaseInstaller
from poetry.repositories.http import HTTPRepository from poetry.repositories.http import HTTPRepository
...@@ -20,6 +21,7 @@ from poetry.utils.pip import pip_install ...@@ -20,6 +21,7 @@ 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 poetry.core.masonry.builders.builder import Builder
from poetry.core.packages.package import Package from poetry.core.packages.package import Package
from poetry.repositories.pool import Pool from poetry.repositories.pool import Pool
...@@ -49,9 +51,11 @@ class PipInstaller(BaseInstaller): ...@@ -49,9 +51,11 @@ class PipInstaller(BaseInstaller):
package.source_type not in {"git", "directory", "file", "url"} package.source_type not in {"git", "directory", "file", "url"}
and package.source_url and package.source_url
): ):
assert package.source_reference is not None
repository = self._pool.repository(package.source_reference) repository = self._pool.repository(package.source_reference)
parsed = urllib.parse.urlparse(package.source_url) parsed = urllib.parse.urlparse(package.source_url)
if parsed.scheme == "http": if parsed.scheme == "http":
assert parsed.hostname is not None
self._io.write_error( self._io.write_error(
" <warning>Installing from unsecure host:" " <warning>Installing from unsecure host:"
f" {parsed.hostname}</warning>" f" {parsed.hostname}</warning>"
...@@ -156,6 +160,7 @@ class PipInstaller(BaseInstaller): ...@@ -156,6 +160,7 @@ class PipInstaller(BaseInstaller):
return req return req
if package.source_type in ["file", "directory"]: if package.source_type in ["file", "directory"]:
assert package.source_url is not None
if package.root_dir: if package.root_dir:
req = (package.root_dir / package.source_url).as_posix() req = (package.root_dir / package.source_url).as_posix()
else: else:
...@@ -200,10 +205,9 @@ class PipInstaller(BaseInstaller): ...@@ -200,10 +205,9 @@ class PipInstaller(BaseInstaller):
from poetry.factory import Factory from poetry.factory import Factory
req: Path assert package.source_url is not None
if package.root_dir: if package.root_dir:
req = (package.root_dir / package.source_url).as_posix() req = package.root_dir / package.source_url
else: else:
req = Path(package.source_url).resolve(strict=False) req = Path(package.source_url).resolve(strict=False)
...@@ -214,11 +218,10 @@ class PipInstaller(BaseInstaller): ...@@ -214,11 +218,10 @@ class PipInstaller(BaseInstaller):
# 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 < self._env.pip_version.__class__( legacy_pip = self._env.pip_version < Version.from_parts(19, 0, 0)
19, 0, 0
)
package_poetry = Factory().create_poetry(pyproject.file.path.parent) package_poetry = Factory().create_poetry(pyproject.file.path.parent)
builder: Builder
if package.develop and not package_poetry.package.build_script: if package.develop and not package_poetry.package.build_script:
from poetry.masonry.builders.editable import EditableBuilder from poetry.masonry.builders.editable import EditableBuilder
...@@ -261,6 +264,7 @@ class PipInstaller(BaseInstaller): ...@@ -261,6 +264,7 @@ class PipInstaller(BaseInstaller):
from poetry.vcs.git import Git from poetry.vcs.git import Git
assert package.source_url is not None
source = Git.clone( source = Git.clone(
url=package.source_url, url=package.source_url,
source_root=self._env.path / "src", source_root=self._env.path / "src",
......
...@@ -47,7 +47,7 @@ class EditableBuilder(Builder): # type: ignore[misc] ...@@ -47,7 +47,7 @@ class EditableBuilder(Builder): # type: ignore[misc]
self._env = env self._env = env
self._io = io self._io = io
def build(self) -> None: def build(self, target_dir: Path | None = None) -> Path:
self._debug( self._debug(
f" - Building package <c1>{self._package.name}</c1> in" f" - Building package <c1>{self._package.name}</c1> in"
" <info>editable</info> mode" " <info>editable</info> mode"
...@@ -58,7 +58,9 @@ class EditableBuilder(Builder): # type: ignore[misc] ...@@ -58,7 +58,9 @@ class EditableBuilder(Builder): # type: ignore[misc]
self._debug( self._debug(
" - <warning>Falling back on using a <b>setup.py</b></warning>" " - <warning>Falling back on using a <b>setup.py</b></warning>"
) )
return self._setup_build() self._setup_build()
path: Path = self._path
return path
self._run_build_script(self._package.build_script) self._run_build_script(self._package.build_script)
...@@ -75,6 +77,9 @@ class EditableBuilder(Builder): # type: ignore[misc] ...@@ -75,6 +77,9 @@ class EditableBuilder(Builder): # type: ignore[misc]
added_files += self._add_scripts() added_files += self._add_scripts()
self._add_dist_info(added_files) self._add_dist_info(added_files)
path = self._path
return path
def _run_build_script(self, build_script: str) -> None: def _run_build_script(self, build_script: str) -> None:
with build_environment(poetry=self._poetry, env=self._env, io=self._io) as env: with build_environment(poetry=self._poetry, env=self._env, io=self._io) as env:
self._debug(f" - Executing build script: <b>{build_script}</b>") self._debug(f" - Executing build script: <b>{build_script}</b>")
......
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