Skip to content

Commit

Permalink
feat: add deepcopy error
Browse files Browse the repository at this point in the history
  • Loading branch information
goasleep committed Aug 31, 2024
1 parent 36818b1 commit 71b22d4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion scrapegraphai/utils/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
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:
"""
Expand All @@ -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:
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions tests/utils/copy_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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

Expand Down Expand Up @@ -154,15 +154,15 @@ 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)

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
Expand Down

0 comments on commit 71b22d4

Please sign in to comment.