refactor: Remove too-many-statements in multiple files#2093
Open
thamitaboza wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_mapsindoctr/models/detection/differentiable_binarization/base.py_extract_features,_setup_queries,_prepare_encoder_inputs,_encoder_group_predictions,_decode_and_predict,_prepare_outputs, and_compute_lossesindoctr/models/layout/lw_detr/pytorch.py_forward_simpleand_forward_with_aspect_ratioindoctr/transforms/modules/pytorch.py_get_classes,_collect_gt_by_image,_collect_detections,_match_detections, and_evaluate_classindoctr/utils/metrics.pyWhy is this change necessary?
Methods like
LWDETR.forwardandObjectDetectionMetric.summarywere 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?
make qualityand Pylint score verified locally (achieved 10/10 for statement metrics on affected files).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.