Skip to content

Commit dfd8e4b

Browse files
Follow most recent packaging specification for wheel name normalization (#394)
* test(masonry): add tests for `escape_*` methods * feat(masonry): follow newer PyPA specification for wheel name As per https://packaging.python.org/en/latest/specifications/binary-distribution-format/#binary-distribution-format, the specification replaces the original PEP 427 specification. The specification for the distribution name is https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode. * refactor(masonry): indicate that `escape_name` is for generated wheels only * doc(masonry): improve `escape_name` documentation Co-authored-by: Bjorn Neergaard <[email protected]>
1 parent 392438a commit dfd8e4b

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/poetry/core/masonry/utils/helpers.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@ def escape_version(version: str) -> str:
2929

3030

3131
def escape_name(name: str) -> str:
32-
"""Escaped wheel name as specified in :pep:`427#escaping-and-unicode`."""
33-
return re.sub(r"[^\w\d.]+", "_", name, flags=re.UNICODE)
32+
"""
33+
Escaped wheel name as specified in https://packaging.python.org/en/latest/specifications/binary-distribution-format/#escaping-and-unicode.
34+
This function should only be used for the generation of artifact names, and not to normalize or filter existing artifact names.
35+
"""
36+
return re.sub(r"[-_.]+", "_", name, flags=re.UNICODE).lower()

tests/masonry/utils/test_helpers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from poetry.core.masonry.utils.helpers import escape_name
6+
from poetry.core.masonry.utils.helpers import escape_version
7+
8+
9+
@pytest.mark.parametrize(
10+
"version,expected",
11+
[
12+
("1.2.3", "1.2.3"),
13+
("1.2.3_1", "1.2.3_1"),
14+
("1.2.3-1", "1.2.3_1"),
15+
("1.2.3-1", "1.2.3_1"),
16+
("2022.2", "2022.2"),
17+
("12.20.12-----451---14-1-4-41", "12.20.12_451_14_1_4_41"),
18+
("1.0b2.dev1", "1.0b2.dev1"),
19+
("1.0+abc.7", "1.0+abc.7"),
20+
],
21+
)
22+
def test_escape_version(version: str, expected: str) -> None:
23+
assert escape_version(version) == expected
24+
25+
26+
@pytest.mark.parametrize(
27+
"name,expected",
28+
[
29+
("foo", "foo"),
30+
("foo-bar", "foo_bar"),
31+
("FOO-bAr", "foo_bar"),
32+
("foo.bar", "foo_bar"),
33+
("foo123-ba---.r", "foo123_ba_r"),
34+
],
35+
)
36+
def test_escape_name(name: str, expected: str) -> None:
37+
assert escape_name(name) == expected

0 commit comments

Comments
 (0)