Skip to content

[FIX] Circular dependency and global assignments imports #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 28, 2025

Conversation

mohammedahmed18
Copy link
Contributor

@mohammedahmed18 mohammedahmed18 commented Jun 27, 2025

Description

This fixes two issues

  • circular dependency, which can happen if some object in a module is referenced in the same module and also is imported in the optimized code context like here

  • imports of the global assignments are not included in the module if they weren't used before adding them, which result in a fatal error when running the tests, fixed just by adding the global assignments before adding the imports, not the opposite

Changes walkthrough 📝

Relevant files
Bug fix
code_extractor.py
Skip same-module imports to prevent circular references   

codeflash/code_utils/code_extractor.py

  • Added condition to skip imports when dst_context.full_module_name
    equals module
  • Consolidated import checks into improved if statement
  • +4/-1     

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @mohammedahmed18 mohammedahmed18 changed the title [FIX] avoid importing from the same module [FIX] avoid importing from the same module (CF-684) Jun 27, 2025
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    String Comparison

    Use == instead of is when comparing dst_context.full_module_name to mod to avoid unintended behavior from string interning.

    if (
        f"{mod}.{obj}" in helper_functions_fqn
        or dst_context.full_module_name is mod  # avoid circular imports
    ):
    Missing Tests

    There are no tests validating the new skip logic for helper functions or circular imports. Add tests to ensure imports are correctly skipped when expected.

    if (
        f"{mod}.{obj}" in helper_functions_fqn
        or dst_context.full_module_name is mod  # avoid circular imports
    ):
        continue  # Skip adding imports for helper functions already in the context
    AddImportsVisitor.add_needed_import(dst_context, mod, obj)
    RemoveImportsVisitor.remove_unused_import(dst_context, mod, obj)

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Use == for string comparison

    Use value comparison instead of identity when checking module names to avoid
    unpredictable behavior. Replace is with == for string comparison.

    codeflash/code_utils/code_extractor.py [334-338]

     if (
         f"{mod}.{obj}" in helper_functions_fqn
    -    or dst_context.full_module_name is mod  # avoid circular imports
    +    or dst_context.full_module_name == mod  # avoid circular imports
     ):
         continue  # Skip adding imports for helper functions already in the context
    Suggestion importance[1-10]: 8

    __

    Why: The code uses is to compare strings, which can lead to unpredictable behavior; using == ensures correct string comparison.

    Medium

    @misrasaurabh1
    Copy link
    Contributor

    Very cool! Thanks for looking into this. Can you add a test for this that would fail before this change and then passes after this change?

    if f"{mod}.{obj}" in helper_functions_fqn:
    if (
    f"{mod}.{obj}" in helper_functions_fqn
    or dst_context.full_module_name is mod # avoid circular imports
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    prefer == over is

    @mohammedahmed18 mohammedahmed18 changed the title [FIX] avoid importing from the same module (CF-684) [FIX] Circular dependency and global assignments imports Jun 28, 2025
    Copy link
    Contributor

    @misrasaurabh1 misrasaurabh1 left a comment

    Choose a reason for hiding this comment

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

    awesome, thanks

    @misrasaurabh1 misrasaurabh1 enabled auto-merge June 28, 2025 22:03
    @misrasaurabh1 misrasaurabh1 merged commit 2b5fa6e into main Jun 28, 2025
    16 checks passed
    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