Skip to content

Reduce cognitive complexity and remove commented code in ClassTypeMroUtils - #2318

Open
sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20261028-060339-10bf8f61
Open

sonarqube-agent[bot] wants to merge 1 commit into
masterfrom
remediate-master-20261028-060339-10bf8f61

Conversation

@sonarqube-agent

Copy link
Copy Markdown

This PR was automatically created by the Remediation Agent's Scheduled backlog remediation feature.

Why these issues? Both issues are located in the same file, enabling an efficient single-PR fix. The CRITICAL severity cognitive complexity issue (S3776) is prioritized first due to its higher severity and direct impact on code quality metrics. The MAJOR severity commented-code issue (S125) is bundled together as a complementary, well-scoped improvement in the same location.

This PR fixes two SonarQube issues in ClassTypeMroUtils.java: refactors the computeAlternatives method to reduce cognitive complexity from 16 to below the allowed threshold by extracting nested loops into a helper method, and removes a commented-out line flagged as dead code. These changes improve code maintainability and reduce technical debt while maintaining the same functionality.

View Project in SonarCloud


Fixed Issues

java:S3776 - Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. • CRITICAL • View issue

Location: python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java:61

Why 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

Replaces the doubly nested loops in computeAlternatives with a call to the new expandSelections helper. Those nested loops sat inside the outer union loop and added +2 and +3 nesting-weighted points. Moving them out cuts the method's cognitive complexity from 16 to below the allowed 15.

