docstring-format-checker 0.3.0__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/PKG-INFO +1 -1
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/pyproject.toml +1 -1
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/__init__.py +1 -1
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/cli.py +241 -309
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/core.py +8 -17
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/README.md +0 -0
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/config.py +0 -0
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/utils/__init__.py +0 -0
- {docstring_format_checker-0.3.0 → docstring_format_checker-0.5.0}/src/docstring_format_checker/utils/exceptions.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: docstring-format-checker
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: A CLI tool to check and validate Python docstring formatting and completeness
|
|
5
5
|
Author: Chris Mahoney
|
|
6
6
|
Author-email: Chris Mahoney <docstring-format-checker@data-science-extensions.com>
|
|
@@ -4,7 +4,7 @@ Docstring Format Checker.
|
|
|
4
4
|
A CLI tool to check and validate Python docstring formatting and completeness.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
__version__ = "v0.
|
|
7
|
+
__version__ = "v0.5.0"
|
|
8
8
|
__author__ = "Chris Mahoney"
|
|
9
9
|
__email__ = "docstring-format-checker@data-science-extensions.com"
|
|
10
10
|
|
|
@@ -43,25 +43,16 @@
|
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
# ## Python StdLib Imports ----
|
|
46
|
+
from functools import partial
|
|
46
47
|
from pathlib import Path
|
|
47
48
|
from textwrap import dedent
|
|
48
|
-
from typing import Optional
|
|
49
|
+
from typing import Optional
|
|
49
50
|
|
|
50
51
|
# ## Python Third Party Imports ----
|
|
51
52
|
from rich.console import Console
|
|
52
53
|
from rich.panel import Panel
|
|
53
54
|
from rich.table import Table
|
|
54
|
-
from
|
|
55
|
-
from typer import (
|
|
56
|
-
Argument,
|
|
57
|
-
BadParameter,
|
|
58
|
-
CallbackParam,
|
|
59
|
-
Context,
|
|
60
|
-
Exit,
|
|
61
|
-
Option,
|
|
62
|
-
Typer,
|
|
63
|
-
echo,
|
|
64
|
-
)
|
|
55
|
+
from typer import Argument, CallbackParam, Context, Exit, Option, Typer, echo
|
|
65
56
|
|
|
66
57
|
# ## Local First Party Imports ----
|
|
67
58
|
from docstring_format_checker import __version__
|
|
@@ -76,8 +67,6 @@ from docstring_format_checker.core import DocstringChecker, DocstringError
|
|
|
76
67
|
|
|
77
68
|
__all__: list[str] = [
|
|
78
69
|
"main",
|
|
79
|
-
"config_example",
|
|
80
|
-
"check",
|
|
81
70
|
"entry_point",
|
|
82
71
|
]
|
|
83
72
|
|
|
@@ -90,6 +79,22 @@ __all__: list[str] = [
|
|
|
90
79
|
NEW_LINE = "\n"
|
|
91
80
|
|
|
92
81
|
|
|
82
|
+
## --------------------------------------------------------------------------- #
|
|
83
|
+
## Helpers ####
|
|
84
|
+
## --------------------------------------------------------------------------- #
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
### Colours ----
|
|
88
|
+
def _colour(text: str, colour: str) -> str:
|
|
89
|
+
return f"[{colour}]{text}[/{colour}]"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
_green = partial(_colour, colour="green")
|
|
93
|
+
_red = partial(_colour, colour="red")
|
|
94
|
+
_cyan = partial(_colour, colour="cyan")
|
|
95
|
+
_blue = partial(_colour, colour="blue")
|
|
96
|
+
|
|
97
|
+
|
|
93
98
|
# ---------------------------------------------------------------------------- #
|
|
94
99
|
# #
|
|
95
100
|
# Main Application ####
|
|
@@ -136,7 +141,7 @@ def _version_callback(ctx: Context, param: CallbackParam, value: bool) -> None:
|
|
|
136
141
|
raise Exit()
|
|
137
142
|
|
|
138
143
|
|
|
139
|
-
def
|
|
144
|
+
def _help_callback_main(ctx: Context, param: CallbackParam, value: bool) -> None:
|
|
140
145
|
"""
|
|
141
146
|
!!! note "Summary"
|
|
142
147
|
Show help and exit.
|
|
@@ -159,10 +164,10 @@ def _help_callback(ctx: Context, param: CallbackParam, value: bool) -> None:
|
|
|
159
164
|
raise Exit()
|
|
160
165
|
|
|
161
166
|
|
|
162
|
-
def
|
|
167
|
+
def _example_callback(ctx: Context, param: CallbackParam, value: Optional[str]) -> None:
|
|
163
168
|
"""
|
|
164
169
|
!!! note "Summary"
|
|
165
|
-
|
|
170
|
+
Handle example flag and show appropriate example content.
|
|
166
171
|
|
|
167
172
|
Params:
|
|
168
173
|
ctx (Context):
|
|
@@ -170,51 +175,26 @@ def _parse_boolean_flag(ctx: Context, param: CallbackParam, value: Optional[str]
|
|
|
170
175
|
param (CallbackParam):
|
|
171
176
|
The parameter object.
|
|
172
177
|
value (Optional[str]):
|
|
173
|
-
The
|
|
178
|
+
The example type to show: 'config' or 'usage'.
|
|
174
179
|
|
|
175
180
|
Returns:
|
|
176
|
-
(
|
|
177
|
-
|
|
178
|
-
"""
|
|
179
|
-
|
|
180
|
-
# Handle the case where the flag is provided without a value (e.g., just --recursive or -r)
|
|
181
|
-
# In this case, Typer doesn't call the callback, so we need to handle it differently
|
|
182
|
-
if value is None:
|
|
183
|
-
# This means the flag wasn't provided at all, use default
|
|
184
|
-
return True
|
|
185
|
-
|
|
186
|
-
# If value is an empty string, it means the flag was provided without a value
|
|
187
|
-
if value == "":
|
|
188
|
-
return True
|
|
189
|
-
|
|
190
|
-
try:
|
|
191
|
-
return strtobool(value)
|
|
192
|
-
except ValueError as e:
|
|
193
|
-
raise BadParameter(
|
|
194
|
-
message=(
|
|
195
|
-
f"Invalid boolean value: '{value}'.{NEW_LINE}"
|
|
196
|
-
"Use one of: true/false, t/f, yes/no, y/n, 1/0, or on/off."
|
|
197
|
-
)
|
|
198
|
-
) from e
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
def _parse_recursive_flag(value: str) -> bool:
|
|
181
|
+
(None):
|
|
182
|
+
Nothing is returned.
|
|
202
183
|
"""
|
|
203
|
-
!!! note "Summary"
|
|
204
|
-
Parse recursive flag using `strtobool()` utility.
|
|
205
184
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
The string value of the flag.
|
|
185
|
+
if not value or ctx.resilient_parsing:
|
|
186
|
+
return
|
|
209
187
|
|
|
210
|
-
|
|
211
|
-
(
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
188
|
+
if value == "config":
|
|
189
|
+
_show_config_example_callback()
|
|
190
|
+
elif value == "usage":
|
|
191
|
+
_show_usage_examples_callback()
|
|
192
|
+
else:
|
|
193
|
+
console.print(_red(f"Error: Invalid example type '{value}'. Use 'config' or 'usage'."))
|
|
194
|
+
raise Exit(1)
|
|
215
195
|
|
|
216
196
|
|
|
217
|
-
def
|
|
197
|
+
def _show_usage_examples_callback() -> None:
|
|
218
198
|
"""
|
|
219
199
|
!!! note "Summary"
|
|
220
200
|
Show examples and exit.
|
|
@@ -232,17 +212,19 @@ def _show_examples_callback(ctx: Context, param: CallbackParam, value: bool) ->
|
|
|
232
212
|
Nothing is returned.
|
|
233
213
|
"""
|
|
234
214
|
|
|
235
|
-
if not value or ctx.resilient_parsing:
|
|
236
|
-
return
|
|
237
|
-
|
|
238
215
|
examples_content: str = dedent(
|
|
239
|
-
"""
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
216
|
+
f"""
|
|
217
|
+
{_green("dfc myfile.py")} Check a single Python file (list output)
|
|
218
|
+
{_green("dfc src/")} Check all Python files in src/ directory
|
|
219
|
+
{_green("dfc --output=table myfile.py")} Check with table output format
|
|
220
|
+
{_green("dfc -o list myfile.py")} Check with list output format (default)
|
|
221
|
+
{_green("dfc --check myfile.py")} Check and exit with error if issues found
|
|
222
|
+
{_green("dfc --quiet myfile.py")} Check quietly, only show pass/fail
|
|
223
|
+
{_green("dfc --quiet --check myfile.py")} Check quietly and exit with error if issues found
|
|
224
|
+
{_green("dfc . --exclude '*/tests/*'")} Check current directory, excluding tests
|
|
225
|
+
{_green("dfc . -c custom.toml")} Use custom configuration file
|
|
226
|
+
{_green("dfc --example=config")} Show example configuration
|
|
227
|
+
{_green("dfc -e usage")} Show usage examples (this help)
|
|
246
228
|
"""
|
|
247
229
|
).strip()
|
|
248
230
|
|
|
@@ -258,10 +240,10 @@ def _show_examples_callback(ctx: Context, param: CallbackParam, value: bool) ->
|
|
|
258
240
|
raise Exit()
|
|
259
241
|
|
|
260
242
|
|
|
261
|
-
def
|
|
243
|
+
def _show_config_example_callback() -> None:
|
|
262
244
|
"""
|
|
263
245
|
!!! note "Summary"
|
|
264
|
-
Show
|
|
246
|
+
Show configuration example and exit.
|
|
265
247
|
|
|
266
248
|
Params:
|
|
267
249
|
ctx (Context):
|
|
@@ -276,33 +258,101 @@ def _show_check_examples_callback(ctx: Context, param: CallbackParam, value: boo
|
|
|
276
258
|
Nothing is returned.
|
|
277
259
|
"""
|
|
278
260
|
|
|
279
|
-
|
|
280
|
-
return
|
|
281
|
-
|
|
282
|
-
examples_content: str = dedent(
|
|
283
|
-
"""
|
|
284
|
-
[green]dfc check myfile.py[/green] Check a single Python file
|
|
285
|
-
[green]dfc check src/[/green] Check all Python files in src/ directory
|
|
286
|
-
[green]dfc check . --exclude "*/tests/*"[/green] Check current directory, excluding tests
|
|
287
|
-
[green]dfc check . --config custom.toml[/green] Use custom configuration file
|
|
288
|
-
[green]dfc check . --verbose --recursive[/green] Show detailed output for all subdirectories
|
|
289
|
-
[green]dfc check . --quiet[/green] Only show errors, suppress success messages
|
|
261
|
+
example_config: str = dedent(
|
|
290
262
|
"""
|
|
291
|
-
|
|
263
|
+
# Example configuration for docstring-format-checker
|
|
264
|
+
# Place this in your pyproject.toml file
|
|
292
265
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
title="Check Command Examples",
|
|
296
|
-
title_align="left",
|
|
297
|
-
border_style="dim",
|
|
298
|
-
padding=(0, 1),
|
|
299
|
-
)
|
|
266
|
+
[tool.dfc]
|
|
267
|
+
# or [tool.docstring-format-checker]
|
|
300
268
|
|
|
301
|
-
|
|
269
|
+
[[tool.dfc.sections]]
|
|
270
|
+
order = 1
|
|
271
|
+
name = "summary"
|
|
272
|
+
type = "free_text"
|
|
273
|
+
admonition = "note"
|
|
274
|
+
prefix = "!!!"
|
|
275
|
+
required = true
|
|
276
|
+
|
|
277
|
+
[[tool.dfc.sections]]
|
|
278
|
+
order = 2
|
|
279
|
+
name = "details"
|
|
280
|
+
type = "free_text"
|
|
281
|
+
admonition = "info"
|
|
282
|
+
prefix = "???+"
|
|
283
|
+
required = false
|
|
284
|
+
|
|
285
|
+
[[tool.dfc.sections]]
|
|
286
|
+
order = 3
|
|
287
|
+
name = "params"
|
|
288
|
+
type = "list_name_and_type"
|
|
289
|
+
required = true
|
|
290
|
+
|
|
291
|
+
[[tool.dfc.sections]]
|
|
292
|
+
order = 4
|
|
293
|
+
name = "returns"
|
|
294
|
+
type = "list_name_and_type"
|
|
295
|
+
required = false
|
|
296
|
+
|
|
297
|
+
[[tool.dfc.sections]]
|
|
298
|
+
order = 5
|
|
299
|
+
name = "yields"
|
|
300
|
+
type = "list_type"
|
|
301
|
+
required = false
|
|
302
|
+
|
|
303
|
+
[[tool.dfc.sections]]
|
|
304
|
+
order = 6
|
|
305
|
+
name = "raises"
|
|
306
|
+
type = "list_type"
|
|
307
|
+
required = false
|
|
308
|
+
|
|
309
|
+
[[tool.dfc.sections]]
|
|
310
|
+
order = 7
|
|
311
|
+
name = "examples"
|
|
312
|
+
type = "free_text"
|
|
313
|
+
admonition = "example"
|
|
314
|
+
prefix = "???+"
|
|
315
|
+
required = false
|
|
316
|
+
|
|
317
|
+
[[tool.dfc.sections]]
|
|
318
|
+
order = 8
|
|
319
|
+
name = "notes"
|
|
320
|
+
type = "free_text"
|
|
321
|
+
admonition = "note"
|
|
322
|
+
prefix = "???"
|
|
323
|
+
required = false
|
|
324
|
+
"""
|
|
325
|
+
).strip()
|
|
326
|
+
|
|
327
|
+
# Print without Rich markup processing to avoid bracket interpretation
|
|
328
|
+
console.print(example_config, markup=False)
|
|
302
329
|
raise Exit()
|
|
303
330
|
|
|
304
331
|
|
|
305
|
-
def
|
|
332
|
+
def _format_error_messages(error_message: str) -> str:
|
|
333
|
+
"""
|
|
334
|
+
!!! note "Summary"
|
|
335
|
+
Format error messages for better readability in CLI output.
|
|
336
|
+
|
|
337
|
+
Params:
|
|
338
|
+
error_message (str):
|
|
339
|
+
The raw error message that may contain semicolon-separated errors
|
|
340
|
+
|
|
341
|
+
Returns:
|
|
342
|
+
(str):
|
|
343
|
+
Formatted error message with each error prefixed with "- " and separated by ";\n"
|
|
344
|
+
"""
|
|
345
|
+
if "; " in error_message:
|
|
346
|
+
# Split by semicolon and rejoin with proper formatting
|
|
347
|
+
errors: list[str] = error_message.split("; ")
|
|
348
|
+
formatted_errors: list[str] = [f"- {error.strip()}" for error in errors if error.strip()]
|
|
349
|
+
return ";\n".join(formatted_errors) + "."
|
|
350
|
+
else:
|
|
351
|
+
# Single error message
|
|
352
|
+
return f"- {error_message.strip()}."
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
def _display_results(results: dict[str, list[DocstringError]], quiet: bool, output: str, check: bool) -> int:
|
|
306
356
|
"""
|
|
307
357
|
!!! note "Summary"
|
|
308
358
|
Display the results of docstring checking.
|
|
@@ -311,9 +361,11 @@ def _display_results(results: dict[str, list[DocstringError]], quiet: bool, verb
|
|
|
311
361
|
results (dict[str, list[DocstringError]]):
|
|
312
362
|
Dictionary mapping file paths to lists of errors
|
|
313
363
|
quiet (bool):
|
|
314
|
-
Whether to suppress success messages
|
|
315
|
-
|
|
316
|
-
|
|
364
|
+
Whether to suppress success messages and error details
|
|
365
|
+
output (str):
|
|
366
|
+
Output format: 'table' or 'list'
|
|
367
|
+
check (bool):
|
|
368
|
+
Whether this is a check run (affects quiet behavior)
|
|
317
369
|
|
|
318
370
|
Returns:
|
|
319
371
|
(int):
|
|
@@ -321,14 +373,19 @@ def _display_results(results: dict[str, list[DocstringError]], quiet: bool, verb
|
|
|
321
373
|
"""
|
|
322
374
|
if not results:
|
|
323
375
|
if not quiet:
|
|
324
|
-
console.print("
|
|
376
|
+
console.print(_green("✓ All docstrings are valid!"))
|
|
325
377
|
return 0
|
|
326
378
|
|
|
327
379
|
# Count total errors
|
|
328
380
|
total_errors: int = sum(len(errors) for errors in results.values())
|
|
329
381
|
total_files: int = len(results)
|
|
330
382
|
|
|
331
|
-
if
|
|
383
|
+
if quiet:
|
|
384
|
+
# In quiet mode, only show summary
|
|
385
|
+
console.print(_red(f"{NEW_LINE}Found {total_errors} error(s) in {total_files} file(s)"))
|
|
386
|
+
return 1
|
|
387
|
+
|
|
388
|
+
if output == "table":
|
|
332
389
|
# Show detailed table
|
|
333
390
|
table = Table(show_header=True, header_style="bold magenta")
|
|
334
391
|
table.add_column("File", style="cyan", no_wrap=False)
|
|
@@ -340,29 +397,36 @@ def _display_results(results: dict[str, list[DocstringError]], quiet: bool, verb
|
|
|
340
397
|
for file_path, errors in results.items():
|
|
341
398
|
for i, error in enumerate(errors):
|
|
342
399
|
file_display: str = file_path if i == 0 else ""
|
|
400
|
+
|
|
401
|
+
# Format error message with improved formatting
|
|
402
|
+
formatted_error_message: str = _format_error_messages(error.message)
|
|
403
|
+
|
|
343
404
|
table.add_row(
|
|
344
405
|
file_display,
|
|
345
406
|
str(error.line_number) if error.line_number > 0 else "",
|
|
346
407
|
error.item_name,
|
|
347
408
|
error.item_type,
|
|
348
|
-
|
|
409
|
+
formatted_error_message,
|
|
349
410
|
)
|
|
350
411
|
console.print(table)
|
|
351
412
|
|
|
352
413
|
else:
|
|
353
414
|
# Show compact output
|
|
354
415
|
for file_path, errors in results.items():
|
|
355
|
-
console.print(f"{NEW_LINE}
|
|
416
|
+
console.print(f"{NEW_LINE}{_cyan(file_path)}")
|
|
356
417
|
for error in errors:
|
|
418
|
+
# Format error message with improved formatting
|
|
419
|
+
formatted_error_message: str = _format_error_messages(error.message)
|
|
420
|
+
|
|
357
421
|
if error.line_number > 0:
|
|
358
422
|
console.print(
|
|
359
|
-
f" [red]Line {error.line_number}[/red] - {error.item_type} '{error.item_name}': {
|
|
423
|
+
f" [red]Line {error.line_number}[/red] - {error.item_type} '{error.item_name}': {formatted_error_message}"
|
|
360
424
|
)
|
|
361
425
|
else:
|
|
362
|
-
console.print(f"
|
|
426
|
+
console.print(f" {_red('Error')}: {formatted_error_message}")
|
|
363
427
|
|
|
364
428
|
# Summary
|
|
365
|
-
console.print(f"{NEW_LINE}
|
|
429
|
+
console.print(_red(f"{NEW_LINE}Found {total_errors} error(s) in {total_files} file(s)"))
|
|
366
430
|
|
|
367
431
|
return 1
|
|
368
432
|
|
|
@@ -378,10 +442,10 @@ def _display_results(results: dict[str, list[DocstringError]], quiet: bool, verb
|
|
|
378
442
|
def _check_docstrings(
|
|
379
443
|
path: str,
|
|
380
444
|
config: Optional[str] = None,
|
|
381
|
-
recursive: bool = True,
|
|
382
445
|
exclude: Optional[list[str]] = None,
|
|
383
446
|
quiet: bool = False,
|
|
384
|
-
|
|
447
|
+
output: str = "list",
|
|
448
|
+
check: bool = False,
|
|
385
449
|
) -> None:
|
|
386
450
|
"""
|
|
387
451
|
!!! note "Summary"
|
|
@@ -392,14 +456,14 @@ def _check_docstrings(
|
|
|
392
456
|
The path to the file or directory to check.
|
|
393
457
|
config (Optional[str]):
|
|
394
458
|
The path to the configuration file.
|
|
395
|
-
recursive (bool):
|
|
396
|
-
Whether to check files recursively.
|
|
397
459
|
exclude (Optional[list[str]]):
|
|
398
460
|
List of glob patterns to exclude from checking.
|
|
399
461
|
quiet (bool):
|
|
400
462
|
Whether to suppress output.
|
|
401
|
-
|
|
402
|
-
|
|
463
|
+
output (str):
|
|
464
|
+
Output format: 'table' or 'list'.
|
|
465
|
+
check (bool):
|
|
466
|
+
Whether to throw error if issues are found.
|
|
403
467
|
|
|
404
468
|
Returns:
|
|
405
469
|
(None):
|
|
@@ -410,7 +474,7 @@ def _check_docstrings(
|
|
|
410
474
|
|
|
411
475
|
# Validate target path
|
|
412
476
|
if not target_path.exists():
|
|
413
|
-
console.print(f"
|
|
477
|
+
console.print(_red(f"Error: Path does not exist: '{path}'"))
|
|
414
478
|
raise Exit(1)
|
|
415
479
|
|
|
416
480
|
# Load configuration
|
|
@@ -418,25 +482,19 @@ def _check_docstrings(
|
|
|
418
482
|
if config:
|
|
419
483
|
config_path = Path(config)
|
|
420
484
|
if not config_path.exists():
|
|
421
|
-
console.print(f"
|
|
485
|
+
console.print(_red(f"Error: Configuration file does not exist: {config}"))
|
|
422
486
|
raise Exit(1)
|
|
423
487
|
sections_config = load_config(config_path)
|
|
424
488
|
else:
|
|
425
489
|
# Try to find config file automatically
|
|
426
|
-
found_config:
|
|
427
|
-
target_path if target_path.is_dir() else target_path.parent
|
|
428
|
-
)
|
|
490
|
+
found_config: Optional[Path] = find_config_file(target_path if target_path.is_dir() else target_path.parent)
|
|
429
491
|
if found_config:
|
|
430
|
-
if verbose:
|
|
431
|
-
console.print(f"[blue]Using configuration from: {found_config}[/blue]")
|
|
432
492
|
sections_config: list[SectionConfig] = load_config(found_config)
|
|
433
493
|
else:
|
|
434
|
-
if verbose:
|
|
435
|
-
console.print("[blue]Using default configuration[/blue]")
|
|
436
494
|
sections_config: list[SectionConfig] = load_config()
|
|
437
495
|
|
|
438
496
|
except Exception as e:
|
|
439
|
-
console.print(f"
|
|
497
|
+
console.print(_red(f"Error loading configuration: {e}"))
|
|
440
498
|
raise Exit(1)
|
|
441
499
|
|
|
442
500
|
# Initialize checker
|
|
@@ -445,23 +503,18 @@ def _check_docstrings(
|
|
|
445
503
|
# Check files
|
|
446
504
|
try:
|
|
447
505
|
if target_path.is_file():
|
|
448
|
-
if verbose:
|
|
449
|
-
console.print(f"[blue]Checking file: {target_path}[/blue]")
|
|
450
506
|
errors: list[DocstringError] = checker.check_file(target_path)
|
|
451
507
|
results: dict[str, list[DocstringError]] = {str(target_path): errors} if errors else {}
|
|
452
508
|
else:
|
|
453
|
-
|
|
454
|
-
console.print(f"[blue]Checking directory: {target_path} (recursive={recursive})[/blue]")
|
|
455
|
-
results: dict[str, list[DocstringError]] = checker.check_directory(
|
|
456
|
-
target_path, recursive=recursive, exclude_patterns=exclude
|
|
457
|
-
)
|
|
509
|
+
results: dict[str, list[DocstringError]] = checker.check_directory(target_path, exclude_patterns=exclude)
|
|
458
510
|
except Exception as e:
|
|
459
|
-
console.print(f"
|
|
511
|
+
console.print(_red(f"Error during checking: {e}"))
|
|
460
512
|
raise Exit(1)
|
|
461
513
|
|
|
462
514
|
# Display results
|
|
463
|
-
exit_code: int = _display_results(results, quiet,
|
|
515
|
+
exit_code: int = _display_results(results, quiet, output, check)
|
|
464
516
|
|
|
517
|
+
# Always exit with error code if issues are found, regardless of check flag
|
|
465
518
|
if exit_code != 0:
|
|
466
519
|
raise Exit(exit_code)
|
|
467
520
|
|
|
@@ -477,6 +530,41 @@ def _check_docstrings(
|
|
|
477
530
|
@app.callback(invoke_without_command=True)
|
|
478
531
|
def main(
|
|
479
532
|
ctx: Context,
|
|
533
|
+
path: Optional[str] = Argument(None, help="Path to Python file or directory to check"),
|
|
534
|
+
config: Optional[str] = Option(None, "--config", "-f", help="Path to configuration file (TOML format)"),
|
|
535
|
+
exclude: Optional[list[str]] = Option(
|
|
536
|
+
None,
|
|
537
|
+
"--exclude",
|
|
538
|
+
"-x",
|
|
539
|
+
help="Glob patterns to exclude (can be used multiple times)",
|
|
540
|
+
),
|
|
541
|
+
output: str = Option(
|
|
542
|
+
"list",
|
|
543
|
+
"--output",
|
|
544
|
+
"-o",
|
|
545
|
+
help="Output format: 'table' or 'list'",
|
|
546
|
+
show_default=True,
|
|
547
|
+
),
|
|
548
|
+
check: bool = Option(
|
|
549
|
+
False,
|
|
550
|
+
"--check",
|
|
551
|
+
"-c",
|
|
552
|
+
help="Throw error (exit 1) if any issues are found",
|
|
553
|
+
),
|
|
554
|
+
quiet: bool = Option(
|
|
555
|
+
False,
|
|
556
|
+
"--quiet",
|
|
557
|
+
"-q",
|
|
558
|
+
help="Only output pass/fail confirmation, suppress errors unless failing",
|
|
559
|
+
),
|
|
560
|
+
example: Optional[str] = Option(
|
|
561
|
+
None,
|
|
562
|
+
"--example",
|
|
563
|
+
"-e",
|
|
564
|
+
callback=_example_callback,
|
|
565
|
+
is_eager=True,
|
|
566
|
+
help="Show examples: 'config' for configuration example, 'usage' for usage examples",
|
|
567
|
+
),
|
|
480
568
|
version: Optional[bool] = Option(
|
|
481
569
|
None,
|
|
482
570
|
"--version",
|
|
@@ -485,19 +573,11 @@ def main(
|
|
|
485
573
|
is_eager=True,
|
|
486
574
|
help="Show version and exit",
|
|
487
575
|
),
|
|
488
|
-
examples: Optional[bool] = Option(
|
|
489
|
-
None,
|
|
490
|
-
"--examples",
|
|
491
|
-
"-e",
|
|
492
|
-
callback=_show_examples_callback,
|
|
493
|
-
is_eager=True,
|
|
494
|
-
help="Show usage examples and exit",
|
|
495
|
-
),
|
|
496
576
|
help_flag: Optional[bool] = Option(
|
|
497
577
|
None,
|
|
498
578
|
"--help",
|
|
499
579
|
"-h",
|
|
500
|
-
callback=
|
|
580
|
+
callback=_help_callback_main,
|
|
501
581
|
is_eager=True,
|
|
502
582
|
help="Show this message and exit",
|
|
503
583
|
),
|
|
@@ -512,122 +592,22 @@ def main(
|
|
|
512
592
|
Params:
|
|
513
593
|
ctx (Context):
|
|
514
594
|
The context object for the command.
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
examples (Optional[bool]):
|
|
518
|
-
Show usage examples and exit.
|
|
519
|
-
help_flag (Optional[bool]):
|
|
520
|
-
Show help message and exit.
|
|
521
|
-
|
|
522
|
-
Returns:
|
|
523
|
-
(None):
|
|
524
|
-
Nothing is returned.
|
|
525
|
-
"""
|
|
526
|
-
# If no subcommand is provided, show help
|
|
527
|
-
if ctx.invoked_subcommand is None:
|
|
528
|
-
echo(ctx.get_help())
|
|
529
|
-
raise Exit()
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
@app.command(
|
|
533
|
-
rich_help_panel="Commands",
|
|
534
|
-
add_help_option=False, # Disable automatic help so we can add our own with -h
|
|
535
|
-
)
|
|
536
|
-
def check(
|
|
537
|
-
path: str = Argument(..., help="Path to Python file or directory to check"),
|
|
538
|
-
config: Optional[str] = Option(None, "--config", "-c", help="Path to configuration file (TOML format)"),
|
|
539
|
-
recursive: str = Option(
|
|
540
|
-
"true",
|
|
541
|
-
"--recursive",
|
|
542
|
-
"-r",
|
|
543
|
-
help="Check directories recursively (default: true). Accepts: true/false, t/f, yes/no, y/n, 1/0, on/off",
|
|
544
|
-
),
|
|
545
|
-
exclude: Optional[list[str]] = Option(
|
|
546
|
-
None,
|
|
547
|
-
"--exclude",
|
|
548
|
-
"-x",
|
|
549
|
-
help="Glob patterns to exclude (can be used multiple times)",
|
|
550
|
-
),
|
|
551
|
-
quiet: bool = Option(False, "--quiet", "-q", help="Only show errors, no success messages"),
|
|
552
|
-
verbose: bool = Option(False, "--verbose", "-n", help="Show detailed output"),
|
|
553
|
-
examples: Optional[bool] = Option(
|
|
554
|
-
None,
|
|
555
|
-
"--examples",
|
|
556
|
-
"-e",
|
|
557
|
-
callback=_show_check_examples_callback,
|
|
558
|
-
is_eager=True,
|
|
559
|
-
help="Show usage examples and exit",
|
|
560
|
-
),
|
|
561
|
-
help_flag: Optional[bool] = Option(
|
|
562
|
-
None,
|
|
563
|
-
"--help",
|
|
564
|
-
"-h",
|
|
565
|
-
callback=_help_callback,
|
|
566
|
-
is_eager=True,
|
|
567
|
-
help="Show this message and exit",
|
|
568
|
-
),
|
|
569
|
-
) -> None:
|
|
570
|
-
"""
|
|
571
|
-
!!! note "Summary"
|
|
572
|
-
Check docstrings in Python files.
|
|
573
|
-
|
|
574
|
-
???+ abstract "Details"
|
|
575
|
-
This command checks the docstrings in the specified Python file or directory.
|
|
576
|
-
|
|
577
|
-
Params:
|
|
578
|
-
path (str):
|
|
579
|
-
The path to the Python file or directory to check.
|
|
595
|
+
path (Optional[str]):
|
|
596
|
+
Path to Python file or directory to check.
|
|
580
597
|
config (Optional[str]):
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
598
|
+
Path to configuration file (TOML format).
|
|
599
|
+
exclude (Optional[list[str]]):
|
|
600
|
+
Glob patterns to exclude.
|
|
601
|
+
output (str):
|
|
602
|
+
Output format: 'table' or 'list'.
|
|
603
|
+
check (bool):
|
|
604
|
+
Throw error if any issues are found.
|
|
586
605
|
quiet (bool):
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
Show
|
|
592
|
-
help_flag (Optional[bool]):
|
|
593
|
-
Show help message and exit.
|
|
594
|
-
|
|
595
|
-
Returns:
|
|
596
|
-
(None):
|
|
597
|
-
Nothing is returned.
|
|
598
|
-
"""
|
|
599
|
-
# Parse the recursive string value into a boolean
|
|
600
|
-
try:
|
|
601
|
-
recursive_bool: bool = _parse_recursive_flag(recursive)
|
|
602
|
-
except ValueError as e:
|
|
603
|
-
raise BadParameter(
|
|
604
|
-
message=(
|
|
605
|
-
f"Invalid value for --recursive: '{recursive}'.{NEW_LINE}"
|
|
606
|
-
"Use one of: true/false, t/f, yes/no, y/n, 1/0, or on/off."
|
|
607
|
-
)
|
|
608
|
-
) from e
|
|
609
|
-
_check_docstrings(path, config, recursive_bool, exclude, quiet, verbose)
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
@app.command(
|
|
613
|
-
rich_help_panel="Commands",
|
|
614
|
-
add_help_option=False, # Disable automatic help so we can add our own with -h
|
|
615
|
-
)
|
|
616
|
-
def config_example(
|
|
617
|
-
help_flag: Optional[bool] = Option(
|
|
618
|
-
None,
|
|
619
|
-
"--help",
|
|
620
|
-
"-h",
|
|
621
|
-
callback=_help_callback,
|
|
622
|
-
is_eager=True,
|
|
623
|
-
help="Show this message and exit",
|
|
624
|
-
),
|
|
625
|
-
) -> None:
|
|
626
|
-
"""
|
|
627
|
-
!!! note "Summary"
|
|
628
|
-
Show example configuration file.
|
|
629
|
-
|
|
630
|
-
Params:
|
|
606
|
+
Only output pass/fail confirmation.
|
|
607
|
+
example (Optional[str]):
|
|
608
|
+
Show examples: 'config' or 'usage'.
|
|
609
|
+
version (Optional[bool]):
|
|
610
|
+
Show version and exit.
|
|
631
611
|
help_flag (Optional[bool]):
|
|
632
612
|
Show help message and exit.
|
|
633
613
|
|
|
@@ -635,74 +615,26 @@ def config_example(
|
|
|
635
615
|
(None):
|
|
636
616
|
Nothing is returned.
|
|
637
617
|
"""
|
|
638
|
-
example_config: str = dedent(
|
|
639
|
-
"""
|
|
640
|
-
# Example configuration for docstring-format-checker
|
|
641
|
-
# Place this in your pyproject.toml file
|
|
642
|
-
|
|
643
|
-
[tool.dfc]
|
|
644
|
-
# or [tool.docstring-format-checker]
|
|
645
|
-
|
|
646
|
-
[[tool.dfc.sections]]
|
|
647
|
-
order = 1
|
|
648
|
-
name = "summary"
|
|
649
|
-
type = "free_text"
|
|
650
|
-
admonition = "note"
|
|
651
|
-
prefix = "!!!"
|
|
652
|
-
required = true
|
|
653
|
-
|
|
654
|
-
[[tool.dfc.sections]]
|
|
655
|
-
order = 2
|
|
656
|
-
name = "details"
|
|
657
|
-
type = "free_text"
|
|
658
|
-
admonition = "info"
|
|
659
|
-
prefix = "???+"
|
|
660
|
-
required = false
|
|
661
|
-
|
|
662
|
-
[[tool.dfc.sections]]
|
|
663
|
-
order = 3
|
|
664
|
-
name = "params"
|
|
665
|
-
type = "list_name_and_type"
|
|
666
|
-
required = true
|
|
667
|
-
|
|
668
|
-
[[tool.dfc.sections]]
|
|
669
|
-
order = 4
|
|
670
|
-
name = "returns"
|
|
671
|
-
type = "list_name_and_type"
|
|
672
|
-
required = false
|
|
673
618
|
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
required = false
|
|
679
|
-
|
|
680
|
-
[[tool.dfc.sections]]
|
|
681
|
-
order = 6
|
|
682
|
-
name = "raises"
|
|
683
|
-
type = "list_type"
|
|
684
|
-
required = false
|
|
619
|
+
# If no path is provided, show help
|
|
620
|
+
if path is None:
|
|
621
|
+
echo(ctx.get_help())
|
|
622
|
+
raise Exit(0)
|
|
685
623
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
admonition = "example"
|
|
691
|
-
prefix = "???+"
|
|
692
|
-
required = false
|
|
624
|
+
# Validate output format
|
|
625
|
+
if output not in ["table", "list"]:
|
|
626
|
+
console.print(_red(f"Error: Invalid output format '{output}'. Use 'table' or 'list'."))
|
|
627
|
+
raise Exit(1)
|
|
693
628
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
""".strip()
|
|
629
|
+
_check_docstrings(
|
|
630
|
+
path=path,
|
|
631
|
+
config=config,
|
|
632
|
+
exclude=exclude,
|
|
633
|
+
quiet=quiet,
|
|
634
|
+
output=output,
|
|
635
|
+
check=check,
|
|
702
636
|
)
|
|
703
637
|
|
|
704
|
-
print(example_config)
|
|
705
|
-
|
|
706
638
|
|
|
707
639
|
def entry_point() -> None:
|
|
708
640
|
"""
|
|
@@ -167,18 +167,15 @@ class DocstringChecker:
|
|
|
167
167
|
def check_directory(
|
|
168
168
|
self,
|
|
169
169
|
directory_path: Union[str, Path],
|
|
170
|
-
recursive: bool = True,
|
|
171
170
|
exclude_patterns: Optional[list[str]] = None,
|
|
172
171
|
) -> dict[str, list[DocstringError]]:
|
|
173
172
|
"""
|
|
174
173
|
!!! note "Summary"
|
|
175
|
-
Check docstrings in all Python files in a directory.
|
|
174
|
+
Check docstrings in all Python files in a directory recursively.
|
|
176
175
|
|
|
177
176
|
Params:
|
|
178
177
|
directory_path (Union[str, Path]):
|
|
179
178
|
Path to the directory to check.
|
|
180
|
-
recursive (bool):
|
|
181
|
-
Whether to check subdirectories recursively.
|
|
182
179
|
exclude_patterns (Optional[list[str]]):
|
|
183
180
|
List of glob patterns to exclude.
|
|
184
181
|
|
|
@@ -200,13 +197,7 @@ class DocstringChecker:
|
|
|
200
197
|
if not directory_path.is_dir():
|
|
201
198
|
raise DirectoryNotFoundError(f"Path is not a directory: {directory_path}")
|
|
202
199
|
|
|
203
|
-
|
|
204
|
-
if recursive:
|
|
205
|
-
pattern = "**/*.py"
|
|
206
|
-
else:
|
|
207
|
-
pattern = "*.py"
|
|
208
|
-
|
|
209
|
-
python_files: list[Path] = list(directory_path.glob(pattern))
|
|
200
|
+
python_files: list[Path] = list(directory_path.glob("**/*.py"))
|
|
210
201
|
|
|
211
202
|
# Filter out excluded patterns
|
|
212
203
|
if exclude_patterns:
|
|
@@ -860,7 +851,7 @@ class DocstringChecker:
|
|
|
860
851
|
if has_colon:
|
|
861
852
|
errors.append(
|
|
862
853
|
f"Section '{section_title_clean}' is an admonition, therefore it should not end with ':', "
|
|
863
|
-
f"see: {match.group(0)}"
|
|
854
|
+
f"see: '{match.group(0)}'"
|
|
864
855
|
)
|
|
865
856
|
|
|
866
857
|
# Check non-admonition sections (should end with colon)
|
|
@@ -878,7 +869,7 @@ class DocstringChecker:
|
|
|
878
869
|
if not has_colon:
|
|
879
870
|
errors.append(
|
|
880
871
|
f"Section '{section_name}' is non-admonition, therefore it must end with ':', "
|
|
881
|
-
f"see: {line}"
|
|
872
|
+
f"see: '{line}'"
|
|
882
873
|
)
|
|
883
874
|
|
|
884
875
|
return errors
|
|
@@ -976,8 +967,8 @@ class DocstringChecker:
|
|
|
976
967
|
# Pattern: name (type): or (type):
|
|
977
968
|
if not re.search(r"\([^)]+\):", stripped_line):
|
|
978
969
|
errors.append(
|
|
979
|
-
f"Section '{current_section.name}' (type: {current_section.type}) requires "
|
|
980
|
-
f"parenthesized types,
|
|
970
|
+
f"Section '{current_section.name}' (type: '{current_section.type}') requires "
|
|
971
|
+
f"parenthesized types, see: '{stripped_line}'"
|
|
981
972
|
)
|
|
982
973
|
|
|
983
974
|
# For list_type sections, check format like "(Type):"
|
|
@@ -985,8 +976,8 @@ class DocstringChecker:
|
|
|
985
976
|
# Pattern: (Type):
|
|
986
977
|
if not re.search(r"^\s*\([^)]+\):", stripped_line):
|
|
987
978
|
errors.append(
|
|
988
|
-
f"Section '{current_section.name}' (type: {current_section.type}) requires "
|
|
989
|
-
f"parenthesized types,
|
|
979
|
+
f"Section '{current_section.name}' (type: '{current_section.type}') requires "
|
|
980
|
+
f"parenthesized types, see: '{stripped_line}'"
|
|
990
981
|
)
|
|
991
982
|
|
|
992
983
|
return errors
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|