From 1fa8ac9550a251005878131a7a8ac0113cd5f3e3 Mon Sep 17 00:00:00 2001 From: Casey Jao Date: Sat, 25 May 2024 08:14:09 -0400 Subject: [PATCH] Delay Sqlalchemy import in DB trigger Since Sqlalchemy is only used server-side, it's an extraneous dependency of the SDK-only install --- covalent/triggers/database_trigger.py | 9 ++++++--- tests/covalent_tests/triggers/database_trigger_test.py | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/covalent/triggers/database_trigger.py b/covalent/triggers/database_trigger.py index d2a53402e..6baa0141c 100644 --- a/covalent/triggers/database_trigger.py +++ b/covalent/triggers/database_trigger.py @@ -19,9 +19,6 @@ from threading import Event from typing import List -from sqlalchemy import create_engine -from sqlalchemy.orm import Session - from covalent._shared_files import logger from .base import BaseTrigger @@ -88,6 +85,12 @@ def observe(self) -> None: where conditions are met or until stop has being called """ + # Since these modules are only used server-side, delay their + # imports to avoid introducing a sqlalchemy requirement to + # SDK-only installs + from sqlalchemy import create_engine + from sqlalchemy.orm import Session + app_log.debug("Inside DatabaseTrigger's observe") event_count = 0 diff --git a/tests/covalent_tests/triggers/database_trigger_test.py b/tests/covalent_tests/triggers/database_trigger_test.py index 734eb2e06..8927417be 100644 --- a/tests/covalent_tests/triggers/database_trigger_test.py +++ b/tests/covalent_tests/triggers/database_trigger_test.py @@ -61,8 +61,8 @@ def test_database_trigger_observe(mocker, where_clauses, database_trigger): database_trigger.where_clauses = where_clauses database_trigger.trigger = mocker.MagicMock() - mock_db_engine = mocker.patch("covalent.triggers.database_trigger.create_engine") - mock_session = mocker.patch("covalent.triggers.database_trigger.Session") + mock_db_engine = mocker.patch("sqlalchemy.create_engine") + mock_session = mocker.patch("sqlalchemy.orm.Session") mock_event = mocker.patch("covalent.triggers.database_trigger.Event") mock_sleep = mocker.patch("covalent.triggers.database_trigger.time.sleep")