Skip to content

refactor: Remove too-many-statements in multiple files#2093

Open
thamitaboza wants to merge 1 commit into
mindee:mainfrom
thamitaboza:refactor/remove-code-smell-too-many-statements
Open

refactor: Remove too-many-statements in multiple files#2093
thamitaboza wants to merge 1 commit into
mindee:mainfrom
thamitaboza:refactor/remove-code-smell-too-many-statements

Conversation

@thamitaboza

Copy link
Copy Markdown

What does this PR do?

Refactors monolithic structures across multiple files to eliminate Pylint too-many-statements (R0915) warnings. Heavy logical blocks were extracted into specialized, private helper methods with single responsibilities:

  • _draw_polygon_on_maps in doctr/models/detection/differentiable_binarization/base.py
  • _extract_features, _setup_queries, _prepare_encoder_inputs, _encoder_group_predictions, _decode_and_predict, _prepare_outputs, and _compute_losses in doctr/models/layout/lw_detr/pytorch.py
  • _forward_simple and _forward_with_aspect_ratio in doctr/transforms/modules/pytorch.py
  • _get_classes, _collect_gt_by_image, _collect_detections, _match_detections, and _evaluate_class in doctr/utils/metrics.py

Why is this change necessary?

Methods like LWDETR.forward and ObjectDetectionMetric.summary were handling too many distinct operations simultaneously (e.g., mixing raw tensor slicing, dictionary evaluation, and geometry constraints within nested loops). This accumulation made code maintenance highly error-prone. Splitting them drops statement counts well below limits, isolates complex tensor scopes, and heavily reduces cognitive load.

How was it tested?

  • Built-in quality check engines executed via make quality and Pylint score verified locally (achieved 10/10 for statement metrics on affected files).
  • Unit tests executed locally using the project test suite runner (make test). Core execution logic, bounding box matching, and transforms remained fully operational.

Notes for the reviewer

The external API semantics and functional behaviors remain entirely identical. Variables and mathematical thresholds were isolated cleanly within their new private methods to prevent cross-scope leaks, easing future pipeline updates and production exports.

This change resolves the too-many-statements Pylint warnings by
extracting monolithic blocks into specialized private functions.

The refactoring directly addresses code smells within:
 - doctr/models/detection/differentiable_binarization/base.py
 - doctr/models/layout/lw_detr/pytorch.py
 - doctr/transforms/modules/pytorch.py
 - doctr/utils/metrics.py

Isolating these heavy operations ensures the main execution paths
remain readable and lowers cognitive load without changing behavior.
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