Skip to content

Commit

Permalink
typechecking poetry.core.masonry
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Apr 27, 2022
1 parent b1cc918 commit 810b42b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 52 deletions.
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,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',
Expand Down
2 changes: 1 addition & 1 deletion src/poetry/core/masonry/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 10 additions & 25 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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
Expand All @@ -56,12 +56,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)
Expand All @@ -70,12 +65,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)
Expand All @@ -93,7 +83,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]:
Expand Down Expand Up @@ -203,10 +193,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)

Expand Down Expand Up @@ -349,6 +335,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 +366,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)
Expand Down
27 changes: 19 additions & 8 deletions src/poetry/core/masonry/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 -*-
Expand Down Expand Up @@ -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 <info>sdist</info>")
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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand All @@ -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.
Expand All @@ -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("*")
Expand Down
25 changes: 13 additions & 12 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 @@ -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:
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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()
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

0 comments on commit 810b42b

Please sign in to comment.