Skip to content

Commit 8f72751

Browse files
Review feedback
1 parent 8b5c223 commit 8f72751

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/rapids_pre_commit_hooks/pyproject_license.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,35 @@
2222

2323
RAPIDS_LICENSE = "Apache 2.0"
2424
ACCEPTABLE_LICENSES = {
25-
"Apache 2.0",
25+
RAPIDS_LICENSE,
2626
"BSD-3-Clause",
2727
}
2828

2929

30-
def find_value_location(document, key):
30+
def find_value_location(document, key, append):
3131
copied_document = copy.deepcopy(document)
3232
placeholder = uuid.uuid4()
3333

34+
# tomlkit does not provide "mark" information to determine where exactly in the
35+
# document a value is located, so instead we replace it with a placeholder and
36+
# look for that in the new document.
3437
node = copied_document
35-
while len(key) > 1:
38+
while len(key) > (0 if append else 1):
3639
node = node[key[0]]
3740
key = key[1:]
38-
old_value = node[key[0]]
39-
node[key[0]] = str(placeholder)
40-
41-
begin_loc = copied_document.as_string().find(
42-
tomlkit.string(str(placeholder)).as_string()
41+
if append:
42+
node.add(str(placeholder), tomlkit.string(str(placeholder)))
43+
else:
44+
old_value = node[key[0]]
45+
node[key[0]] = str(placeholder)
46+
47+
value_to_find = (
48+
f"{placeholder} = {tomlkit.string(str(placeholder)).as_string()}"
49+
if append
50+
else tomlkit.string(str(placeholder)).as_string()
4351
)
44-
end_loc = begin_loc + len(old_value.as_string())
52+
begin_loc = copied_document.as_string().find(value_to_find)
53+
end_loc = begin_loc + (0 if append else len(old_value.as_string()))
4554
return begin_loc, end_loc
4655

4756

@@ -73,16 +82,7 @@ def check_pyproject_license(linter, args):
7382
f"{{ text = {tomlkit.string(RAPIDS_LICENSE).as_string()} }}\n",
7483
)
7584
else:
76-
placeholder = uuid.uuid4()
77-
copied_document = copy.deepcopy(document)
78-
copied_document["project"].add(
79-
str(placeholder), tomlkit.string(str(placeholder))
80-
)
81-
index = copied_document.as_string().find(
82-
f"{placeholder} = {tomlkit.string(str(placeholder)).as_string()}"
83-
)
84-
85-
loc = (index, index)
85+
loc = find_value_location(document, ("project",), True)
8686
linter.add_warning(
8787
loc, f'add project.license with value {{ text = "{RAPIDS_LICENSE}" }}'
8888
).add_replacement(
@@ -93,7 +93,7 @@ def check_pyproject_license(linter, args):
9393
return
9494

9595
if license_value not in ACCEPTABLE_LICENSES:
96-
loc = find_value_location(document, ("project", "license", "text"))
96+
loc = find_value_location(document, ("project", "license", "text"), False)
9797
linter.add_warning(
9898
loc, f'license should be "{RAPIDS_LICENSE}"'
9999
).add_replacement(loc, tomlkit.string(RAPIDS_LICENSE).as_string())

test/rapids_pre_commit_hooks/test_pyproject_license.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,49 @@
2222

2323

2424
@pytest.mark.parametrize(
25-
["key", "loc"],
25+
["key", "append", "loc"],
2626
[
2727
(
2828
("table", "key1"),
29+
False,
2930
(15, 22),
3031
),
3132
(
3233
("table", "key2"),
34+
False,
3335
(30, 32),
3436
),
3537
(
3638
("table", "key3"),
39+
False,
3740
(40, 60),
3841
),
3942
(
4043
("table", "key3", "nested"),
44+
False,
4145
(51, 58),
4246
),
47+
(
48+
("table",),
49+
True,
50+
(61, 61),
51+
),
4352
],
4453
)
45-
def test_find_value_location(key, loc):
54+
def test_find_value_location(key, append, loc):
4655
CONTENT = dedent(
4756
"""\
4857
[table]
4958
key1 = "value"
5059
key2 = 42
5160
key3 = { nested = "value" }
61+
62+
[table2]
63+
key = "value"
5264
"""
5365
)
5466
parsed_doc = tomlkit.loads(CONTENT)
55-
assert pyproject_license.find_value_location(parsed_doc, key) == loc
67+
assert pyproject_license.find_value_location(parsed_doc, key, append) == loc
5668
assert parsed_doc.as_string() == CONTENT
5769

5870

0 commit comments

Comments
 (0)