Skip to content

Commit 5168881

Browse files
committed
Implement PEP 714 - rename dist-info-metadata
1 parent 232cc9d commit 5168881

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/pip/_internal/models/link.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,16 +277,21 @@ def from_json(
277277
yanked_reason = file_data.get("yanked")
278278
hashes = file_data.get("hashes", {})
279279

280-
# The dist-info-metadata value may be a boolean, or a dict of hashes.
281-
metadata_info = file_data.get("dist-info-metadata", False)
280+
# PEP 714: Indexes must use the name core-metadata, but
281+
# clients should support the old name as a fallback for compatibility.
282+
metadata_info = file_data.get("core-metadata")
283+
if metadata_info is None:
284+
metadata_info = file_data.get("dist-info-metadata")
285+
286+
# The metadata info value may be a boolean, or a dict of hashes.
282287
if isinstance(metadata_info, dict):
283288
# The file exists, and hashes have been supplied
284289
metadata_file_data = MetadataFile(supported_hashes(metadata_info))
285290
elif metadata_info:
286291
# The file exists, but there are no hashes
287292
metadata_file_data = MetadataFile(None)
288293
else:
289-
# The file does not exist
294+
# False or not present: the file does not exist
290295
metadata_file_data = None
291296

292297
# The Link.yanked_reason expects an empty string instead of a boolean.
@@ -323,9 +328,13 @@ def from_element(
323328
pyrequire = anchor_attribs.get("data-requires-python")
324329
yanked_reason = anchor_attribs.get("data-yanked")
325330

326-
# The dist-info-metadata value may be the string "true", or a string of
331+
# PEP 714: Indexes must use the name data-core-metadata, but
332+
# clients should support the old name as a fallback for compatibility.
333+
metadata_info = anchor_attribs.get("data-core-metadata")
334+
if metadata_info is None:
335+
metadata_info = anchor_attribs.get("data-dist-info-metadata")
336+
# The metadata info value may be the string "true", or a string of
327337
# the form "hashname=hashval"
328-
metadata_info = anchor_attribs.get("data-dist-info-metadata")
329338
if metadata_info == "true":
330339
# The file exists, but there are no hashes
331340
metadata_file_data = MetadataFile(None)

tests/unit/test_collector.py

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,31 @@ def test_parse_links_json() -> None:
486486
"requires-python": ">=3.7",
487487
"dist-info-metadata": False,
488488
},
489-
# Same as above, but parsing dist-info-metadata.
489+
# Same as above, but parsing core-metadata.
490+
{
491+
"filename": "holygrail-1.0-py3-none-any.whl",
492+
"url": "/files/holygrail-1.0-py3-none-any.whl",
493+
"hashes": {"sha256": "sha256 hash", "blake2b": "blake2b hash"},
494+
"requires-python": ">=3.7",
495+
"core-metadata": {"sha512": "aabdd41"},
496+
},
497+
# Ensure fallback to dist-info-metadata works
490498
{
491499
"filename": "holygrail-1.0-py3-none-any.whl",
492500
"url": "/files/holygrail-1.0-py3-none-any.whl",
493501
"hashes": {"sha256": "sha256 hash", "blake2b": "blake2b hash"},
494502
"requires-python": ">=3.7",
495503
"dist-info-metadata": {"sha512": "aabdd41"},
496504
},
505+
# Ensure that core-metadata gets priority.
506+
{
507+
"filename": "holygrail-1.0-py3-none-any.whl",
508+
"url": "/files/holygrail-1.0-py3-none-any.whl",
509+
"hashes": {"sha256": "sha256 hash", "blake2b": "blake2b hash"},
510+
"requires-python": ">=3.7",
511+
"core-metadata": {"sha512": "aabdd41"},
512+
"dist-info-metadata": {"sha512": "this_is_wrong"},
513+
},
497514
],
498515
}
499516
).encode("utf8")
@@ -530,6 +547,22 @@ def test_parse_links_json() -> None:
530547
hashes={"sha256": "sha256 hash", "blake2b": "blake2b hash"},
531548
metadata_file_data=MetadataFile({"sha512": "aabdd41"}),
532549
),
550+
Link(
551+
"https://example.com/files/holygrail-1.0-py3-none-any.whl",
552+
comes_from=page.url,
553+
requires_python=">=3.7",
554+
yanked_reason=None,
555+
hashes={"sha256": "sha256 hash", "blake2b": "blake2b hash"},
556+
metadata_file_data=MetadataFile({"sha512": "aabdd41"}),
557+
),
558+
Link(
559+
"https://example.com/files/holygrail-1.0-py3-none-any.whl",
560+
comes_from=page.url,
561+
requires_python=">=3.7",
562+
yanked_reason=None,
563+
hashes={"sha256": "sha256 hash", "blake2b": "blake2b hash"},
564+
metadata_file_data=MetadataFile({"sha512": "aabdd41"}),
565+
),
533566
]
534567

535568
# Ensure the metadata info can be parsed into the correct link.
@@ -586,22 +619,34 @@ def test_parse_links__yanked_reason(anchor_html: str, expected: Optional[str]) -
586619
),
587620
# Test with value "true".
588621
(
589-
'<a href="/pkg1-1.0.tar.gz" data-dist-info-metadata="true"></a>',
622+
'<a href="/pkg1-1.0.tar.gz" data-core-metadata="true"></a>',
590623
MetadataFile(None),
591624
{},
592625
),
593626
# Test with a provided hash value.
594627
(
595-
'<a href="/pkg1-1.0.tar.gz" data-dist-info-metadata="sha256=aa113592bbe"></a>', # noqa: E501
628+
'<a href="/pkg1-1.0.tar.gz" data-core-metadata="sha256=aa113592bbe"></a>', # noqa: E501
596629
MetadataFile({"sha256": "aa113592bbe"}),
597630
{},
598631
),
599632
# Test with a provided hash value for both the requirement as well as metadata.
600633
(
601-
'<a href="/pkg1-1.0.tar.gz#sha512=abc132409cb" data-dist-info-metadata="sha256=aa113592bbe"></a>', # noqa: E501
634+
'<a href="/pkg1-1.0.tar.gz#sha512=abc132409cb" data-core-metadata="sha256=aa113592bbe"></a>', # noqa: E501
602635
MetadataFile({"sha256": "aa113592bbe"}),
603636
{"sha512": "abc132409cb"},
604637
),
638+
# Ensure the fallback to the old name works.
639+
(
640+
'<a href="/pkg1-1.0.tar.gz" data-dist-info-metadata="sha256=aa113592bbe"></a>', # noqa: E501
641+
MetadataFile({"sha256": "aa113592bbe"}),
642+
{},
643+
),
644+
# Ensure that the data-core-metadata name gets priority.
645+
(
646+
'<a href="/pkg1-1.0.tar.gz" data-core-metadata="sha256=aa113592bbe" data-dist-info-metadata="sha256=invalid_value"></a>', # noqa: E501
647+
MetadataFile({"sha256": "aa113592bbe"}),
648+
{},
649+
),
605650
],
606651
)
607652
def test_parse_links__metadata_file_data(

0 commit comments

Comments
 (0)