Skip to content

Commit dc775eb

Browse files
committed
Add --allow-errors option
This option can be used as a ratchet [1] to make it easier to adopt docstub gradually. Basically, limit the allowed number of errors and decrease it when errors are fixed. This should prevent unrelated development efforts from adding new errors. [1] https://qntm.org/ratchet
1 parent bb6d93c commit dc775eb

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

src/docstub/_cli.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,37 +132,55 @@ def report_execution_time():
132132

133133
@click.command()
134134
@click.version_option(__version__)
135-
@click.argument("root_path", type=click.Path(exists=True))
135+
@click.argument("root_path", type=click.Path(exists=True), metavar="PACKAGE_PATH")
136136
@click.option(
137137
"-o",
138138
"--out-dir",
139139
type=click.Path(file_okay=False),
140+
metavar="PATH",
140141
help="Set output directory explicitly.",
141142
)
142143
@click.option(
143144
"--config",
144145
"config_path",
145146
type=click.Path(exists=True, dir_okay=False),
147+
metavar="PATH",
146148
help="Set configuration file explicitly.",
147149
)
148150
@click.option(
149151
"--group-errors",
150152
is_flag=True,
151-
help="Group errors by type and content. "
152-
"Will delay showing errors until all files have been processed.",
153+
help="Group identical errors together and list where they occured. "
154+
"Will delay showing errors until all files have been processed. "
155+
"Otherwise, simply report errors as the occur.",
153156
)
154-
@click.option("-v", "--verbose", count=True, help="Log more details.")
157+
@click.option(
158+
"--allow-errors",
159+
type=click.IntRange(min=0),
160+
default=0,
161+
show_default=True,
162+
metavar="INT",
163+
help="Allow this many or fewer errors. "
164+
"If docstub reports more, exit with error code '1'.",
165+
)
166+
@click.option("-v", "--verbose", count=True, help="Print more details (repeatable).")
155167
@click.help_option("-h", "--help")
156168
@report_execution_time()
157-
def main(root_path, out_dir, config_path, group_errors, verbose):
158-
"""Generate Python stub files from docstrings.
169+
def main(root_path, out_dir, config_path, group_errors, allow_errors, verbose):
170+
"""Generate Python stub files with type annotations from docstrings.
171+
172+
Given a path `PACKAGE_PATH` to a Python package, generate stub files for it.
173+
Type descriptions in docstrings will be used to fill in missing inline type
174+
annotations or to override them.
159175
\f
160176
161177
Parameters
162178
----------
163179
source_dir : Path
164180
out_dir : Path
165181
config_path : Path
182+
group_errors : bool
183+
allow_errors : int
166184
verbose : str
167185
"""
168186

@@ -232,10 +250,18 @@ def main(root_path, out_dir, config_path, group_errors, verbose):
232250

233251
unknown_doctypes = types_db.stats["unknown_doctypes"]
234252
if unknown_doctypes:
235-
click.secho(f"{len(unknown_doctypes)} unknown doctypes:", fg="red")
253+
click.secho(f"{len(unknown_doctypes)} unknown doctypes", fg="red")
236254
counter = Counter(unknown_doctypes)
237-
for item, count in sorted(counter.items(), key=lambda x: x[1]):
255+
sorted_item_counts = sorted(counter.items(), key=lambda x: x[1], reverse=True)
256+
for item, count in sorted_item_counts:
238257
click.echo(f" {item} (x{count})")
239258

240-
if unknown_doctypes or syntax_error_count:
259+
total_errors = len(unknown_doctypes) + syntax_error_count
260+
total_msg = f"{total_errors} total errors"
261+
if allow_errors:
262+
total_msg = f"{total_msg} (allowed {allow_errors})"
263+
click.secho(total_msg, bold=True)
264+
265+
if allow_errors < total_errors:
266+
logger.debug("number of allowed errors %i was exceeded")
241267
sys.exit(1)

0 commit comments

Comments
 (0)