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 Feb 9, 2022
1 parent 4e5207e commit d57e384
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 54 deletions.
88 changes: 87 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ python = "^3.7"
importlib-metadata = {version = ">=1.7.0", python = "<3.8"}

[tool.poetry.dev-dependencies]
mypy = ">=0.931"
pre-commit = "^2.15.0"
pyrsistent = "^0.18.0"
pytest = "^6.2"
Expand Down Expand Up @@ -79,6 +80,7 @@ exclude = "(?x)(^tests/.*/fixtures | ^src/poetry/core/_vendor)"
[[tool.mypy.overrides]]
module = [
'jsonschema.*',
'lark.*',
'setuptools.*',
'tomlkit.*',
]
Expand All @@ -93,9 +95,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 @@ -74,7 +74,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
36 changes: 10 additions & 26 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class Builder:
def __init__(
self,
poetry: "Poetry",
ignore_packages_formats: bool = False,
executable: Optional[Union[Path, str]] = None,
executable: Optional[Path] = None,
) -> None:
from poetry.core.masonry.metadata import Metadata
from poetry.core.masonry.utils.module import Module
Expand All @@ -59,12 +58,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 @@ -73,12 +67,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 @@ -96,7 +85,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: Optional[str] = None) -> Set[str]:
Expand Down Expand Up @@ -206,10 +195,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 @@ -352,6 +337,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 @@ -381,14 +368,11 @@ def __init__(

self.path = self.path.resolve()

def __eq__(self, other: Union["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: Union["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 @@ -13,8 +13,8 @@
from pprint import pformat
from tarfile import TarInfo
from typing import TYPE_CHECKING
from typing import ContextManager
from typing import Dict
from typing import Iterator
from typing import List
from typing import Optional
from typing import Set
Expand All @@ -28,6 +28,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 @@ -58,10 +59,18 @@ class SdistBuilder(Builder):

format = "sdist"

def build(self, target_dir: Optional[Path] = None) -> Path:
def __init__(
self,
poetry: "Poetry",
executable: Optional[Path] = None,
target_dir: Optional[Path] = 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 @@ -116,7 +125,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 @@ -216,7 +225,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 @@ -234,7 +243,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[Optional[str], List[str], Dict[str, List[str]]]:
"""
Discover subpackages and data.
Expand All @@ -247,7 +258,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
Loading

0 comments on commit d57e384

Please sign in to comment.