Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typechecking poetry.core.masonry #274

Merged
merged 5 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ ignore_missing_imports = true
[[tool.mypy.overrides]]
module = [
# src modules
'poetry.core.masonry.builders.builder',
'poetry.core.masonry.builders.sdist',
'poetry.core.masonry.builders.wheel',
'poetry.core.packages.utils.utils',
'poetry.core.packages.dependency',
'poetry.core.packages.package',
Expand Down
27 changes: 13 additions & 14 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def __init__(
self,
poetry: Poetry,
ignore_packages_formats: bool = False,
executable: Path | str | None = None,
executable: Path | None = None,
) -> None:
from poetry.core.masonry.metadata import Metadata
from poetry.core.masonry.utils.module import Module

self._poetry = poetry
self._package = poetry.package
self._path = poetry.file.parent
self._path: Path = poetry.file.parent
self._excluded_files: set[str] | None = None
self._executable = Path(executable or sys.executable)

Expand Down Expand Up @@ -93,7 +93,11 @@ def __init__(
def executable(self) -> Path:
return self._executable

def build(self) -> None:
@property
def default_target_dir(self) -> Path:
return self._path / "dist"

def build(self, target_dir: Path | None) -> Path:
raise NotImplementedError()

def find_excluded_files(self, fmt: str | None = None) -> set[str]:
Expand Down Expand Up @@ -203,10 +207,6 @@ def find_files_to_add(self, exclude_build: bool = True) -> set[BuildIncludeFile]
if file.suffix == ".pyc":
continue

if file in to_add:
# Skip duplicates
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_add is a set, it couldn't contain duplicates if it tried. Just need to be happy with __hash__ and __eq__ on the BuildIncludeFile.

continue

logger.debug(f"Adding: {str(file)}")
to_add.add(include_file)

Expand Down Expand Up @@ -349,6 +349,8 @@ def convert_script_files(self) -> list[Path]:
@classmethod
def convert_author(cls, author: str) -> dict[str, str]:
m = AUTHOR_REGEX.match(author)
if m is None:
raise RuntimeError(f"{author} does not match regex")

name = m.group("name")
email = m.group("email")
Expand Down Expand Up @@ -378,14 +380,11 @@ def __init__(

self.path = self.path.resolve()

def __eq__(self, other: BuildIncludeFile | Path) -> bool:
if hasattr(other, "path"):
return self.path == other.path

return self.path == other
def __eq__(self, other: object) -> bool:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we disallowing comparison with Path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not disallowing, but always returning False.

Because a BuildIncludeFile is not a Path!

if not isinstance(other, BuildIncludeFile):
abn marked this conversation as resolved.
Show resolved Hide resolved
return False

def __ne__(self, other: BuildIncludeFile | Path) -> bool:
return not self.__eq__(other)
return self.path == other.path

def __hash__(self) -> int:
return hash(self.path)
Expand Down
22 changes: 13 additions & 9 deletions src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class SdistBuilder(Builder):

format = "sdist"

def build(self, target_dir: Path | None = None) -> Path:
def build(
self,
target_dir: Path | None = None,
) -> Path:
logger.info("Building <info>sdist</info>")
if target_dir is None:
target_dir = self._path / "dist"
target_dir = target_dir or self.default_target_dir

if not target_dir.exists():
target_dir.mkdir(parents=True)
Expand Down Expand Up @@ -113,7 +115,7 @@ def build_setup(self) -> bytes:
from poetry.core.masonry.utils.package_include import PackageInclude

before, extra, after = [], [], []
package_dir = {}
package_dir: dict[str, str] = {}

# If we have a build script, use it
if self._package.build_script:
Expand Down Expand Up @@ -233,7 +235,9 @@ def setup_py(self) -> Iterator[Path]:
def build_pkg_info(self) -> bytes:
return self.get_metadata_content().encode()

def find_packages(self, include: PackageInclude) -> tuple[str, list[str], dict]:
def find_packages(
self, include: PackageInclude
) -> tuple[str | None, list[str], dict[str, list[str]]]:
"""
Discover subpackages and data.

Expand All @@ -246,7 +250,7 @@ def find_packages(self, include: PackageInclude) -> tuple[str, list[str], dict]:
base = str(include.elements[0].parent)

pkg_name = include.package
pkg_data = defaultdict(list)
pkg_data: dict[str, list[str]] = defaultdict(list)
# Undocumented distutils feature:
# the empty string matches all package names
pkg_data[""].append("*")
Expand Down Expand Up @@ -322,15 +326,15 @@ def find_files_to_add(self, exclude_build: bool = False) -> set[BuildIncludeFile
additional_files.update(self.convert_script_files())

# Include project files
additional_files.add("pyproject.toml")
additional_files.add(Path("pyproject.toml"))

# add readme if it is specified
if "readme" in self._poetry.local_config:
additional_files.add(self._poetry.local_config["readme"])

for file in additional_files:
for additional_file in additional_files:
file = BuildIncludeFile(
path=file, project_root=self._path, source_root=self._path
path=additional_file, project_root=self._path, source_root=self._path
)
if file.path.exists():
logger.debug(f"Adding: {file.relative_to_source_root()}")
Expand Down
41 changes: 21 additions & 20 deletions src/poetry/core/masonry/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from io import StringIO
from pathlib import Path
from typing import TYPE_CHECKING
from typing import ContextManager
from typing import Iterator
from typing import TextIO

from packaging.tags import sys_tags
Expand Down Expand Up @@ -49,16 +49,14 @@ class WheelBuilder(Builder):
def __init__(
self,
poetry: Poetry,
target_dir: Path | None = None,
original: Path | None = None,
executable: str | None = None,
executable: Path | None = None,
editable: bool = False,
) -> None:
super().__init__(poetry, executable=executable)

self._records = []
self._records: list[tuple[str, str, int]] = []
self._original_path = self._path
self._target_dir = target_dir or (self._poetry.file.parent / "dist")
if original:
self._original_path = original.parent
self._editable = editable
Expand All @@ -69,31 +67,33 @@ def make_in(
poetry: Poetry,
directory: Path | None = None,
original: Path | None = None,
executable: str | None = None,
executable: Path | None = None,
editable: bool = False,
) -> str:
wb = WheelBuilder(
poetry,
target_dir=directory,
original=original,
executable=executable,
editable=editable,
)
wb.build()
wb.build(target_dir=directory)

return wb.wheel_filename

@classmethod
def make(cls, poetry: Poetry, executable: str | None = None) -> None:
def make(cls, poetry: Poetry, executable: Path | None = None) -> None:
"""Build a wheel in the dist/ directory, and optionally upload it."""
cls.make_in(poetry, executable=executable)

def build(self) -> None:
def build(
self,
target_dir: Path | None = None,
) -> Path:
logger.info("Building wheel")

dist_dir = self._target_dir
if not dist_dir.exists():
dist_dir.mkdir()
target_dir = target_dir or self.default_target_dir
if not target_dir.exists():
target_dir.mkdir()

(fd, temp_path) = tempfile.mkstemp(suffix=".whl")

Expand All @@ -119,12 +119,13 @@ def build(self) -> None:
self._write_metadata(zip_file)
self._write_record(zip_file)

wheel_path = dist_dir / self.wheel_filename
wheel_path = target_dir / self.wheel_filename
if wheel_path.exists():
wheel_path.unlink()
shutil.move(temp_path, str(wheel_path))

logger.info(f"Built {self.wheel_filename}")
return wheel_path

def _add_pth(self, wheel: zipfile.ZipFile) -> None:
paths = set()
Expand Down Expand Up @@ -168,14 +169,14 @@ def _build(self, wheel: zipfile.ZipFile) -> None:
os.chdir(current_path)

build_dir = self._path / "build"
lib = list(build_dir.glob("lib.*"))
if not lib:
libs: list[Path] = list(build_dir.glob("lib.*"))
if not libs:
# The result of building the extensions
# does not exist, this may due to conditional
# builds, so we assume that it's okay
return

lib = lib[0]
lib = libs[0]

for pkg in lib.glob("**/*"):
if pkg.is_dir() or self.is_excluded(pkg):
Expand Down Expand Up @@ -298,8 +299,8 @@ def dist_info_name(self, distribution: str, version: str) -> str:
@property
def tag(self) -> str:
if self._package.build_script:
tag = next(sys_tags())
tag = (tag.interpreter, tag.abi, tag.platform)
sys_tag = next(sys_tags())
tag = (sys_tag.interpreter, sys_tag.abi, sys_tag.platform)
else:
platform = "any"
if self.supports_python2():
Expand Down Expand Up @@ -352,7 +353,7 @@ def _add_file(
@contextlib.contextmanager
def _write_to_zip(
self, wheel: zipfile.ZipFile, rel_path: str
) -> ContextManager[StringIO]:
) -> Iterator[StringIO]:
sio = StringIO()
yield sio

Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Metadata:
metadata_version = "2.1"
# version 1.0
name = None
version = None
version: str
platforms = ()
supported_platforms = ()
summary = None
Expand Down
4 changes: 2 additions & 2 deletions src/poetry/core/masonry/utils/package_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
formats: list[str] | None = None,
source: str | None = None,
) -> None:
self._package: str | None = None
self._package: str
self._is_package = False
self._is_module = False
self._source = source
Expand All @@ -29,7 +29,7 @@ def __init__(
self.check_elements()

@property
def package(self) -> str | None:
def package(self) -> str:
return self._package

@property
Expand Down