Skip to content

Commit 6252fa4

Browse files
committed
Refactor dependency installation in test workflow and improve module imports. Streamlined package installation process and added lazy loading for imports to avoid cyclic dependencies. Enhanced error handling for missing requests library.
1 parent 1111e82 commit 6252fa4

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,13 @@ jobs:
4444
- name: Install dependencies
4545
run: |
4646
python -m pip install --upgrade pip
47-
pip install pytest requests || { echo 'Failed to install test dependencies'; exit 1; }
47+
pip install pytest || { echo 'Failed to install pytest'; exit 1; }
4848
49-
- name: Install package dependencies (pre-installation)
49+
- name: Install package and dependencies
5050
run: |
51-
# Installing dependencies before the package to avoid build errors
52-
pip install requests anthropic openai langchain langchain-core langchain-openai langchain-anthropic || { echo 'Failed to install dependencies'; exit 1; }
51+
# Install package and all its dependencies including optional ones
52+
pip install -e ".[all]" requests || { echo 'Failed to install package'; exit 1; }
5353
54-
- name: Install package
55-
run: |
56-
pip install -e . || { echo 'Failed to install editable package'; exit 1; }
57-
5854
- name: List installed packages
5955
run: pip list
6056

osmosis_wrap/__init__.py

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,55 @@
3434
- LangChain (LLMs, Chat Models, and Prompt Templates)
3535
"""
3636

37-
from . import utils
38-
from . import consts
39-
from .adapters import anthropic, openai
40-
from .logger import logger, reconfigure_logger
37+
# Import consts directly to avoid cyclic imports
38+
from .consts import package_name, package_version, indent
4139

42-
# Re-export configuration flags for easy access
43-
enabled = utils.enabled
44-
use_stderr = utils.use_stderr
45-
pretty_print = utils.pretty_print
46-
print_messages = utils.print_messages
47-
indent = consts.indent
48-
49-
# Re-export initialization function
50-
init = utils.init
51-
52-
# Export adapter functions
53-
wrap_anthropic = anthropic.wrap_anthropic
54-
wrap_openai = openai.wrap_openai
55-
56-
# Export disable and enable functions
57-
disable_hoover = utils.disable_hoover
58-
enable_hoover = utils.enable_hoover
59-
60-
# Automatically apply patches when the module is imported
61-
wrap_anthropic()
62-
wrap_openai()
63-
64-
# Conditionally load and apply LangChain wrapper
65-
try:
66-
from .adapters import langchain
67-
wrap_langchain = langchain.wrap_langchain
68-
# Apply the wrapper, but don't fail if it doesn't work
40+
# Use lazy imports to avoid importing modules during setup
41+
def _import_modules():
42+
global utils, logger, reconfigure_logger, anthropic, openai, langchain
43+
global enabled, use_stderr, pretty_print, print_messages
44+
global init, wrap_anthropic, wrap_openai, disable_hoover, enable_hoover, wrap_langchain
45+
46+
from . import utils
47+
from .logger import logger, reconfigure_logger
48+
from .adapters import anthropic, openai
49+
50+
# Re-export configuration flags for easy access
51+
enabled = utils.enabled
52+
use_stderr = utils.use_stderr
53+
pretty_print = utils.pretty_print
54+
print_messages = utils.print_messages
55+
56+
# Re-export initialization function
57+
init = utils.init
58+
59+
# Export adapter functions
60+
wrap_anthropic = anthropic.wrap_anthropic
61+
wrap_openai = openai.wrap_openai
62+
63+
# Export disable and enable functions
64+
disable_hoover = utils.disable_hoover
65+
enable_hoover = utils.enable_hoover
66+
67+
# Automatically apply patches when the module is imported
68+
wrap_anthropic()
69+
wrap_openai()
70+
71+
# Conditionally load and apply LangChain wrapper
6972
try:
70-
wrap_langchain()
71-
except Exception as e:
72-
logger.warning(f"Failed to wrap LangChain: {str(e)}")
73-
except ImportError:
74-
# LangChain not installed or not compatible
75-
def wrap_langchain():
76-
logger.warning("LangChain support not available. Install LangChain to use this feature.")
73+
from .adapters import langchain
74+
wrap_langchain = langchain.wrap_langchain
75+
# Apply the wrapper, but don't fail if it doesn't work
76+
try:
77+
wrap_langchain()
78+
except Exception as e:
79+
logger.warning(f"Failed to wrap LangChain: {str(e)}")
80+
except ImportError:
81+
# LangChain not installed or not compatible
82+
def wrap_langchain():
83+
logger.warning("LangChain support not available. Install LangChain to use this feature.")
84+
85+
# Initialize the module on first import, but not during installation
86+
import sys
87+
if 'pip' not in sys.modules:
88+
_import_modules()

osmosis_wrap/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
import json
66
import sys
7-
import requests
87
from datetime import datetime, timezone
9-
from typing import Any, Dict
8+
from typing import Any, Dict, Optional
109

1110
# Import constants
1211
from .consts import hoover_api_url
@@ -57,6 +56,9 @@ def send_to_hoover(query: Dict[str, Any], response: Dict[str, Any], status: int
5756
return
5857

5958
try:
59+
# Import requests only when needed
60+
import requests
61+
6062
# Create headers
6163
headers = {
6264
"Content-Type": "application/json",
@@ -82,5 +84,7 @@ def send_to_hoover(query: Dict[str, Any], response: Dict[str, Any], status: int
8284
if response_data.status_code != 200:
8385
logger.warning(f"Hoover API returned status {response_data.status_code} for data")
8486

87+
except ImportError:
88+
logger.warning("Requests library not installed. Please install it with 'pip install requests'.")
8589
except Exception as e:
8690
logger.warning(f"Failed to send data to Hoover API: {str(e)}")

0 commit comments

Comments
 (0)