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 _warn_on_import() by 15% in libs/langchain/langchain/__init__.py #20

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

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented Feb 16, 2024

📄 _warn_on_import() in libs/langchain/langchain/__init__.py

📈 Performance went up by 15% (0.15x faster)

⏱️ Runtime went down from 76.00μs to 66.30μs

Explanation and details

(click to show)

Your python program can be optimized by making a few changes. Instead of importing inside the function, you can place the imports of the required modules outside the function. This will reduce the overhead of import statement every time the function is called and will be done once when the module is imported.

Please have a look at the optimized code.

The is_interactive_env method was not optimized because this method is already optimized.

Correctness verification

The new optimized code was tested for correctness. The results are listed below.

✅ 0 Passed − ⚙️ Existing Unit Tests

✅ 0 Passed − 🎨 Inspired Regression Tests

✅ 7 Passed − 🌀 Generated Regression Tests

(click to show generated tests)
# imports
import pytest  # used for our unit tests
import warnings  # used to capture warnings
from typing import Optional

# function to test
def is_interactive_env() -> bool:
    """Determine if running within IPython or Jupyter."""
    import sys

    return hasattr(sys, "ps2")
from langchain.__init__ import _warn_on_import
# unit tests

# Test that no warning is issued in an interactive environment
def test_no_warning_in_interactive_env(monkeypatch):
    # Patch is_interactive_env to always return True
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: True)
    with pytest.warns(None) as record:
        _warn_on_import('deprecated_module')
    assert len(record) == 0  # No warnings should be recorded

# Test that a warning is issued in a non-interactive environment with a replacement
def test_warning_with_replacement(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(UserWarning) as record:
        _warn_on_import('deprecated_module', 'new_module')
    assert len(record) == 1  # One warning should be recorded
    assert "Please use new_module instead." in str(record[0].message)

# Test that a warning is issued in a non-interactive environment without a replacement
def test_warning_without_replacement(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(UserWarning) as record:
        _warn_on_import('deprecated_module')
    assert len(record) == 1  # One warning should be recorded
    assert "is no longer supported." in str(record[0].message)

# Test that no warning is issued when the module name is an empty string
def test_no_warning_with_empty_name(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(None) as record:
        _warn_on_import('')
    assert len(record) == 0  # No warnings should be recorded

# Test that no warning is issued when both module name and replacement are empty strings
def test_no_warning_with_empty_name_and_replacement(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(None) as record:
        _warn_on_import('', '')
    assert len(record) == 0  # No warnings should be recorded

# Test that no warning is issued when the module name is None
def test_no_warning_with_none_name(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.raises(TypeError):
        _warn_on_import(None)  # TypeError should be raised because name is None

# Test that a warning is issued with correct message content when replacement is provided
def test_warning_message_content_with_replacement(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(UserWarning) as record:
        _warn_on_import('deprecated_module', 'new_module')
    assert "Importing deprecated_module from langchain root module is no longer supported. Please use new_module instead." in str(record[0].message)

# Test that a warning is issued with correct message content without replacement
def test_warning_message_content_without_replacement(monkeypatch):
    # Patch is_interactive_env to always return False
    monkeypatch.setattr('langchain.utils.interactive_env.is_interactive_env', lambda: False)
    with pytest.warns(UserWarning) as record:
        _warn_on_import('deprecated_module')
    assert "Importing deprecated_module from langchain root module is no longer supported." in str(record[0].message)

@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by CodeFlash AI label Feb 16, 2024
@codeflash-ai codeflash-ai bot requested a review from aphexcx February 16, 2024 04:46
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