Skip to content

fix(metadata): ignore optional deps not part of an extra #830

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

Merged
merged 1 commit into from
Feb 1, 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
6 changes: 5 additions & 1 deletion src/poetry/core/masonry/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ def from_package(cls, package: ProjectPackage) -> Metadata:
elif package.python_versions != "*":
meta.requires_python = format_python_constraint(package.python_constraint)

meta.requires_dist = [d.to_pep_508() for d in package.requires]
meta.requires_dist = [
d.to_pep_508()
for d in package.requires
if not d.is_optional() or d.in_extras
]

# Version 2.1
if package.readme_content_type:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[project]
name = "my-packager"
description = "Something"
version = "0.1"
classifiers = [
"Topic :: Software Development :: Build Tools",
"Topic :: Software Development :: Libraries :: Python Modules"
]
dynamic = ["dependencies"]

[tool.poetry.dependencies]
requests = { version = "^0.28.1", optional = true }
httpx = { version = "^0.28.1", optional = true }
grpcio = { version = "^0.2.0" }
pycowsay = { version = "^0.1.0" }

[tool.poetry.extras]
http = ["httpx"]
grpc = ["grpcio"]
28 changes: 28 additions & 0 deletions tests/masonry/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,34 @@ def test_prepare_metadata_for_build_wheel_with_local_version() -> None:
assert f.read() == metadata


def test_prepare_metadata_excludes_optional_without_extras() -> None:
with (
temporary_directory() as tmp_dir,
cwd(fixtures / "with_optional_without_extras"),
):
dirname = api.prepare_metadata_for_build_wheel(str(tmp_dir))
dist_info = Path(tmp_dir, dirname)
assert (dist_info / "METADATA").exists()

with (dist_info / "METADATA").open(encoding="utf-8") as f:
assert (
f.read()
== """\
Metadata-Version: 2.3
Name: my-packager
Version: 0.1
Summary: Something
Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Provides-Extra: grpc
Provides-Extra: http
Requires-Dist: grpcio (>=0.2.0,<0.3.0) ; extra == "grpc"
Requires-Dist: httpx (>=0.28.1,<0.29.0) ; extra == "http"
Requires-Dist: pycowsay (>=0.1.0,<0.2.0)
"""
)


def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds() -> None:
with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dev_dep"):
api.prepare_metadata_for_build_wheel(str(tmp_dir))
Expand Down