From 6e3828e0b0eced0ae4a42b2534748cf1b005a4aa Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Mon, 15 Jul 2024 09:32:37 -0500 Subject: [PATCH] conditionally import deepdiff (#164) only import deepdiff when needed --- dbt_common/record.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dbt_common/record.py b/dbt_common/record.py index 8fe068bb..b33d4b5a 100644 --- a/dbt_common/record.py +++ b/dbt_common/record.py @@ -10,7 +10,6 @@ import json import os -from deepdiff import DeepDiff # type: ignore from enum import Enum from typing import Any, Callable, Dict, List, Mapping, Optional, Type @@ -52,6 +51,11 @@ def from_dict(cls, dct: Mapping) -> "Record": class Diff: def __init__(self, current_recording_path: str, previous_recording_path: str) -> None: + # deepdiff is expensive to import, so we only do it here when we need it + from deepdiff import DeepDiff # type: ignore + + self.diff = DeepDiff + self.current_recording_path = current_recording_path self.previous_recording_path = previous_recording_path @@ -67,7 +71,7 @@ def diff_query_records(self, current: List, previous: List) -> Dict[str, Any]: if previous[i].get("result").get("table") is not None: previous[i]["result"]["table"] = json.loads(previous[i]["result"]["table"]) - return DeepDiff(previous, current, ignore_order=True, verbose_level=2) + return self.diff(previous, current, ignore_order=True, verbose_level=2) def diff_env_records(self, current: List, previous: List) -> Dict[str, Any]: # The mode and filepath may change. Ignore them. @@ -77,12 +81,12 @@ def diff_env_records(self, current: List, previous: List) -> Dict[str, Any]: "root[0]['result']['env']['DBT_RECORDER_MODE']", ] - return DeepDiff( + return self.diff( previous, current, ignore_order=True, verbose_level=2, exclude_paths=exclude_paths ) def diff_default(self, current: List, previous: List) -> Dict[str, Any]: - return DeepDiff(previous, current, ignore_order=True, verbose_level=2) + return self.diff(previous, current, ignore_order=True, verbose_level=2) def calculate_diff(self) -> Dict[str, Any]: with open(self.current_recording_path) as current_recording: