Skip to content

Commit a1f7056

Browse files
Liam-DeVoehenryiii
andauthored
fix: support nested parens in license expressions (#931)
* support nested parens * format --------- Co-authored-by: Henry Schreiner <[email protected]>
1 parent 3f83dea commit a1f7056

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/packaging/licenses/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ def canonicalize_license_expression(
9090
python_tokens.append("False")
9191
elif token == "with":
9292
python_tokens.append("or")
93-
elif token == "(" and python_tokens and python_tokens[-1] not in {"or", "and"}:
93+
elif (
94+
token == "("
95+
and python_tokens
96+
and python_tokens[-1] not in {"or", "and", "("}
97+
):
9498
message = f"Invalid license expression: {raw_license_expression!r}"
9599
raise InvalidLicenseExpression(message)
96100
elif token == ")" and python_tokens and python_tokens[-1] == "(":

tests/test_metadata.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,12 @@ def test_optional_defaults_to_none(self, field_name):
698698
"(LicenseRef-Special-License OR licenseref-OtherLicense) OR unlicense",
699699
"(LicenseRef-Special-License OR LicenseRef-OtherLicense) OR Unlicense",
700700
),
701+
# we don't canonicalize redundant parens, instead leaving them as-is
702+
# in the license expression.
703+
("(MIT)", "(MIT)"),
704+
("((MIT))", "((MIT))"),
705+
("(( MIT ))", "((MIT))"),
706+
("((MIT AND (MIT)))", "((MIT AND (MIT)))"),
701707
],
702708
)
703709
def test_valid_license_expression(self, license_expression, expected):

0 commit comments

Comments
 (0)