Skip to content

Fix radon complexity warnings across browser table-view code + tests - #748

Merged
ajslater merged 1 commit into
developfrom
fix-radon-complexity
May 9, 2026
Merged

ajslater merged 1 commit into
developfrom
fix-radon-complexity

Conversation

@ajslater

@ajslater ajslater commented May 9, 2026

Copy link
Copy Markdown
Owner

Summary

All five files reported by bin/lint-complexity.sh (radon cc --min C plus the mi --min B for test_browser_table_response.py) are now under threshold. Pure decomposition / consolidation — no behavior change.

What changed

Production code

Function CC before CC after Change
intersections._compute_simple_m2m_intersections_batched 12 6 Extracted _initialize_simple_m2m_buckets, _union_simple_m2m_queries, _collect_simple_m2m_intersections. Main function reads as a 6-line pipeline.
order_by.BrowserOrderByView._add_comic_order_by 11 1 Split into _normalize_comic_order_key (key resolution) and _comic_order_fields_head / _comic_sort_name_head / _comic_indexed_head (head-building branches). Top-level method is a 2-line orchestrator.
annotate.order.BrowserAnnotateOrderView.annotate_extra_order_values 11 6 Hoisted the early-return predicate into _should_annotate_extras and the per-extra annotation map into _build_extra_annotations.

Tests

Function CC before CC after Change
test_save_view_round_trips_table_fields 11 4 Extracted _save_named_view and _load_saved_settings round-trip helpers; collapsed four field-by-field assertions into a single dict-equality check against the live patch payload.
test_table_view_m2m_sort_groups_identical_sets 19 5 Extracted _create_sibling_comic factory (replaces three ~12-line Comic.objects.create blocks) and _assert_genre_sort_classes. The equivalence-class assertion uses frozenset + sorted for a single equality check.
test_table_view_simple_m2m_intersections_share_one_union_query 11 5 Extracted the seven-table or chain into a module-level _SIMPLE_M2M_THROUGH_TABLES tuple + a _count_through_table_queries helper that uses any().

File-level MI

  • tests/test_browser_table_response.py: B (17.08) → A (19.66). Driven entirely by the function-level fixes above; no other restructuring.

Test plan

  • bin/lint-complexity.sh clean (no functions over CC 10, no files at MI grade B)
  • complexipy clean (no functions over CC 15)
  • ruff check clean
  • 193 backend pytest tests pass

Reviewer notes

  • The new _create_sibling_comic test helper is private to the test class and replicates the setUp Comic-creation shape with a uniform size = 42 + issue_number formula. Per-call size keeps cumulative-sum assertions in adjacent tests deterministic. (Existing tests didn't use the helper because their assertions don't constrain size in ways that conflict with the formula — but if a future test does, override locally.)

🤖 Generated with Claude Code

All five files were in the radon ``cc --min C`` output (or for one
test file, ``mi --min B``). Pure decomposition / consolidation, no
behavior change.

Production code:
- ``intersections._compute_simple_m2m_intersections_batched`` (CC 12 → 6):
  extract ``_initialize_simple_m2m_buckets``,
  ``_union_simple_m2m_queries``, and
  ``_collect_simple_m2m_intersections``. Main function now reads as
  a 6-line pipeline.
- ``order_by.BrowserOrderByView._add_comic_order_by`` (CC 11 → 1):
  split the order-key normalization out into
  ``_normalize_comic_order_key`` and the head-building branches into
  ``_comic_order_fields_head`` / ``_comic_sort_name_head`` /
  ``_comic_indexed_head``. Top-level method is now a 2-line orchestrator.
- ``annotate.order.BrowserAnnotateOrderView.annotate_extra_order_values``
  (CC 11 → 6): hoist the early-return predicate into
  ``_should_annotate_extras`` and the per-extra annotation map into
  ``_build_extra_annotations``. The applier function now reads top-
  to-bottom with no nested branches.

Tests:
- ``test_save_view_round_trips_table_fields`` (CC 11 → 4): extract
  ``_save_named_view`` and ``_load_saved_settings`` round-trip
  helpers; collapse four field-by-field assertions into a single
  dict-equality check against the live patch payload.
- ``test_table_view_m2m_sort_groups_identical_sets`` (CC 19 → 5):
  extract ``_create_sibling_comic`` factory (replaces three
  ~12-line ``Comic.objects.create`` blocks) and
  ``_assert_genre_sort_classes`` for the equivalence-class
  verification. The class-counts assertion uses ``frozenset`` +
  ``sorted`` for a single equality check.
- ``test_table_view_simple_m2m_intersections_share_one_union_query``
  (CC 11 → 5): extract the seven-table ``or`` chain into a module-
  level ``_SIMPLE_M2M_THROUGH_TABLES`` tuple + a
  ``_count_through_table_queries`` helper that uses ``any()``.
- ``tests/test_browser_table_response.py`` MI: B 17.08 → A 19.66
  (driven by the function-level fixes above).

Verified: ``bin/lint-complexity.sh`` clean, ``ruff check`` clean,
193 backend tests pass.
@ajslater
ajslater merged commit 31a9c96 into develop May 9, 2026
3 checks passed
@ajslater
ajslater deleted the fix-radon-complexity branch May 11, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant