formate.reformat_generics

Formats generics (List[...], Union[...] etc.) at the module and class level.

Example output, with a line length of 100:

ParamsMappingValueType = Union[str, bytes, int, float, Iterable[Union[str, bytes, int, float]]]
Data = Union[None, str, bytes, MutableMapping[str, Any], Iterable[Tuple[str, Optional[str]]], IO]
ParamsType = Union[
    Mapping[Union[str, bytes, int, float], ParamsMappingValueType],
    Union[str, bytes],
    Tuple[Union[str, bytes, int, float], ParamsMappingValueType],
    None
    ]

Functions:

reformat_generics(source[, …])

Reformats generics (typing.Generic, typing.Union, typing.Callable etc.) in the given source, and returns the reformatted source.

Classes:

Generic(name, elements)

Represents a typing.Generic, typing.Union, typing.Callable etc.

List(elements)

Represents a list of elements, most often used within a typing.Callable.

reformat_generics(source, formate_global_config=None, **kwargs)[source]

Reformats generics (typing.Generic, typing.Union, typing.Callable etc.) in the given source, and returns the reformatted source.

Parameters
  • source (str) – The source to reformat.

  • formate_global_config (Optional[Mapping]) – The global configuration dictionary. Optional. Default None.

  • **kwargs

Return type

str

Returns

The reformatted source.

class Generic(name, elements)[source]

Bases: object

Represents a typing.Generic, typing.Union, typing.Callable etc.

Parameters

Methods:

format([line_offset])

Formats the Generic.

format(line_offset=0)[source]

Formats the Generic.

Parameters

line_offset (int) – Default 0.

Return type

str

class List(elements)[source]

Bases: object

Represents a list of elements, most often used within a typing.Callable.

Parameters

elements (Sequence[Union[str, Generic, List]])