--- a/python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java
+++ b/python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java
@@ -73,9 +73,1 @@ final class ClassTypeMroUtils {
-      List<Map<UnionType, ClassType>> expanded = new ArrayList<>();
-      for (Map<UnionType, ClassType> selection : selections) {
-        for (PythonType candidate : union.candidates()) {
-          Map<UnionType, ClassType> alternative = new IdentityHashMap<>(selection);
-          alternative.put(union, (ClassType) candidate);
-          expanded.add(alternative);
-        }
-      }
-      selections = expanded;
+      selections = expandSelections(selections, union);
java:S125 - This block of commented-out lines of code should be removed. • MAJOR • View issue

Location: python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java:179

Why is this an issue?

Commented-out code distracts the focus from the actual executed code. It creates a noise that increases maintenance code. And because it is never executed, it quickly becomes out of date and invalid.

What changed

Deletes the comment line 'SuperProxyType also uses the known edges alone to reject invalid ancestry;'. The scanner flagged it as commented-out code, and removing it clears that warning.

--- a/python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java
+++ b/python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java
@@ -179,1 +182,0 @@ final class ClassTypeMroUtils {
-    // SuperProxyType also uses the known edges alone to reject invalid ancestry;

Have a suggestion or found an issue? Share your feedback here.


SonarQube Remediation Agent uses AI. Check for mistakes.
SonarQube Cloud Views of this pull request are counted to improve the agent.

Fixed issues:
- AaF8h6-Afx2H9N8dJgnQ for java:S125 rule
- AaF-F8chCdVe95fkeBSC for java:S3776 rule

Generated by SonarQube Agent (task: c65e0bc5-fea5-40c1-8ba2-a4c56259dfd0)
@gitar-bot

gitar-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Code Review 👍 Approved with suggestions 0 closed / 1 findings

🟢 Low risk · The union-selection loops move unchanged into a private helper, and removing commented code leaves runtime behavior and public interfaces unaffected.

Extracts the nested selection-expansion loops in computeAlternatives into an expandSelections helper to reduce cognitive complexity, and removes a stale comment line. Consider restoring the removed line in ClassTypeMroUtils.java (182-183): it was explanatory prose documenting that SuperProxyType calls compute with unresolved ancestry, so the partial-order caveat no longer has context.

💡 Quality: Removed comment was explanatory prose, not commented-out code

📄 python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java:182-183

The deleted line // SuperProxyType also uses the known edges alone to reject invalid ancestry; was not commented-out code. It was the middle of a three-line explanation. It records a real caller contract: SuperProxyType.directBaseInIncompleteHierarchy() calls ClassTypeMroUtils.compute(ownerClassType) when hasUnresolvedHierarchy() is true, so here compute can see unresolved ancestry and return a partial order.

With that line gone, two problems remain. First, the comment now says only that ClassType.mro() rejects unresolved ancestry before calling this helper, which suggests the helper never gets an incomplete hierarchy. That is wrong for the SuperProxyType path. Second, the next line, "the resulting partial order must not be used as a complete MRO", no longer refers to anything. Future maintainers lose the reason why partial results are acceptable here.

Fix: put the line back. The expandSelections extraction is fine.

Restore the explanatory comment line
// ClassType.mro() rejects unresolved ancestry before calling this helper.
// SuperProxyType also uses the known edges alone to reject invalid ancestry;
// the resulting partial order must not be used as a complete MRO.
🤖 Prompt for agents
Code Review: Extracts the nested selection-expansion loops in `computeAlternatives` into an `expandSelections` helper to reduce cognitive complexity, and removes a stale comment line. Consider restoring the removed line in `ClassTypeMroUtils.java` (182-183): it was explanatory prose documenting that `SuperProxyType` calls `compute` with unresolved ancestry, so the partial-order caveat no longer has context.

1. 💡 Quality: Removed comment was explanatory prose, not commented-out code
   Files: python-frontend/src/main/java/org/sonar/plugins/python/api/types/v2/ClassTypeMroUtils.java:182-183

   The deleted line `// SuperProxyType also uses the known edges alone to reject invalid ancestry;` was not commented-out code. It was the middle of a three-line explanation. It records a real caller contract: `SuperProxyType.directBaseInIncompleteHierarchy()` calls `ClassTypeMroUtils.compute(ownerClassType)` when `hasUnresolvedHierarchy()` is true, so here `compute` can see unresolved ancestry and return a partial order.
   
   With that line gone, two problems remain. First, the comment now says only that `ClassType.mro()` rejects unresolved ancestry before calling this helper, which suggests the helper never gets an incomplete hierarchy. That is wrong for the SuperProxyType path. Second, the next line, "the resulting partial order must not be used as a complete MRO", no longer refers to anything. Future maintainers lose the reason why partial results are acceptable here.
   
   Fix: put the line back. The `expandSelections` extraction is fine.

   Fix (Restore the explanatory comment line):
   // ClassType.mro() rejects unresolved ancestry before calling this helper.
   // SuperProxyType also uses the known edges alone to reject invalid ancestry;
   // the resulting partial order must not be used as a complete MRO.

Verification Rules 1/2 met
Rules 1 failed 1 passed 1 not applicable

❌ Internal artifact registries - Pre-existing python-frontend/typeshed_serializer/docker/Dockerfile runs pip install uv against public PyPI without Repox; CI build.yml correctly points uv at Repox. Not introduced by this PR.
✅ Repository license compliance

Functional validation No results

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

Comment on lines 182 to 183
// ClassType.mro() rejects unresolved ancestry before calling this helper.
// SuperProxyType also uses the known edges alone to reject invalid ancestry;
// the resulting partial order must not be used as a complete MRO.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Quality: Removed comment was explanatory prose, not commented-out code

The deleted line // SuperProxyType also uses the known edges alone to reject invalid ancestry; was not commented-out code. It was the middle of a three-line explanation. It records a real caller contract: SuperProxyType.directBaseInIncompleteHierarchy() calls ClassTypeMroUtils.compute(ownerClassType) when hasUnresolvedHierarchy() is true, so here compute can see unresolved ancestry and return a partial order.

With that line gone, two problems remain. First, the comment now says only that ClassType.mro() rejects unresolved ancestry before calling this helper, which suggests the helper never gets an incomplete hierarchy. That is wrong for the SuperProxyType path. Second, the next line, "the resulting partial order must not be used as a complete MRO", no longer refers to anything. Future maintainers lose the reason why partial results are acceptable here.

Fix: put the line back. The expandSelections extraction is fine.

Restore the explanatory comment line:

// ClassType.mro() rejects unresolved ancestry before calling this helper.
// SuperProxyType also uses the known edges alone to reject invalid ancestry;
// the resulting partial order must not be used as a complete MRO.
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant