From 4f88be8b48584951a4544d9339a883dd68b7609b Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 29 Jan 2022 18:42:24 +0000 Subject: [PATCH 1/5] typechecking poetry.core.masonry --- pyproject.toml | 3 -- src/poetry/core/masonry/api.py | 2 +- src/poetry/core/masonry/builders/builder.py | 36 ++++++------------- src/poetry/core/masonry/builders/sdist.py | 23 ++++++++---- src/poetry/core/masonry/builders/wheel.py | 25 ++++++------- src/poetry/core/masonry/metadata.py | 2 +- .../core/masonry/utils/package_include.py | 4 +-- 7 files changed, 44 insertions(+), 51 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 98d141e4d..6a3edafcd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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', diff --git a/src/poetry/core/masonry/api.py b/src/poetry/core/masonry/api.py index 2ed4ed5f3..4850c1420 100644 --- a/src/poetry/core/masonry/api.py +++ b/src/poetry/core/masonry/api.py @@ -73,7 +73,7 @@ def build_sdist( """Builds an sdist, places it in sdist_directory""" poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) - path = SdistBuilder(poetry).build(Path(sdist_directory)) + path = SdistBuilder(poetry, target_dir=Path(sdist_directory)).build() return path.name diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 847d2d1ab..6e9fc6f45 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -32,8 +32,7 @@ class Builder: 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 @@ -56,12 +55,7 @@ def __init__( if not isinstance(formats, list): formats = [formats] - if ( - formats - and self.format - and self.format not in formats - and not ignore_packages_formats - ): + if formats and self.format and self.format not in formats: continue packages.append(p) @@ -70,12 +64,7 @@ def __init__( for include in self._package.include: formats = include.get("format", []) - if ( - formats - and self.format - and self.format not in formats - and not ignore_packages_formats - ): + if formats and self.format and self.format not in formats: continue includes.append(include) @@ -93,7 +82,7 @@ def __init__( def executable(self) -> Path: return self._executable - def build(self) -> None: + def build(self) -> Path: raise NotImplementedError() def find_excluded_files(self, fmt: str | None = None) -> set[str]: @@ -203,10 +192,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 - continue - logger.debug(f"Adding: {str(file)}") to_add.add(include_file) @@ -349,6 +334,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") @@ -378,14 +365,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: + if not isinstance(other, BuildIncludeFile): + 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) diff --git a/src/poetry/core/masonry/builders/sdist.py b/src/poetry/core/masonry/builders/sdist.py index 3b8a36dcf..bbfec8b3b 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -25,6 +25,7 @@ from poetry.core.masonry.utils.package_include import PackageInclude from poetry.core.packages.dependency import Dependency from poetry.core.packages.project_package import ProjectPackage + from poetry.core.poetry import Poetry SETUP = """\ # -*- coding: utf-8 -*- @@ -55,10 +56,18 @@ class SdistBuilder(Builder): format = "sdist" - def build(self, target_dir: Path | None = None) -> Path: + def __init__( + self, + poetry: Poetry, + executable: Path | None = None, + target_dir: Path | None = None, + ) -> None: + super().__init__(poetry, executable=executable) + self._target_dir = target_dir + + def build(self) -> Path: logger.info("Building sdist") - if target_dir is None: - target_dir = self._path / "dist" + target_dir = self._target_dir or self._path / "dist" if not target_dir.exists(): target_dir.mkdir(parents=True) @@ -113,7 +122,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: @@ -233,7 +242,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. @@ -246,7 +257,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("*") diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 34da6eceb..843d7c751 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -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 @@ -51,12 +51,12 @@ def __init__( 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: @@ -69,7 +69,7 @@ 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( @@ -84,11 +84,11 @@ def make_in( 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) -> Path: logger.info("Building wheel") dist_dir = self._target_dir @@ -125,6 +125,7 @@ def build(self) -> None: 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() @@ -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): @@ -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(): @@ -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 diff --git a/src/poetry/core/masonry/metadata.py b/src/poetry/core/masonry/metadata.py index ba6d2ef74..9e2530ae7 100644 --- a/src/poetry/core/masonry/metadata.py +++ b/src/poetry/core/masonry/metadata.py @@ -14,7 +14,7 @@ class Metadata: metadata_version = "2.1" # version 1.0 name = None - version = None + version: str platforms = () supported_platforms = () summary = None diff --git a/src/poetry/core/masonry/utils/package_include.py b/src/poetry/core/masonry/utils/package_include.py index 1b52b7de2..643d02f83 100644 --- a/src/poetry/core/masonry/utils/package_include.py +++ b/src/poetry/core/masonry/utils/package_include.py @@ -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 @@ -29,7 +29,7 @@ def __init__( self.check_elements() @property - def package(self) -> str | None: + def package(self) -> str: return self._package @property From 15d2419b96258b4925087eb27bb9c68b55a10325 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 30 Apr 2022 12:19:19 +0100 Subject: [PATCH 2/5] reinstate ignore_packages_formats --- src/poetry/core/masonry/builders/builder.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 6e9fc6f45..deaf7f396 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -32,6 +32,7 @@ class Builder: def __init__( self, poetry: Poetry, + ignore_packages_formats: bool = False, executable: Path | None = None, ) -> None: from poetry.core.masonry.metadata import Metadata @@ -55,7 +56,12 @@ def __init__( if not isinstance(formats, list): formats = [formats] - if formats and self.format and self.format not in formats: + if ( + formats + and self.format + and self.format not in formats + and not ignore_packages_formats + ): continue packages.append(p) @@ -64,7 +70,12 @@ def __init__( for include in self._package.include: formats = include.get("format", []) - if formats and self.format and self.format not in formats: + if ( + formats + and self.format + and self.format not in formats + and not ignore_packages_formats + ): continue includes.append(include) From 7778af2120cac6586de2bc995e5d306bae330539 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 30 Apr 2022 12:59:20 +0100 Subject: [PATCH 3/5] shuffling the Builder methods target_directory is removed from the constructor, included on `build()` --- src/poetry/core/masonry/api.py | 2 +- src/poetry/core/masonry/builders/builder.py | 2 +- src/poetry/core/masonry/builders/sdist.py | 9 +++++---- src/poetry/core/masonry/builders/wheel.py | 12 ++++++------ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/poetry/core/masonry/api.py b/src/poetry/core/masonry/api.py index 4850c1420..2ed4ed5f3 100644 --- a/src/poetry/core/masonry/api.py +++ b/src/poetry/core/masonry/api.py @@ -73,7 +73,7 @@ def build_sdist( """Builds an sdist, places it in sdist_directory""" poetry = Factory().create_poetry(Path(".").resolve(), with_groups=False) - path = SdistBuilder(poetry, target_dir=Path(sdist_directory)).build() + path = SdistBuilder(poetry).build(Path(sdist_directory)) return path.name diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index deaf7f396..329ccebab 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -93,7 +93,7 @@ def __init__( def executable(self) -> Path: return self._executable - def build(self) -> Path: + def build(self, target_dir: Path | None) -> Path: raise NotImplementedError() def find_excluded_files(self, fmt: str | None = None) -> set[str]: diff --git a/src/poetry/core/masonry/builders/sdist.py b/src/poetry/core/masonry/builders/sdist.py index bbfec8b3b..8ad411999 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -60,14 +60,15 @@ def __init__( self, poetry: Poetry, executable: Path | None = None, - target_dir: Path | None = None, ) -> None: super().__init__(poetry, executable=executable) - self._target_dir = target_dir - def build(self) -> Path: + def build( + self, + target_dir: Path | None = None, + ) -> Path: logger.info("Building sdist") - target_dir = self._target_dir or self._path / "dist" + target_dir = target_dir or self._path / "dist" if not target_dir.exists(): target_dir.mkdir(parents=True) diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 843d7c751..7ec872b86 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -49,7 +49,6 @@ class WheelBuilder(Builder): def __init__( self, poetry: Poetry, - target_dir: Path | None = None, original: Path | None = None, executable: Path | None = None, editable: bool = False, @@ -58,7 +57,6 @@ def __init__( 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 @@ -74,12 +72,11 @@ def make_in( ) -> str: wb = WheelBuilder( poetry, - target_dir=directory, original=original, executable=executable, editable=editable, ) - wb.build() + wb.build(target_dir=directory) return wb.wheel_filename @@ -88,10 +85,13 @@ 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) -> Path: + def build( + self, + target_dir: Path | None = None, + ) -> Path: logger.info("Building wheel") - dist_dir = self._target_dir + dist_dir = target_dir or (self._poetry.file.parent / "dist") if not dist_dir.exists(): dist_dir.mkdir() From 58d0c1ddb146998a6094d6a74390c3e843fac3e4 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 7 May 2022 16:58:23 +0100 Subject: [PATCH 4/5] remove unnecessary __init__ --- src/poetry/core/masonry/builders/sdist.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/poetry/core/masonry/builders/sdist.py b/src/poetry/core/masonry/builders/sdist.py index 8ad411999..1c708db23 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -25,7 +25,6 @@ from poetry.core.masonry.utils.package_include import PackageInclude from poetry.core.packages.dependency import Dependency from poetry.core.packages.project_package import ProjectPackage - from poetry.core.poetry import Poetry SETUP = """\ # -*- coding: utf-8 -*- @@ -56,13 +55,6 @@ class SdistBuilder(Builder): format = "sdist" - def __init__( - self, - poetry: Poetry, - executable: Path | None = None, - ) -> None: - super().__init__(poetry, executable=executable) - def build( self, target_dir: Path | None = None, From f6258d7ae982b50b96ea5d98ff2c271b95f7b894 Mon Sep 17 00:00:00 2001 From: David Hotham Date: Sat, 7 May 2022 17:18:05 +0100 Subject: [PATCH 5/5] introduce default_target_dir on builders --- src/poetry/core/masonry/builders/builder.py | 6 +++++- src/poetry/core/masonry/builders/sdist.py | 8 ++++---- src/poetry/core/masonry/builders/wheel.py | 8 ++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/poetry/core/masonry/builders/builder.py b/src/poetry/core/masonry/builders/builder.py index 329ccebab..0a546ae57 100644 --- a/src/poetry/core/masonry/builders/builder.py +++ b/src/poetry/core/masonry/builders/builder.py @@ -40,7 +40,7 @@ def __init__( 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) @@ -93,6 +93,10 @@ def __init__( def executable(self) -> Path: return self._executable + @property + def default_target_dir(self) -> Path: + return self._path / "dist" + def build(self, target_dir: Path | None) -> Path: raise NotImplementedError() diff --git a/src/poetry/core/masonry/builders/sdist.py b/src/poetry/core/masonry/builders/sdist.py index 1c708db23..ecdd08a49 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -60,7 +60,7 @@ def build( target_dir: Path | None = None, ) -> Path: logger.info("Building sdist") - target_dir = target_dir or self._path / "dist" + target_dir = target_dir or self.default_target_dir if not target_dir.exists(): target_dir.mkdir(parents=True) @@ -326,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()}") diff --git a/src/poetry/core/masonry/builders/wheel.py b/src/poetry/core/masonry/builders/wheel.py index 7ec872b86..aa87dde31 100644 --- a/src/poetry/core/masonry/builders/wheel.py +++ b/src/poetry/core/masonry/builders/wheel.py @@ -91,9 +91,9 @@ def build( ) -> Path: logger.info("Building wheel") - dist_dir = target_dir or (self._poetry.file.parent / "dist") - 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") @@ -119,7 +119,7 @@ def build( 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))