Skip to content

Commit f4f327c

Browse files
committed
fix(metadata): ignore optional deps not part of an extra
Resolves: #2357
1 parent ad5f274 commit f4f327c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/poetry/core/masonry/metadata.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ def from_package(cls, package: ProjectPackage) -> Metadata:
9595
elif package.python_versions != "*":
9696
meta.requires_python = format_python_constraint(package.python_constraint)
9797

98-
meta.requires_dist = [d.to_pep_508() for d in package.requires]
98+
meta.requires_dist = [
99+
d.to_pep_508()
100+
for d in package.requires
101+
if not d.is_optional() or d.in_extras
102+
]
99103

100104
# Version 2.1
101105
if package.readme_content_type:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[project]
2+
name = "my-packager"
3+
description = "Something"
4+
version = "0.1"
5+
classifiers = [
6+
"Topic :: Software Development :: Build Tools",
7+
"Topic :: Software Development :: Libraries :: Python Modules"
8+
]
9+
dynamic = ["dependencies"]
10+
11+
[tool.poetry.dependencies]
12+
requests = { version = "^0.28.1", optional = true }
13+
httpx = { version = "^0.28.1", optional = true }
14+
grpcio = { version = "^0.2.0" }
15+
pycowsay = { version = "^0.1.0" }
16+
17+
[tool.poetry.extras]
18+
http = ["httpx"]
19+
grpc = ["grpcio"]

tests/masonry/test_api.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,34 @@ def test_prepare_metadata_for_build_wheel_with_local_version() -> None:
313313
assert f.read() == metadata
314314

315315

316+
def test_prepare_metadata_excludes_optional_without_extras() -> None:
317+
with (
318+
temporary_directory() as tmp_dir,
319+
cwd(fixtures / "with_optional_without_extras"),
320+
):
321+
dirname = api.prepare_metadata_for_build_wheel(str(tmp_dir))
322+
dist_info = Path(tmp_dir, dirname)
323+
assert (dist_info / "METADATA").exists()
324+
325+
with (dist_info / "METADATA").open(encoding="utf-8") as f:
326+
assert (
327+
f.read()
328+
== """\
329+
Metadata-Version: 2.3
330+
Name: my-packager
331+
Version: 0.1
332+
Summary: Something
333+
Classifier: Topic :: Software Development :: Build Tools
334+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
335+
Provides-Extra: grpc
336+
Provides-Extra: http
337+
Requires-Dist: grpcio (>=0.2.0,<0.3.0) ; extra == "grpc"
338+
Requires-Dist: httpx (>=0.28.1,<0.29.0) ; extra == "http"
339+
Requires-Dist: pycowsay (>=0.1.0,<0.2.0)
340+
"""
341+
)
342+
343+
316344
def test_prepare_metadata_for_build_wheel_with_bad_path_dev_dep_succeeds() -> None:
317345
with temporary_directory() as tmp_dir, cwd(fixtures / "with_bad_path_dev_dep"):
318346
api.prepare_metadata_for_build_wheel(str(tmp_dir))

0 commit comments

Comments
 (0)