Skip to content

Commit 8ed36bd

Browse files
authored
Fix project name canonicalization. (#10628)
Since we already depend on packaging, use its authoritative canonicalization that tracks PEP-503.
1 parent 765fe64 commit 8ed36bd

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/python/pants/backend/python/rules/pex_from_targets.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from dataclasses import dataclass
77
from typing import Iterable, Optional, Tuple
88

9+
from packaging.utils import canonicalize_name as canonicalize_project_name
910
from pkg_resources import Requirement, parse_requirements
1011

1112
from pants.backend.python.rules.pex import (
@@ -188,11 +189,13 @@ async def pex_from_targets(request: PexFromTargetsRequest, python_setup: PythonS
188189
description = request.description
189190

190191
if python_setup.requirement_constraints:
191-
# In requirement strings Foo_Bar and foo-bar refer to the same project.
192-
def canonical(name: str) -> str:
193-
return name.lower().replace("_", "-")
192+
# In requirement strings Foo_-Bar.BAZ and foo-bar-baz refer to the same project. We let
193+
# packaging canonicalize for us.
194+
# See: https://www.python.org/dev/peps/pep-0503/#normalized-names
194195

195-
exact_req_projects = {canonical(Requirement.parse(req).project_name) for req in exact_reqs}
196+
exact_req_projects = {
197+
canonicalize_project_name(Requirement.parse(req).project_name) for req in exact_reqs
198+
}
196199
constraints_file_contents = await Get(
197200
DigestContents,
198201
PathGlobs(
@@ -205,7 +208,9 @@ def canonical(name: str) -> str:
205208
constraints_file_reqs = set(
206209
parse_requirements(next(iter(constraints_file_contents)).content.decode())
207210
)
208-
constraint_file_projects = {canonical(req.project_name) for req in constraints_file_reqs}
211+
constraint_file_projects = {
212+
canonicalize_project_name(req.project_name) for req in constraints_file_reqs
213+
}
209214
unconstrained_projects = exact_req_projects - constraint_file_projects
210215
if unconstrained_projects:
211216
logger.warning(

src/python/pants/backend/python/rules/pex_from_targets_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_constraints_validation(self) -> None:
4747
dedent(
4848
"""
4949
python_requirement_library(name="foo",
50-
requirements=[python_requirement("foo>=0.1.2")])
50+
requirements=[python_requirement("foo-bar>=0.1.2")])
5151
python_requirement_library(name="bar",
5252
requirements=[ python_requirement("bar==5.5.5")])
5353
python_requirement_library(name="baz",
@@ -60,7 +60,7 @@ def test_constraints_validation(self) -> None:
6060
"constraints1.txt",
6161
dedent(
6262
"""
63-
foo==1.0.0
63+
Foo._-BAR==1.0.0
6464
bar==5.5.5
6565
baz==2.2.2
6666
qux==3.4.5
@@ -97,11 +97,11 @@ def get_pex_request(
9797
)
9898

9999
pex_req1 = get_pex_request("constraints1.txt", ResolveAllConstraintsOption.NEVER)
100-
assert pex_req1.requirements == PexRequirements(["foo>=0.1.2", "bar==5.5.5", "baz"])
100+
assert pex_req1.requirements == PexRequirements(["foo-bar>=0.1.2", "bar==5.5.5", "baz"])
101101

102102
pex_req2 = get_pex_request("constraints1.txt", ResolveAllConstraintsOption.ALWAYS)
103103
assert pex_req2.requirements == PexRequirements(
104-
["foo==1.0.0", "bar==5.5.5", "baz==2.2.2", "qux==3.4.5"]
104+
["Foo._-BAR==1.0.0", "bar==5.5.5", "baz==2.2.2", "qux==3.4.5"]
105105
)
106106

107107
with self.assertRaises(ExecutionError) as err:

0 commit comments

Comments
 (0)