Skip to content

Python scraper based on AI

feat: add deepcopy error

smith peng71b22d48804c
diff --git a/scrapegraphai/utils/copy.py b/scrapegraphai/utils/copy.py
index e1fdd37f..4f77f947 100644
--- a/scrapegraphai/utils/copy.py
+++ b/scrapegraphai/utils/copy.py
@@ -2,6 +2,9 @@ import copy
from typing import Any, Dict, Optional
from pydantic.v1 import BaseModel
+class DeepCopyError(Exception):
+ """Custom exception raised when an object cannot be deep-copied."""
+ pass
def safe_deepcopy(obj: Any) -> Any:
"""
@@ -16,6 +19,8 @@ def safe_deepcopy(obj: Any) -> Any:
Any: A deep copy of the object if possible; otherwise, a shallow
copy if deep copying fails; if neither is possible, the original
object is returned.
+ Raises:
+ DeepCopyError: If the object cannot be deep-copied or shallow-copied.
"""
try:
@@ -70,4 +75,4 @@ def safe_deepcopy(obj: Any) -> Any:
try:
return copy.copy(obj)
except (TypeError, AttributeError):
- raise TypeError(f"Failed to create a deep copy obj") from e
+ raise DeepCopyError(f"Cannot deep copy the object of type {type(obj)}") from e
diff --git a/tests/utils/copy_utils_test.py b/tests/utils/copy_utils_test.py
index 8fb5a804..3cb1d5fb 100644
--- a/tests/utils/copy_utils_test.py
+++ b/tests/utils/copy_utils_test.py
@@ -2,7 +2,7 @@ import copy
import pytest
# Assuming the custom_deepcopy function is imported or defined above this line
-from scrapegraphai.utils.copy import safe_deepcopy
+from scrapegraphai.utils.copy import DeepCopyError, safe_deepcopy
from pydantic.v1 import BaseModel
from pydantic import BaseModel as BaseModelV2
@@ -154,7 +154,7 @@ def test_deepcopy_object_without_dict():
assert copy_obj_item is original_item
def test_unhandled_type():
- with pytest.raises(TypeError):
+ with pytest.raises(DeepCopyError):
original = {"origin": NonCopyableObject(10)}
copy_obj = safe_deepcopy(original)
@@ -162,7 +162,7 @@ def test_client():
llm_instance_config = {
"model": "moonshot-v1-8k",
"base_url": "https://api.moonshot.cn/v1",
- "api_key": "xxx",
+ "moonshot_api_key": "sk-OWo8hbSubp1QzOPyskOEwXQtZ867Ph0PZWCQdWrc3PH4o0lI",
}
from langchain_community.chat_models.moonshot import MoonshotChat