Skip to content

Commit d80be26

Browse files
committed
Provide hints when an invalid license id is input
1 parent 39c57cc commit d80be26

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

poetry/spdx/__init__.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,36 @@ def license_by_id(identifier):
1717
id = identifier.lower()
1818

1919
if id not in _licenses:
20-
raise ValueError("Invalid license id: {}".format(identifier))
20+
err = "Invalid license id: {}\nPoetry uses SPDX license identifiers: https://spdx.org/licenses/".format(
21+
identifier
22+
)
23+
24+
# Covers the licenses listed as common for python packages in https://snyk.io/blog/over-10-of-python-packages-on-pypi-are-distributed-without-any-license/
25+
# MIT/WTFPL/Unlicense are excluded as their ids are simply their name - if someone types "mit", they've already found the license they were looking for
26+
27+
common_strings = ["agpl", "lgpl", "gpl", "bsd", "apache", "mpl", "cc0"]
28+
for str in common_strings:
29+
if str in id:
30+
31+
err += "\n"
32+
err += "Did you mean one of the following?"
33+
34+
matches = sorted(
35+
{
36+
license.id
37+
for license in _licenses.values()
38+
if license.id.lower().startswith(str)
39+
and not license.is_deprecated
40+
}
41+
)
42+
43+
for license in matches:
44+
err += "\n * {}".format(license)
45+
46+
# Don't match agpl for "gpl"
47+
break
48+
49+
raise ValueError(err)
2150

2251
return _licenses[id]
2352

tests/spdx/test_main.py

+35
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,38 @@ def test_license_by_id_with_full_name():
4141
def test_license_by_id_invalid():
4242
with pytest.raises(ValueError):
4343
license_by_id("invalid")
44+
45+
46+
def test_license_by_id_invalid_gpl():
47+
with pytest.raises(ValueError) as exc_info:
48+
license_by_id("gpl")
49+
50+
assert "Did you mean" in str(exc_info.value)
51+
assert " GPL-3.0-only" in str(exc_info.value)
52+
assert " AGPL-3.0-only" not in str(exc_info.value)
53+
54+
55+
def test_license_by_id_invalid_agpl():
56+
with pytest.raises(ValueError) as exc_info:
57+
license_by_id("agpl")
58+
59+
assert "Did you mean" in str(exc_info.value)
60+
assert " GPL-3.0-only" not in str(exc_info.value)
61+
assert " AGPL-3.0-only" in str(exc_info.value)
62+
63+
64+
def test_license_by_id_invalid_agpl_versioned():
65+
with pytest.raises(ValueError) as exc_info:
66+
license_by_id("gnu agpl v3+")
67+
68+
assert "Did you mean" in str(exc_info.value)
69+
assert " GPL-3.0-only" not in str(exc_info.value)
70+
assert " AGPL-3.0-only" in str(exc_info.value)
71+
72+
73+
def test_license_by_id_invalid_unpopular():
74+
with pytest.raises(ValueError) as exc_info:
75+
license_by_id("not-a-well-known-license")
76+
77+
assert "spdx.org" in str(exc_info.value)
78+
assert "Did you mean" not in str(exc_info.value)

0 commit comments

Comments
 (0)