click_extra.bug_report moduleΒΆ

Bug reports for the errors a command has no rule for.

A command given a report_url ends the run on such an error with a report that its user can paste into an issue. The report gives the version and platform of the run, its options, and the traceback of the error, between two 8< markers. A link opens the issue form of the project with a title filled in. See invoke().

A CLI can also log a report of its own with log_report(), for a failure that does not stop the run.

Caution

Issues are often public, so a report holds no file path. The traceback keeps module names and line numbers, which say as much about the code as file paths, and nothing about where it is installed. The options line masks the values of paths, patterns and positional arguments. The message of the error stays as the code wrote it: the report asks the user to read it before they paste it.

click_extra.bug_report.MAX_FRAMES: Final = 12ΒΆ

Innermost frames of a traceback that a report keeps.

The frames past them give way to a line that counts them: the innermost ones hold the code that raised the error.

click_extra.bug_report.MAX_MESSAGE_LENGTH: Final = 200ΒΆ

Characters of an error message that a report keeps.

click_extra.bug_report.REPORT_START: Final = '----- 8< ----- bug report ----- 8< -----'ΒΆ

First line of the block that a user pastes into an issue.

click_extra.bug_report.REPORT_END: Final = '----- 8< ----- end of bug report ----- 8< -----'ΒΆ

Last line of the block that a user pastes into an issue.

click_extra.bug_report.REPORTS_FOLDER: Final = 'reports'ΒΆ

Folder of the cache directory of a CLI that holds its saved reports.

click_extra.bug_report.describe_error(expt)[source]ΒΆ

The class and message of an exception, on one line of capped length.

The class shows with its module, unless it is built in. The message keeps its first MAX_MESSAGE_LENGTH characters.

Return type:

str

click_extra.bug_report.compact_frames(tb)[source]ΒΆ

A traceback as module:line in function entries, outermost first.

Module names say as much as file paths about the code, and nothing about where it is installed, so they are what a report shows. A method shows with its class, like Path.open.

Return type:

tuple[str, ...]

click_extra.bug_report.cli_traceback(tb, callback=None)[source]ΒΆ

The traceback from its first frame outside the Click ecosystem.

An error reaches invoke() through the frames that Click and Click Extra run the command with, which are the same in every report. A traceback with no other frame is kept whole.

The code of a CLI built in the Click ecosystem, like the click-extra CLI, is in it too. So the frame of callback, the function of the command, also starts the traceback.

Return type:

TracebackType | None

click_extra.bug_report.culprit_function(tb, package)[source]ΒΆ

The name of the innermost function of package in a traceback, or nothing.

It names the code to fix, even for an error that Python or a library raised from a call of the package. A generator expression, a comprehension or a lambda has no name of its own, so the named function nearest to it in the traceback stands for it.

Return type:

str

click_extra.bug_report.masked_value(param, value)[source]ΒΆ

One value of a parameter as a report shows it.

A path or a pattern can name the files and the data of the user, so it shows as a placeholder, like the value of a positional argument, which is often the data the command works on. Other values show as typed.

Return type:

str

click_extra.bug_report.option_words(param, value)[source]ΒΆ

Render one parameter as typed on the command line, with its values masked.

A flag shows alone, or as its secondary name when it is off, like --no-color. A counting option shows once per count. See masked_value() for the values.

Return type:

list[str]

click_extra.bug_report.options_summary(ctx)[source]ΒΆ

The parameters of the run that do not come from their default, masked.

Walks from the root command to the command of ctx, and names each subcommand on the way. A value from a configuration file or an environment variable counts, as it changes the run as much as one typed on the command line.

The options of Click Extra keep no value of their own in the context, and none of them changes what a command does, except --jobs. It shows with the number of jobs it resolved to.

Return type:

str

click_extra.bug_report.cli_version(ctx)[source]ΒΆ

The version of the root command, as its --version option prints it.

The report reads it from a new VersionOption, which takes the fields the command pinned on its own. The module of the root command’s callback is pinned too, unless the command pinned one: the walk of the stack that finds it otherwise stops on the caller of the CLI, as the callback has returned when the error reaches the command.

Returns None when the version cannot be found.

Return type:

str | None

click_extra.bug_report.report_form_url(url, title)[source]ΒΆ

The URL of the issue form, with title filled in.

The query of url is kept, like the template that picks the form of a GitHub repository.

Return type:

str

click_extra.bug_report.render_report(headline, title, details, ctx)[source]ΒΆ

The message inviting the user to file a bug report, with the block to paste.

It links to the issue form of the report_url of the root command, with title filled in. The block opens with the version of the CLI, the platform and the options of the run, then gives the details of the error.

An AI agent running the CLI gets a line of its own: it is to show the report to its user, who decides to file it.

Return type:

str

click_extra.bug_report.save_report(report, ctx)[source]ΒΆ

Write a bug report to a new file in the REPORTS_FOLDER of the cache.

The cache is that of the root command, as get_cache_dir() locates it. A user who loses the output of a long run, to a closed terminal or a short scrollback, still finds the report there. The file holds the report as it is printed, so it can be attached to an issue whole.

Each report gets a new file, named after the time it was saved. mkstemp() adds random characters to the name and never reuses one, so two reports of the same second keep a file each.

Returns None if the file cannot be written: the printed report then stands alone.

Return type:

Path | None

click_extra.bug_report.log_report(level, headline, title, details, ctx=None)[source]ΒΆ

Log the report that render_report() builds, and save it with save_report().

The report goes to the root logger, which the CLI prints. The path of the saved file comes last, alone on its line, so it is the last thing a user sees of the run, and easy to copy. It stays out of the file.

ctx defaults to the context of the running command.

Return type:

None

click_extra.bug_report.report_unexpected_error(ctx, expt)[source]ΒΆ

End the run on an error that its command has no rule for.

An OSError comes from the system, like a missing permission or a full disk, so the user can fix it: it ends the run with a one-line error, and its traceback goes to the debug log. Any other error is a bug. Its traceback is logged with the report inviting the user to file it, and the run exits with code 1. The report shows the frames from cli_traceback() on.

The title of the issue names the class of the error and the innermost function of the package of the root command, which culprit_function() finds. Reports of the same bug then get the same title, so the issue form lists the one already open. Unlike a line number, a function name does not change between releases.

Return type:

NoReturn