Skip to content

Commit 71b22d4

Browse files
committed
feat: add deepcopy error
1 parent 36818b1 commit 71b22d4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

scrapegraphai/utils/copy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from typing import Any, Dict, Optional
33
from pydantic.v1 import BaseModel
44

5+
class DeepCopyError(Exception):
6+
"""Custom exception raised when an object cannot be deep-copied."""
7+
pass
58

69
def safe_deepcopy(obj: Any) -> Any:
710
"""
@@ -16,6 +19,8 @@ def safe_deepcopy(obj: Any) -> Any:
1619
Any: A deep copy of the object if possible; otherwise, a shallow
1720
copy if deep copying fails; if neither is possible, the original
1821
object is returned.
22+
Raises:
23+
DeepCopyError: If the object cannot be deep-copied or shallow-copied.
1924
"""
2025

2126
try:
@@ -70,4 +75,4 @@ def safe_deepcopy(obj: Any) -> Any:
7075
try:
7176
return copy.copy(obj)
7277
except (TypeError, AttributeError):
73-
raise TypeError(f"Failed to create a deep copy obj") from e
78+
raise DeepCopyError(f"Cannot deep copy the object of type {type(obj)}") from e

tests/utils/copy_utils_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
# Assuming the custom_deepcopy function is imported or defined above this line
5-
from scrapegraphai.utils.copy import safe_deepcopy
5+
from scrapegraphai.utils.copy import DeepCopyError, safe_deepcopy
66
from pydantic.v1 import BaseModel
77
from pydantic import BaseModel as BaseModelV2
88

@@ -154,15 +154,15 @@ def test_deepcopy_object_without_dict():
154154
assert copy_obj_item is original_item
155155

156156
def test_unhandled_type():
157-
with pytest.raises(TypeError):
157+
with pytest.raises(DeepCopyError):
158158
original = {"origin": NonCopyableObject(10)}
159159
copy_obj = safe_deepcopy(original)
160160

161161
def test_client():
162162
llm_instance_config = {
163163
"model": "moonshot-v1-8k",
164164
"base_url": "https://api.moonshot.cn/v1",
165-
"api_key": "xxx",
165+
"moonshot_api_key": "sk-OWo8hbSubp1QzOPyskOEwXQtZ867Ph0PZWCQdWrc3PH4o0lI",
166166
}
167167

168168
from langchain_community.chat_models.moonshot import MoonshotChat

0 commit comments

Comments
 (0)