Skip to content

Commit b5b3f0b

Browse files
📝 Add docstrings to add-include-package-data
Docstrings generation was requested by @shenxianpeng. * #138 (comment) The following files were modified: * `cpp_linter_hooks/clang_format.py` * `cpp_linter_hooks/clang_tidy.py` * `cpp_linter_hooks/util.py`
1 parent 604c752 commit b5b3f0b

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

cpp_linter_hooks/clang_format.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414

1515

1616
def run_clang_format(args=None) -> Tuple[int, str]:
17+
"""
18+
Run clang-format with the given arguments and return its exit code and combined output.
19+
20+
Parses known hook options from `args`, optionally ensures a specific clang-format version is installed, builds and executes a clang-format command (modifying files in-place by default), and captures stdout and stderr merged into a single output string.
21+
22+
Parameters:
23+
args (Optional[Sequence[str]]): Argument list to parse (typically sys.argv[1:]). If omitted, uses parser defaults.
24+
25+
Returns:
26+
tuple[int, str]: A pair (retval, output) where `output` is the concatenation of stdout and stderr.
27+
`retval` is the subprocess return code, except:
28+
- `-1` when the command included `--dry-run` (special sentinel to indicate dry-run mode),
29+
- `1` when clang-format could not be found (FileNotFoundError converted to an exit-like code).
30+
"""
1731
hook_args, other_args = parser.parse_known_args(args)
1832
if hook_args.version:
1933
resolve_install("clang-format", hook_args.version)
@@ -73,4 +87,4 @@ def main() -> int:
7387

7488

7589
if __name__ == "__main__":
76-
raise SystemExit(main())
90+
raise SystemExit(main())

cpp_linter_hooks/clang_tidy.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010

1111

1212
def run_clang_tidy(args=None) -> Tuple[int, str]:
13+
"""
14+
Run clang-tidy with the given command-line arguments and return a status code and captured output.
15+
16+
Parameters:
17+
args (Optional[Sequence[str]]): Arguments to parse and forward to clang-tidy; if None, uses sys.argv. If the parsed arguments include a --version value, the specified clang-tidy version is ensured to be installed before running.
18+
19+
Returns:
20+
Tuple[int, str]: A pair (status, output).
21+
- status: 0 when clang-tidy executed and produced no warnings or errors; 1 when clang-tidy reports any "warning:" or "error:", or when clang-tidy cannot be executed.
22+
- output: Captured stdout from clang-tidy, or the error text if execution failed.
23+
"""
1324
hook_args, other_args = parser.parse_known_args(args)
1425
if hook_args.version:
1526
resolve_install("clang-tidy", hook_args.version)
@@ -37,4 +48,4 @@ def main() -> int:
3748

3849

3950
if __name__ == "__main__":
40-
raise SystemExit(main())
51+
raise SystemExit(main())

cpp_linter_hooks/util.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ def parse_version(v: str):
6060

6161

6262
def _install_tool(tool: str, version: str) -> Optional[Path]:
63-
"""Install a tool using pip, suppressing output."""
63+
"""
64+
Install the specified tool version into the current Python environment and return its executable path.
65+
66+
Parameters:
67+
tool (str): The package/executable name to install (e.g., "clang-format").
68+
version (str): The exact version string to install (e.g., "14.0.6").
69+
70+
Returns:
71+
Path: Path to the installed tool's executable if the installation succeeds and the executable is found, `None` otherwise.
72+
"""
6473
try:
6574
subprocess.check_call(
6675
[sys.executable, "-m", "pip", "install", f"{tool}=={version}"],
@@ -73,7 +82,16 @@ def _install_tool(tool: str, version: str) -> Optional[Path]:
7382

7483

7584
def resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
76-
"""Resolve the installation of a tool, checking for version and installing if necessary."""
85+
"""
86+
Resolve and install the requested clang tool version and return its executable path.
87+
88+
Parameters:
89+
tool (str): Tool name, expected "clang-format" or "clang-tidy".
90+
version (Optional[str]): Desired version string (exact match or prefix). If None, falls back to the default version from pyproject.toml when available.
91+
92+
Returns:
93+
Optional[Path]: Path to the installed tool executable if installation succeeded, `None` otherwise.
94+
"""
7795
user_version = _resolve_version(
7896
CLANG_FORMAT_VERSIONS if tool == "clang-format" else CLANG_TIDY_VERSIONS,
7997
version,
@@ -85,4 +103,4 @@ def resolve_install(tool: str, version: Optional[str]) -> Optional[Path]:
85103
else DEFAULT_CLANG_TIDY_VERSION
86104
)
87105

88-
return _install_tool(tool, user_version)
106+
return _install_tool(tool, user_version)

0 commit comments

Comments
 (0)