Favor readability and maintainability by keeping functions/methods short, extracting complex logic into well-named helpers, and avoiding duplicated or closure-based implementations in constructors.
Apply these rules:
__init__ as closures; create private methods (or module-level functions) and wire them in from __init__.TYPE_CHECKING blocks).Example (extracting complex logic into helpers):
def _merge_reasoning_details(details: list[dict[str, Any]]) -> list[dict[str, Any]]:
if len(details) <= 1:
return details
merged: list[dict[str, Any]] = []
i = 0
while i < len(details):
entry = details[i]
if not _is_mergeable_fragment(entry):
merged.append(entry)
i += 1
continue
entry_type, entry_index, text_key = _fragment_key(entry)
base = _fragment_base(entry, text_key)
texts = [_fragment_text(entry, text_key)]
i += 1
while i < len(details) and _matches_fragment(details[i], entry_type, entry_index):
texts.append(details[i].get(text_key, "") or "")
base = _merge_fragment_metadata(base, details[i], text_key)
i += 1
merged.append({**base, text_key: "".join(texts)})
return merged
Enter the URL of a public GitHub repository