IpynbFormatter

class IpynbFormatter(options: Namespace)[source]

Default flake8_nb formatter for jupyter notebooks.

If the file to be formatted is a *.py file, it uses flake8’s default formatter.

Initialize with the options parsed from config and cli.

This also calls a hook, after_init(), so subclasses do not need to call super to call this method.

Parameters:

options – User specified configuration parsed from both configuration files and the command-line interface.

Attributes Summary

error_format

Methods Summary

after_init

Check for a custom format string.

beginning

Notify the formatter that we're starting to process a file.

finished

Notify the formatter that we've finished processing a file.

format

Format the error detected by a flake8 checker.

handle

Handle an error reported by Flake8.

show_benchmarks

Format and print the benchmarks.

show_source

Show the physical line generating the error.

show_statistics

Format and print the statistics.

start

Prepare the formatter to receive input.

stop

Clean up after reporting is finished.

write

Write the line either to the output file or stdout.

Methods Documentation

after_init() None[source]

Check for a custom format string.

beginning(filename: str) None

Notify the formatter that we’re starting to process a file.

Parameters:

filename – The name of the file that Flake8 is beginning to report results from.

finished(filename: str) None

Notify the formatter that we’ve finished processing a file.

Parameters:

filename – The name of the file that Flake8 has finished reporting results from.

format(violation: Violation) str | None[source]

Format the error detected by a flake8 checker.

Depending on if the violation was caused by a *.py file or by a parsed notebook.

Parameters:

violation (Violation) – Error a checker reported.

Returns:

Formatted error message, which will be displayed in the terminal.

Return type:

str | None

handle(error: Violation) None

Handle an error reported by Flake8.

This defaults to calling format(), show_source(), and then write(). To extend how errors are handled, override this method.

Parameters:

error – This will be an instance of Violation.

show_benchmarks(benchmarks: List[Tuple[str, float]]) None

Format and print the benchmarks.

show_source(error: Violation) str | None

Show the physical line generating the error.

This also adds an indicator for the particular part of the line that is reported as generating the problem.

Parameters:

error – This will be an instance of Violation.

Returns:

The formatted error string if the user wants to show the source. If the user does not want to show the source, this will return None.

show_statistics(statistics: Statistics) None

Format and print the statistics.

start() None

Prepare the formatter to receive input.

This defaults to initializing output_fd if filename

stop() None

Clean up after reporting is finished.

write(line: str | None, source: str | None) None

Write the line either to the output file or stdout.

This handles deciding whether to write to a file or print to standard out for subclasses. Override this if you want behaviour that differs from the default.

Parameters:
  • line – The formatted string to print or write.

  • source – The source code that has been formatted and associated with the line of output.