mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-06-28 21:01:55 +08:00
feat: add deepcopy error
This commit is contained in:
parent
36818b1fb3
commit
71b22d4880
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user