Reduce cognitive complexity and fix code duplication in scikit-learn utilities - #644
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Reduce cognitive complexity and fix code duplication in scikit-learn utilities#644sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ45Cuz9RXnEWm2Rf4rC for python:S2638 rule - AZ45CvcPRXnEWm2Rf41B for python:S1192 rule - AZ45Cx_SRXnEWm2Rf5ZC for python:S3776 rule - AZ45CuOoRXnEWm2Rf4iL for python:S3776 rule - AZ45CuOoRXnEWm2Rf4iM for python:S3776 rule Generated by SonarQube Agent (task: 7fcef932-1bb0-44cf-877d-1ba3a690a0fe)
Author
|
|
|
|
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.



This PR fixes 5 CRITICAL SonarQube issues including reducing cognitive complexity in dict vectorizer and pretty printing utilities, eliminating string literal duplication, and adding a missing function parameter to maintain Liskov Substitution Principle compliance. These changes improve code maintainability, reduce nesting depth, and ensure method signature consistency across inheritance hierarchies.
View Project in SonarCloud
Fixed Issues
python:S1192 - Define a constant instead of duplicating this literal "boolean indexing" 3 times. • CRITICAL • View issue
Location:
sklearn/externals/array_api_extra/_lib/_utils/_helpers.py:327Why is this an issue?
Duplicated string literals make the process of refactoring complex and error-prone, as any change would need to be propagated on all occurrences.
What changed
Defines the constant
_BOOLEAN_INDEXINGto hold the string literal "boolean indexing", which was previously duplicated 3 times in thecapabilitiesfunction. This addresses the code smell about duplicating the string literal "boolean indexing" 3 times by providing a single constant that can be referenced from all locations, so any future change only needs to be made in one place.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 34 to the 15 allowed. • CRITICAL • View issue
Location:
sklearn/feature_extraction/_dict_vectorizer.py:194Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk extracts two new helper methods (
_process_feature_valueand_sort_feature_names) from the_transformmethod. The_process_feature_valuemethod encapsulates the logic for processing a single feature-value pair (type checking for str, Number, Iterable, vocabulary updates, etc.), and_sort_feature_namesencapsulates the feature name sorting and matrix reordering logic. By moving these deeply nested blocks into separate methods, the cognitive complexity of_transformis significantly reduced. The nested conditionals and loops that contributed heavily to the complexity score — including type checks at nesting level 2-3, vocab updates at nesting level 3-4, and the sorting loop — are now simple method calls, which are 'free' in cognitive complexity scoring. This directly addresses the high cognitive complexity (34 vs allowed 15) of the_transformfunction by breaking it down into smaller, easier-to-manage pieces.python:S3776 - Refactor this function to reduce its Cognitive Complexity from 28 to the 15 allowed. • CRITICAL • View issue 1
python:S3776 - Refactor this function to reduce its Cognitive Complexity from 21 to the 15 allowed. • CRITICAL • View issue 2
Location:
sklearn/utils/_pprint.py:230Why is this an issue?
Cognitive Complexity is a measure of how hard it is to understand the control flow of a unit of code. Code with high cognitive complexity is hard to read, understand, test, and modify.
What changed
This hunk extracts the compact item formatting logic from the
_format_itemsmethod into a new helper method_format_items_compact. By moving the width checking, delimiter management, and representation writing logic into its own method, the cognitive complexity of_format_itemsis reduced because the nested conditionals (if width < w,if delim,if width >= w) that contributed to its complexity score of 21 are now in a separate function. This also helps reduce the overall high cognitive complexity of_format_itemsat line 293, which had a score of 21 exceeding the allowed threshold of 15.python:S2638 - Add missing parameters random_state. • CRITICAL • View issue
Location:
sklearn/ensemble/_gb.py:961Why is this an issue?
Because a subclass instance may be used as an instance of the superclass, overriding methods should uphold the aspects of the superclass contract that relate to the Liskov Substitution Principle. Specifically, an overriding method should be callable with the same parameters as the overriden one.
What changed
This hunk adds the missing
random_stateparameter (with a default value ofNone) to the_make_estimatormethod inBaseGradientBoosting. The overriding method was previously missing therandom_stateparameter that exists in the superclassBaseEnsemble._make_estimatorsignature, violating the Liskov Substitution Principle. By addingrandom_state=Noneas an optional parameter, the overriding method now accepts the same parameters as the overridden one, making it callable with the same arguments as the parent class method. This directly resolves the code smell about the overriding method needing to add the missingrandom_stateparameter to match the overridden method's definition.SonarQube Remediation Agent uses AI. Check for mistakes.