Skip to content

Conversation

@hassineabd
Copy link

@hassineabd hassineabd commented Oct 9, 2025

This PR implements GCI109 rule to detect and reduce the use of exceptions for control flow in Python code.

  • Using exceptions for regular control flow is an anti-pattern that impacts performance
  • PEP 463: Discusses the performance cost of exceptions
  • Python encourages using exceptions only for exceptional cases, not predictable control flow
  • This anti-pattern lead to creating exception objects, stack traces etc consumes significant CPU cycles and creates objects in memory which translates to increased energy consumption

implemetation details : this rule detects the use of common control flow exceptions:

  • KeyError
  • IndexError
  • AttributeError
  • StopIteration

Copy link
Member

@dedece35 dedece35 left a comment

Choose a reason for hiding this comment

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

Hi @hassineabd,

thank you a lot for this interesting PR ...
before accepting this PR, please, you must do following tasks :

Please, in the rule documentation in creedengo-rules-specifications, I will particularly vigilent about the proof of your ideas ... please feel free to giver some URLs to prove your idea. A full example : https://github.com/green-code-initiative/creedengo-rules-specifications/blob/main/src/main/rules/GCI107/python/GCI107.asciidoc

Please, also, take into account Copilot review, bottom, and adapt / complete your unit tests with new modifications.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces the GCI109 rule to detect and discourage the use of exceptions for control flow in Python code, which is an anti-pattern that negatively impacts performance and energy consumption. The rule identifies try-except blocks that catch common control flow exceptions (KeyError, IndexError, AttributeError, StopIteration) and suggests using built-in alternatives like .get(), getattr(), bounds checking, or default parameters.

Key changes:

  • Implemented AvoidExceptionsForControlFlowCheck to detect control flow exception patterns
  • Added test coverage for both compliant and non-compliant code patterns
  • Integrated the rule into the creedengo profile and rule repository

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
AvoidExceptionsForControlFlowCheck.java Core implementation of GCI109 rule with AST traversal and exception detection logic
AvoidExceptionsForControlFlowCheckTest.java Unit test for the new rule
avoidExceptionsForControlFlow.py (test resources) Test cases demonstrating non-compliant and compliant patterns
avoidExceptionsForControlFlow.py (integration test) Integration test file with same test cases
PythonRuleRepository.java Registration of the new check in the rule repository
creedengo_way_profile.json Addition of GCI109 to the default profile
GCIRulesIT.java Integration test validating rule detection on test file
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@hassineabd hassineabd changed the title Add GCI109 rule - Avoid using exceptions for control flow Add GCI110 rule - Avoid using exceptions for control flow Oct 13, 2025
@hassineabd hassineabd changed the title Add GCI110 rule - Avoid using exceptions for control flow Add GCI109 rule - Avoid using exceptions for control flow Oct 13, 2025
@dedece35
Copy link
Member

Hi @hassineabd,

thank you for last modifications but it lacks the part about "creedengo-specifications-rules" in my last comment.

@hassineabd
Copy link
Author

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.

2 participants