Skip to content
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

⚡️ Speed up function _stream_is_misconfigured by 131% #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Dec 20, 2024

📄 131% (1.31x) speedup for _stream_is_misconfigured in src/click/_compat.py

⏱️ Runtime : 66.0 microseconds 28.6 microseconds (best of 108 runs)

📝 Explanation and details
  1. Direct Comparison During Codec Lookup:

Correctness verification report:

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

import codecs
import io
import typing as t

# imports
import pytest  # used for our unit tests
from src.click._compat import _stream_is_misconfigured

# unit tests



def test_no_encoding_attribute():
    """Test streams with no encoding attribute."""
    stream = io.StringIO()
    codeflash_output = _stream_is_misconfigured(stream)





from __future__ import annotations

import codecs
import typing as t
from io import StringIO

# imports
import pytest  # used for our unit tests
from src.click._compat import _stream_is_misconfigured

# unit tests

# Helper class to simulate streams with various encoding attributes
class CustomStream:
    def __init__(self, encoding):
        self.encoding = encoding

@pytest.mark.parametrize("encoding, expected", [
    # Basic Test Cases
    ("ascii", True),  # Explicit ASCII encoding
    ("ASCII", True),  # Explicit ASCII encoding with different case
    ("utf-8", False),  # Non-ASCII encoding
    ("latin-1", False),  # Non-ASCII encoding
    ("utf-16", False),  # Non-ASCII encoding

    # Edge Test Cases
    (None, True),  # No encoding attribute, should default to ASCII
    ("invalid-encoding", False),  # Invalid encoding string
    ("", False),  # Empty string as encoding
    (123, False),  # Non-string encoding (integer)
    (["utf-8"], False),  # Non-string encoding (list)
    (" utf-8 ", False),  # Encoding with leading/trailing whitespace

    # Rare or Unexpected Edge Cases
    (lambda: "ascii", False),  # Encoding as a callable returning "ascii"
    (lambda: "utf-8", False),  # Encoding as a callable returning "utf-8"
    (b"ascii", False),  # Encoding as a byte string
    (b"utf-8", False),  # Encoding as a byte string
    ("utf-8😊", False),  # Non-standard string encoding
    ("utf-\x38", False),  # Encoding with escape sequences
])
def test_stream_is_misconfigured(encoding, expected):
    # Create a custom stream object with the specified encoding
    stream = CustomStream(encoding)
    # Assert that the function returns the expected result
    codeflash_output = _stream_is_misconfigured(stream)

def test_stream_with_property_encoding():
    class StreamWithProperty:
        @property
        def encoding(self):
            return "ascii"
    stream = StreamWithProperty()
    codeflash_output = _stream_is_misconfigured(stream)

    class StreamWithPropertyUTF8:
        @property
        def encoding(self):
            return "utf-8"
    stream = StreamWithPropertyUTF8()
    codeflash_output = _stream_is_misconfigured(stream)

def test_stream_with_dynamic_encoding():
    class StreamWithDynamicEncoding:
        def __init__(self):
            self._toggle = False

        @property
        def encoding(self):
            self._toggle = not self._toggle
            return "ascii" if self._toggle else "utf-8"

    stream = StreamWithDynamicEncoding()

def test_stream_with_custom_object_encoding():
    class CustomObject:
        def __str__(self):
            return "ascii"

    stream = CustomStream(CustomObject())
    codeflash_output = _stream_is_misconfigured(stream)

    class CustomObjectUTF8:
        def __str__(self):
            return "utf-8"

    stream = CustomStream(CustomObjectUTF8())
    codeflash_output = _stream_is_misconfigured(stream)

# Large Scale Test Cases


from src.click._compat import _stream_is_misconfigured
from typing import TextIO

def test__stream_is_misconfigured():
    assert _stream_is_misconfigured(TextIO()) == True

📢 Feedback on this optimization? Discord

1. **Direct Comparison During Codec Lookup:**
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Dec 20, 2024
@codeflash-ai codeflash-ai bot requested a review from alvin-r December 20, 2024 09:43
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants