Skip to content

Commit 6e96165

Browse files
committed
~
1 parent 7cab8d1 commit 6e96165

File tree

2 files changed

+40
-28
lines changed

2 files changed

+40
-28
lines changed

clang-tools-extra/clang-tidy/tool/check_alphabetical_order.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,43 @@
2525
"""
2626

2727
import argparse
28+
from collections import defaultdict
2829
import io
30+
from operator import itemgetter
2931
import os
3032
import re
3133
import sys
32-
from typing import List, Optional, Sequence, Tuple, NamedTuple, DefaultDict
33-
from collections import defaultdict
34-
from operator import itemgetter
34+
from typing import (
35+
List,
36+
Optional,
37+
Sequence,
38+
Tuple,
39+
NamedTuple,
40+
DefaultDict,
41+
Final,
42+
Pattern,
43+
)
3544

3645
# Matches a :doc:`label <path>` or :doc:`label` reference anywhere in text and
3746
# captures the label. Used to sort bullet items alphabetically in ReleaseNotes
3847
# items by their label.
39-
DOC_LABEL_RN_RE = re.compile(r":doc:`(?P<label>[^`<]+)\s*(?:<[^>]+>)?`")
48+
DOC_LABEL_RN_RE: Final[Pattern[str]] = re.compile(
49+
r":doc:`(?P<label>[^`<]+)\s*(?:<[^>]+>)?`"
50+
)
4051

4152
# Matches a single csv-table row line in list.rst that begins with a :doc:
4253
# reference, capturing the label. Used to extract the sort key per row.
43-
DOC_LINE_RE = re.compile(r"^\s*:doc:`(?P<label>[^`<]+?)\s*<[^>]+>`.*$")
54+
DOC_LINE_RE: Final[Pattern[str]] = re.compile(
55+
r"^\s*:doc:`(?P<label>[^`<]+?)\s*<[^>]+>`.*$"
56+
)
4457

4558

46-
EXTRA_DIR = os.path.join(os.path.dirname(__file__), "../..")
47-
DOCS_DIR = os.path.join(EXTRA_DIR, "docs")
48-
CLANG_TIDY_DOCS_DIR = os.path.join(DOCS_DIR, "clang-tidy")
49-
CHECKS_DOCS_DIR = os.path.join(CLANG_TIDY_DOCS_DIR, "checks")
50-
LIST_DOC = os.path.join(CHECKS_DOCS_DIR, "list.rst")
51-
RELEASE_NOTES_DOC = os.path.join(DOCS_DIR, "ReleaseNotes.rst")
59+
EXTRA_DIR: Final[str] = os.path.join(os.path.dirname(__file__), "../..")
60+
DOCS_DIR: Final[str] = os.path.join(EXTRA_DIR, "docs")
61+
CLANG_TIDY_DOCS_DIR: Final[str] = os.path.join(DOCS_DIR, "clang-tidy")
62+
CHECKS_DOCS_DIR: Final[str] = os.path.join(CLANG_TIDY_DOCS_DIR, "checks")
63+
LIST_DOC: Final[str] = os.path.join(CHECKS_DOCS_DIR, "list.rst")
64+
RELEASE_NOTES_DOC: Final[str] = os.path.join(DOCS_DIR, "ReleaseNotes.rst")
5265

5366

5467
# Label extracted from :doc:`...`.
@@ -133,7 +146,7 @@ def _normalize_list_rst_lines(lines: Sequence[str]) -> List[str]:
133146
i = 0
134147
n = len(lines)
135148

136-
def check_name(line: str):
149+
def check_name(line: str) -> Tuple[int, CheckLabel]:
137150
if m := DOC_LINE_RE.match(line):
138151
return (0, m.group("label"))
139152
return (1, "")
@@ -192,8 +205,9 @@ def find_heading(lines: Sequence[str], title: str) -> Optional[int]:
192205

193206

194207
def extract_label(text: str) -> str:
195-
m = DOC_LABEL_RN_RE.search(text)
196-
return m.group("label") if m else text
208+
if m := DOC_LABEL_RN_RE.search(text):
209+
return m.group("label")
210+
return text
197211

198212

199213
def _is_bullet_start(line: str) -> bool:

clang-tools-extra/clang-tidy/tool/check_alphabetical_order_test.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
# To run these tests:
66
# python3 check_alphabetical_order_test.py -v
77

8+
import check_alphabetical_order as _mod
9+
from contextlib import redirect_stderr
810
import io
911
import os
1012
import tempfile
11-
import unittest
12-
from contextlib import redirect_stderr
13-
from typing import cast
1413
import textwrap
15-
16-
17-
import check_alphabetical_order as _mod
14+
from typing import cast
15+
import unittest
1816

1917

2018
class TestAlphabeticalOrderCheck(unittest.TestCase):
21-
def test_normalize_list_rst_sorts_rows(self):
19+
def test_normalize_list_rst_sorts_rows(self) -> None:
2220
input_text = textwrap.dedent(
2321
"""\
2422
.. csv-table:: Clang-Tidy checks
@@ -46,7 +44,7 @@ def test_normalize_list_rst_sorts_rows(self):
4644
out_str = _mod.normalize_list_rst(input_text)
4745
self.assertEqual(out_str, expected_text)
4846

49-
def test_find_heading(self):
47+
def test_find_heading(self) -> None:
5048
text = textwrap.dedent(
5149
"""\
5250
- Deprecated the :program:`clang-tidy` ``zircon`` module. All checks have been
@@ -63,7 +61,7 @@ def test_find_heading(self):
6361
idx = _mod.find_heading(lines, "New checks")
6462
self.assertEqual(idx, 4)
6563

66-
def test_duplicate_detection_and_report(self):
64+
def test_duplicate_detection_and_report(self) -> None:
6765
# Ensure duplicate detection works properly when sorting is incorrect.
6866
text = textwrap.dedent(
6967
"""\
@@ -113,7 +111,7 @@ def test_duplicate_detection_and_report(self):
113111
)
114112
self.assertEqual(report_str, expected_report)
115113

116-
def test_process_release_notes_with_unsorted_content(self):
114+
def test_process_release_notes_with_unsorted_content(self) -> None:
117115
# When content is not normalized, the function writes normalized text and returns 0.
118116
rn_text = textwrap.dedent(
119117
"""\
@@ -168,7 +166,7 @@ def test_process_release_notes_with_unsorted_content(self):
168166
self.assertEqual(out, expected_out)
169167
self.assertIn("not normalized", buf.getvalue())
170168

171-
def test_process_release_notes_prioritizes_sorting_over_duplicates(self):
169+
def test_process_release_notes_prioritizes_sorting_over_duplicates(self) -> None:
172170
# Sorting is incorrect and duplicates exist, should report ordering issues first.
173171
rn_text = textwrap.dedent(
174172
"""\
@@ -234,7 +232,7 @@ def test_process_release_notes_prioritizes_sorting_over_duplicates(self):
234232
)
235233
self.assertEqual(out, expected_out)
236234

237-
def test_process_release_notes_with_duplicates_fails(self):
235+
def test_process_release_notes_with_duplicates_fails(self) -> None:
238236
# Sorting is already correct but duplicates exist, should return 3 and report.
239237
rn_text = textwrap.dedent(
240238
"""\
@@ -295,7 +293,7 @@ def test_process_release_notes_with_duplicates_fails(self):
295293
out = f.read()
296294
self.assertEqual(out, rn_text)
297295

298-
def test_release_notes_handles_nested_sub_bullets(self):
296+
def test_release_notes_handles_nested_sub_bullets(self) -> None:
299297
rn_text = textwrap.dedent(
300298
"""\
301299
Changes in existing checks
@@ -355,7 +353,7 @@ def test_release_notes_handles_nested_sub_bullets(self):
355353
)
356354
self.assertEqual(out, expected_out)
357355

358-
def test_process_checks_list_normalizes_output(self):
356+
def test_process_checks_list_normalizes_output(self) -> None:
359357
list_text = textwrap.dedent(
360358
"""\
361359
.. csv-table:: List

0 commit comments

Comments
 (0)