Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix handling of "format" in package includes initialization #805

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 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]]:
result = []
for item in items:
formats = item.get("format", default_formats)
if not isinstance(formats, list):
formats = [formats]

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

return result

@classmethod
def _configure_package_poetry_specifics(
cls, package: ProjectPackage, tool_poetry: dict[str, Any]
Expand All @@ -367,16 +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"):
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
finswimmer marked this conversation as resolved.
Show resolved Hide resolved
# 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 = [
item
for item in self._package.packages
if not self.format or self.format in item["format"]
]
includes = [
item
for item in self._package.include
if not self.format or self.format in item["format"]
]

return Module(
self._package.name,
Expand Down
27 changes: 18 additions & 9 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,29 @@ def test_create_poetry_with_packages_and_includes() -> None:
package = poetry.package

assert package.packages == [
{"include": "extra_dir/**/*.py"},
{"include": "extra_dir/**/*.py"},
{"include": "my_module.py"},
{"include": "package_with_include"},
{"include": "tests", "format": "sdist"},
{"include": "extra_dir/**/*.py", "format": ["sdist", "wheel"]},
sourcery-ai[bot] marked this conversation as resolved.
Show resolved Hide resolved
{"include": "extra_dir/**/*.py", "format": ["sdist", "wheel"]},
{"include": "my_module.py", "format": ["sdist", "wheel"]},
{"include": "package_with_include", "format": ["sdist", "wheel"]},
{"include": "tests", "format": ["sdist"]},
{"include": "for_wheel_only", "format": ["wheel"]},
{"include": "src_package", "from": "src"},
{"include": "from_to", "from": "etc", "to": "target_from_to"},
{"include": "my_module_to.py", "to": "target_module"},
{"include": "src_package", "from": "src", "format": ["sdist", "wheel"]},
{
"include": "from_to",
"from": "etc",
"to": "target_from_to",
"format": ["sdist", "wheel"],
},
{
"include": "my_module_to.py",
"to": "target_module",
"format": ["sdist", "wheel"],
},
]

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Test explicit format exclusion

Add a test case where "format" explicitly excludes a default format (e.g., format: [] or format: ['wheel'] for a package include) to verify that the package is correctly excluded from the specified format.

Suggested implementation:

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

You'll also need to:

  1. Create a 'format_excluded.txt' file in the test directory structure
  2. Add the corresponding entry in the test setup/fixture that creates this test file
  3. Update any related test data that defines the package includes to include this new file

]


Expand Down
Loading