Skip to content

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Feb 10, 2025

⚡️ This pull request contains optimizations for PR #6028

If you approve this dependent PR, these changes will be merged into the original PR branch PlaygroundPage.

This PR will be automatically closed if the original PR is merged.


📄 12% (0.12x) speedup for ErrorMessage._format_plain_reason in src/backend/base/langflow/schema/message.py

⏱️ Runtime : 44.8 microseconds 40.0 microseconds (best of 153 runs)

📝 Explanation and details

Here's the optimized version of the given Python program.

These changes ensure the program runs faster by reducing redundant operations and optimizing the existing logic to use simpler and efficient patterns.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 32 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage undefined
🌀 Generated Regression Tests Details
from __future__ import annotations

import re
import traceback
from uuid import UUID

# imports
import pytest  # used for our unit tests
from langflow.schema.content_block import ContentBlock
from langflow.schema.content_types import ErrorContent
from langflow.schema.message import ErrorMessage
from langflow.schema.properties import Properties, Source
from pydantic import ValidationError

# unit tests

def test_exception_with_body_message():
    """Test when exception has a body attribute with a message key."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"message": "This is an error message"}
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_code():
    """Test when exception has a code attribute."""
    class CustomException(BaseException):
        def __init__(self):
            self.code = 500
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_args():
    """Test when exception has args attribute."""
    exception = BaseException("This is an error argument")
    codeflash_output = ErrorMessage._format_plain_reason(exception)


def test_exception_with_no_specific_attributes():
    """Test when exception has no specific attributes."""
    exception = BaseException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_empty_args():
    """Test when exception has empty args attribute."""
    class CustomException(BaseException):
        def __init__(self):
            self.args = ()
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_non_dict_body():
    """Test when exception has a body attribute that is not a dictionary."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = "This is a string, not a dict"
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_body_no_message_key():
    """Test when exception has a body attribute that is a dictionary without a message key."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"error": "No message key here"}
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_large_scale_message():
    """Test the function’s performance and scalability with a large message."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"message": "A" * 10000}
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_large_scale_args():
    """Test the function’s performance and scalability with a large argument."""
    exception = BaseException("A" * 10000)
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_mixed_attributes():
    """Test when exception has multiple relevant attributes."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"message": "Error message"}
            self.code = 404
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_unexpected_attribute_types():
    """Test when exception has attributes with unexpected types."""
    class CustomException(BaseException):
        def __init__(self):
            self.code = [1, 2, 3]
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_special_characters_in_message():
    """Test when exception message contains special characters."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"message": "Error with newline\ncharacter"}
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_non_string_message_in_body():
    """Test when exception has a body attribute with a non-string message value."""
    class CustomException(BaseException):
        def __init__(self):
            self.body = {"message": 404}
    
    exception = CustomException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

from __future__ import annotations

import re
import traceback
from uuid import UUID

# imports
import pytest  # used for our unit tests
from langflow.schema.content_block import ContentBlock
from langflow.schema.content_types import ErrorContent
from langflow.schema.message import ErrorMessage
from langflow.schema.properties import Properties, Source
from pydantic import ValidationError

# unit tests

# Test cases for exceptions with `body` attribute
def test_exception_with_body_message():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={"message": "Error occurred"})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_body_message_and_code():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={"message": "Invalid input", "code": 400})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_empty_body_message():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={"message": ""})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

# Test cases for exceptions with `code` attribute
def test_exception_with_code():
    class CustomException(Exception):
        def __init__(self, code):
            self.code = code
    
    exception = CustomException(code=404)
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_string_code():
    class CustomException(Exception):
        def __init__(self, code):
            self.code = code
    
    exception = CustomException(code="E001")
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_none_code():
    class CustomException(Exception):
        def __init__(self, code):
            self.code = code
    
    exception = CustomException(code=None)
    codeflash_output = ErrorMessage._format_plain_reason(exception)

# Test cases for exceptions with `args` attribute
def test_exception_with_args():
    exception = Exception("File not found")
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_multiple_args():
    exception = Exception("Invalid input", "Extra detail")
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_numeric_args():
    exception = Exception(123)
    codeflash_output = ErrorMessage._format_plain_reason(exception)

# Test cases for ValidationError exceptions


def test_exception_with_no_special_attributes():
    exception = Exception()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_base_exception_with_no_special_attributes():
    exception = BaseException()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_unknown_error_message():
    exception = Exception("Unknown error")
    codeflash_output = ErrorMessage._format_plain_reason(exception)

# Edge cases
def test_exception_with_body_message_none():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={"message": None})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_args_none():
    exception = Exception(None)
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_extra_body_fields():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={"message": "Error occurred", "extra": "detail"})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_empty_body():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    exception = CustomException(body={})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_empty_args():
    exception = Exception()
    exception.args = ()
    codeflash_output = ErrorMessage._format_plain_reason(exception)

# Large scale test cases
def test_exception_with_large_message():
    class CustomException(Exception):
        def __init__(self, body):
            self.body = body
    
    large_message = "A" * 10000
    exception = CustomException(body={"message": large_message})
    codeflash_output = ErrorMessage._format_plain_reason(exception)

def test_exception_with_large_number_of_args():
    large_args = tuple("A" * 10000 for _ in range(1000))
    exception = Exception(*large_args)
    codeflash_output = ErrorMessage._format_plain_reason(exception)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

Codeflash

…6028 (`PlaygroundPage`)

Here's the optimized version of the given Python program.

These changes ensure the program runs faster by reducing redundant operations and optimizing the existing logic to use simpler and efficient patterns.
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Feb 10, 2025
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. python Pull requests that update Python code labels Feb 10, 2025
)

@staticmethod
def _format_markdown_reason(exception: BaseException) -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Incorrect. This should edit the existing _format_markdown_reason method

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

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI python Pull requests that update Python code size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants