Skip to content

Commit 1c09ddf

Browse files
authored
fix: normalize all extras, not just the first one (#1024)
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
1 parent 8a805e3 commit 1c09ddf

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

src/packaging/markers.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,28 @@ class Environment(TypedDict):
122122
"""
123123

124124

125+
def _normalize_extras(
126+
result: MarkerList | MarkerAtom | str,
127+
) -> MarkerList | MarkerAtom | str:
128+
if not isinstance(result, tuple):
129+
return result
130+
131+
lhs, op, rhs = result
132+
if isinstance(lhs, Variable) and lhs.value == "extra":
133+
normalized_extra = canonicalize_name(rhs.value)
134+
rhs = Value(normalized_extra)
135+
elif isinstance(rhs, Variable) and rhs.value == "extra":
136+
normalized_extra = canonicalize_name(lhs.value)
137+
lhs = Value(normalized_extra)
138+
return lhs, op, rhs
139+
140+
125141
def _normalize_extra_values(results: MarkerList) -> MarkerList:
126142
"""
127143
Normalize extra values.
128144
"""
129-
if isinstance(results[0], tuple):
130-
lhs, op, rhs = results[0]
131-
if isinstance(lhs, Variable) and lhs.value == "extra":
132-
normalized_extra = canonicalize_name(rhs.value)
133-
rhs = Value(normalized_extra)
134-
elif isinstance(rhs, Variable) and rhs.value == "extra":
135-
normalized_extra = canonicalize_name(lhs.value)
136-
lhs = Value(normalized_extra)
137-
results[0] = lhs, op, rhs
138-
return results
145+
146+
return [_normalize_extras(r) for r in results]
139147

140148

141149
def _format_marker(

tests/test_requirements.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ def test_basic_valid_requirement_parsing(
145145
assert req.marker == (Marker(marker.format(ws="")) if marker else None)
146146

147147

148+
@pytest.mark.parametrize(
149+
("input_req", "norm_req"),
150+
[
151+
(
152+
'mariadb>=1.0.1; extra == "mariadb_connector"',
153+
'mariadb>=1.0.1; extra == "mariadb-connector"',
154+
),
155+
(
156+
'mariadb>=1.0.1; python_version >= "3" and extra == "mariadb_connector"',
157+
'mariadb>=1.0.1; python_version >= "3" and extra == "mariadb-connector"',
158+
),
159+
],
160+
)
161+
def test_normalized_requirements(input_req: str, norm_req: str) -> None:
162+
req = Requirement(input_req)
163+
assert str(req) == norm_req
164+
165+
148166
class TestRequirementParsing:
149167
@pytest.mark.parametrize(
150168
"marker",

0 commit comments

Comments
 (0)