Skip to content

Commit

Permalink
refactor: set default formats for includes in Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Jan 9, 2025
1 parent 4e47388 commit 977ec56
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
33 changes: 23 additions & 10 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from typing import TYPE_CHECKING
from typing import Any
from typing import Literal
from typing import Union

from packaging.utils import canonicalize_name
Expand Down Expand Up @@ -357,6 +358,22 @@ def _configure_package_dependencies(

package.extras = package_extras

@classmethod
def _prepare_formats(
cls,
items: list[dict[str, Any]],
default_formats: list[Literal["sdist", "wheel"]],
) -> list[dict[str, Any]]:
items_ = []
for item in items:
formats = item.get("format", default_formats)
if not isinstance(formats, list):
formats = [formats]

items_.append({**item, "format": formats})

return items_

@classmethod
def _configure_package_poetry_specifics(
cls, package: ProjectPackage, tool_poetry: dict[str, Any]
Expand All @@ -367,24 +384,20 @@ def _configure_package_poetry_specifics(
package.build_config = build or {}

if includes := tool_poetry.get("include"):
package.include = [
includes_ = [
include if isinstance(include, dict) else {"path": include}
for include in includes
]

package.include = cls._prepare_formats(includes_, default_formats=["sdist"])

if exclude := tool_poetry.get("exclude"):
package.exclude = exclude

if packages := tool_poetry.get("packages"):
packages_ = []
for p in packages:
formats = p.get("format", ["sdist", "wheel"])
if not isinstance(formats, list):
formats = [formats]

packages_.append({**p, "format": formats})

package.packages = packages_
package.packages = cls._prepare_formats(
packages, default_formats=["sdist", "wheel"]
)

@classmethod
def create_dependency(
Expand Down
27 changes: 10 additions & 17 deletions src/poetry/core/masonry/builders/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,16 @@ def __init__(self, poetry: Poetry, executable: Path | None = None) -> None:
def _module(self) -> Module:
from poetry.core.masonry.utils.module import Module

packages: list[dict[str, str | dict[str, str]]] = []
includes: list[dict[str, str | dict[str, str]]] = []
for source_list, target_list, default in [
(self._package.packages, packages, ["sdist", "wheel"]),
(self._package.include, includes, ["sdist"]),
]:
for item in source_list:
# Default to including in both sdist & wheel
# if the `format` key is not provided.
formats = item.get("format", default)
if not isinstance(formats, list):
formats = [formats]

if self.format and self.format not in formats:
continue

target_list.append({**item, "format": formats})
packages: list[dict[str, str | dict[str, str]]] = [
{**item}
for item in self._package.packages
if not self.format or self.format in item["format"]
]
includes: list[dict[str, str | dict[str, str]]] = [
{**item}
for item in self._package.include
if not self.format or self.format in item["format"]
]

return Module(
self._package.name,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def test_create_poetry_with_packages_and_includes() -> None:

assert package.include == [
{"path": "extra_dir/vcs_excluded.py", "format": ["sdist", "wheel"]},
{"path": "notes.txt"},
{"path": "notes.txt", "format": ["sdist"]},
]


Expand Down

0 comments on commit 977ec56

Please sign in to comment.