Skip to content

Conversation

@misrasaurabh1
Copy link
Contributor

@misrasaurabh1 misrasaurabh1 commented Jun 9, 2025

PR Type

Enhancement, Tests


Description

  • Normalize code before hashing using ast.unparse

  • Standardize environment checks and quoting

  • Update tests to use single quotes

  • Remove extraneous comments and blank lines


Changes walkthrough 📝

Relevant files
Enhancement
code_context_extractor.py
Normalize code before hashing                                                       

codeflash/context/code_context_extractor.py

  • Imported ast for code processing
  • Added ast.unparse normalization under HASHING context
  • +5/-1     
    Tests
    test_code_context_extractor.py
    Standardize test quoting and formatting                                   

    tests/test_code_context_extractor.py

  • Replaced double quotes with single quotes
  • Removed unnecessary blank lines and comments
  • Consolidated multi-line signatures into single lines
  • +21/-57 

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @github-actions
    Copy link

    github-actions bot commented Jun 9, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Missing import

    The new use of logging.warning requires an import of the logging module; without it calls to logging.warning will cause a NameError.

    if isinstance(node, cst.FunctionDef):
    Bare except

    Using a bare except around hash key generation catches all exceptions, including system exits or keyboard interrupts; consider catching specific exceptions instead.

    return None, False
    Unhandled parse errors

    Parsing and unparsing code with ast.parse/unparse may raise SyntaxError for invalid code; wrap in try/except to provide a fallback to the original code string.

        code = ast.unparse(ast.parse(code))  # Makes it standard
    return code

    @github-actions
    Copy link

    github-actions bot commented Jun 9, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Catch AST parse errors

    Wrap the parsing and unparsing in a try/except block to avoid crashing on invalid
    syntax. Fallback to the original code if a SyntaxError occurs.

    codeflash/context/code_context_extractor.py [516]

    -code = ast.unparse(ast.parse(code))  # Makes it standard
    +try:
    +    code = ast.unparse(ast.parse(code))
    +except SyntaxError:
    +    pass
    Suggestion importance[1-10]: 6

    __

    Why: Catching a SyntaxError when unparsing ensures invalid snippets don’t crash the extractor and preserves the original code as a fallback.

    Low
    General
    Trim normalized code whitespace

    Strip leading and trailing whitespace after normalization to ensure consistent code
    hashes. This removes incidental formatting differences.

    codeflash/context/code_context_extractor.py [516]

    -code = ast.unparse(ast.parse(code))  # Makes it standard
    +code = ast.unparse(ast.parse(code)).strip()
    Suggestion importance[1-10]: 4

    __

    Why: Adding .strip() removes incidental formatting differences, improving hashing consistency with minimal impact.

    Low

    @misrasaurabh1 misrasaurabh1 merged commit 8fe970c into main Jun 9, 2025
    16 checks passed
    Comment on lines +514 to +517
    code = str(filtered_node.code)
    if code_context_type == CodeContextType.HASHING:
    code = ast.unparse(ast.parse(code)) # Makes it standard
    return code
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    codeflash/code_utils/code_replacer.py has normalize_code

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    yes but it did more things as well which i did not want to happen. I have already removed those sections earlier in the libcst processing code

    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.

    3 participants