Built-in Hooks

formate ships with several hooks out of the box:

dynamic_quotes

Reformats quotes in the given source, and returns the reformatted source.

This hook takes no arguments.

collections-import-rewrite

Identify deprecated from collections import <abc> imports, and rewrite them as from collections.abc import <abc>.

This hook takes no arguments.

noqa_reformat

Pull # noqa: ... comments that immediately follow docstrings back up to the end of the correct line.

This hook takes no arguments.

check_ast

Check the source can be parsed as a Python Abstract Syntax Tree. This could be called early in the execution – to check the file is valid before starting reformatting – and again at the end to ensure no errors were introduced by the reformatting.

This hook takes no arguments.

squish_stubs

Squash type stubs by removing unnecessary blank lines.

This hook takes no arguments.

ellipsis_reformat

Move ellipses (...) for type stubs onto the end of the stub definition.

Before:

def foo(value: str) -> int:
    ...

After:

def foo(value: str) -> int: ...

This hook takes no arguments.

reformat-generics

Reformats generics (typing.Generic, typing.Union, typing.Callable etc.).

This hook takes a single keyword argument: indent. The indent can also be configured via the indent key in the config table.

isort

Calls isort, using the given keyword arguments as its configuration.

This hook only takes keyword arguments.

The max line length can be provided via the line_length keyword argument or in the config table as line_length.

yapf

Calls yapf, using the given keyword arguments as its configuration.

This hook only takes keyword arguments.

The indent can be configured via the use_tabs keyword argument or in the config table as indent.