File sorting.py
has 276 lines of code (exceeds 250 allowed). Consider refactoring.
"""Sorts entities in a less disruptive way.
Sorting beancount entities may sound as simple as `sorted(entities)` but doing it properly isn't trivial.
Function _split_sorted_unsorted
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
def _split_sorted_unsorted(
entities: Sequence[_O],
) -> tuple[list[_O], list[_O]]:
p = dict[tuple[int, int], tuple[int, int]]()
Function format_posting
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
def format_posting(posting: models.Posting, context: base.Context) -> Iterator[models.RawTokenModel]:
children_it = iterating.BufferedIterator(posting.iter_children_formatted())
for child, indented in children_it.take_until(lambda x: isinstance(x[0], models.Indent)):
Function _merge_entries
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
def _merge_entries(sorted: Sequence['_OrderedEntry'], unsorted: Sequence['_OrderedEntry']) -> Iterator[Sequence['_OrderedEntry']]:
cursor_sorted, cursor_unsorted = 0, 0
reversed_running_min_unsorted = _build_reversed_running_min(unsorted)
while cursor_sorted < len(sorted) and cursor_unsorted < len(unsorted):
prev_cursor_sorted = cursor_sorted
Function merge
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
def merge(cls, sorted: list['_OrderedBlock'], unsorted: list['_OrderedBlock']) -> Iterator['_OrderedBlock']:
keyed_unsorted = [(block.simple_sort_key(), block) for block in unsorted]
heapq.heapify(keyed_unsorted)
cursor_sorted = 0
while cursor_sorted < len(sorted) and keyed_unsorted:
Function format
has 6 arguments (exceeds 4 allowed). Consider refactoring.
def format(
Avoid too many return
statements within this function.
return None
Avoid too many return
statements within this function.
return False
Function sort_blocks
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
def sort_blocks(blocks: Iterable[Sequence[_TopLevelEntitiy]]) -> list[Sequence[_TopLevelEntitiy]]:
results = []
compartment = list[_OrderedBlock]()
first_entry_index = 0
for block in blocks:
Function format_cost
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
def format_cost(cost: models.UnitCost | models.TotalCost, context: base.Context) -> Iterator[models.RawTokenModel]:
spaces_in_braces = context.options.spaces_in_braces and next(iter(cost.raw_components), None) is not None
for child, indented in cost.iter_children_formatted():
if spaces_in_braces and isinstance(child, models.RightBrace | models.DblRightBrace):
Function _partition
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
def _partition(file: models.File, context: base.Context) -> list[_Block]:
prev = None
last_block = None
blocks: list[_Block] = []
for child, indented in file.iter_children_formatted():
Function format
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
def format(model: models.RawModel, context: Context) -> Iterator[models.RawTokenModel]:
formatter = _FORMATTERS.get(type(model))
if formatter:
yield from formatter(model, context)
elif isinstance(model, models.RawTokenModel):