diff --git a/pyproject.toml b/pyproject.toml index d606cd6ff..503041f4c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -79,6 +79,7 @@ exclude = "(?x)(^tests/.*/fixtures | ^src/poetry/core/_vendor)" [[tool.mypy.overrides]] module = [ 'jsonschema.*', + 'lark.*', 'setuptools.*', 'tomlkit.*', ] @@ -93,9 +94,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 08c17b78c..aef1ddaea 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 69a950343..7ae26d651 100644 --- a/src/poetry/core/masonry/builders/sdist.py +++ b/src/poetry/core/masonry/builders/sdist.py @@ -15,7 +15,7 @@ from pprint import pformat from tarfile import TarInfo from typing import TYPE_CHECKING -from typing import ContextManager +from typing import Iterator from poetry.core.masonry.builders.builder import Builder from poetry.core.masonry.builders.builder import BuildIncludeFile @@ -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: @@ -213,7 +222,7 @@ def build_setup(self) -> bytes: ).encode() @contextmanager - def setup_py(self) -> ContextManager[Path]: + def setup_py(self) -> Iterator[Path]: setup = self._path / "setup.py" has_setup = setup.exists() @@ -231,7 +240,9 @@ def setup_py(self) -> ContextManager[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. @@ -244,7 +255,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 66eecbe92..ecf7a2611 100644 --- a/src/poetry/core/masonry/utils/package_include.py +++ b/src/poetry/core/masonry/utils/package_include.py @@ -13,7 +13,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 @@ -25,7 +25,7 @@ def __init__( self.check_elements() @property - def package(self) -> str | None: + def package(self) -> str: return self._package @property