|
1 | 1 | import copy |
2 | 2 | from typing import Any, Dict, Optional |
| 3 | +from pydantic.v1 import BaseModel |
3 | 4 |
|
4 | 5 |
|
5 | | -def safe_deepcopy(obj: Any, memo: Optional[Dict[int, Any]] = None) -> Any: |
| 6 | +def safe_deepcopy(obj: Any) -> Any: |
6 | 7 | """ |
7 | 8 | Attempts to create a deep copy of the object using `copy.deepcopy` |
8 | 9 | whenever possible. If that fails, it falls back to custom deep copy |
9 | 10 | logic or returns the original object. |
10 | 11 |
|
11 | 12 | Args: |
12 | 13 | obj (Any): The object to be copied, which can be of any type. |
13 | | - memo (Optional[Dict[int, Any]]): A dictionary used to track objects |
14 | | - that have already been copied to handle circular references. |
15 | | - If None, a new dictionary is created. |
16 | 14 |
|
17 | 15 | Returns: |
18 | 16 | Any: A deep copy of the object if possible; otherwise, a shallow |
19 | 17 | copy if deep copying fails; if neither is possible, the original |
20 | 18 | object is returned. |
21 | 19 | """ |
22 | 20 |
|
23 | | - if memo is None: |
24 | | - memo = {} |
25 | | - |
26 | | - if id(obj) in memo: |
27 | | - return memo[id(obj)] |
28 | | - |
29 | 21 | try: |
| 22 | + |
30 | 23 | # Try to use copy.deepcopy first |
31 | | - return copy.deepcopy(obj, memo) |
32 | | - except (TypeError, AttributeError): |
| 24 | + if isinstance(obj,BaseModel): |
| 25 | + # handle BaseModel because __fields_set__ need compatibility |
| 26 | + copied_obj = obj.copy(deep=True) |
| 27 | + else: |
| 28 | + copied_obj = copy.deepcopy(obj) |
| 29 | + |
| 30 | + return copied_obj |
| 31 | + except (TypeError, AttributeError) as e: |
33 | 32 | # If deepcopy fails, handle specific types manually |
34 | 33 |
|
35 | 34 | # Handle dictionaries |
36 | 35 | if isinstance(obj, dict): |
37 | 36 | new_obj = {} |
38 | | - memo[id(obj)] = new_obj |
| 37 | + |
39 | 38 | for k, v in obj.items(): |
40 | | - new_obj[k] = safe_deepcopy(v, memo) |
| 39 | + new_obj[k] = safe_deepcopy(v) |
41 | 40 | return new_obj |
42 | 41 |
|
43 | 42 | # Handle lists |
44 | 43 | elif isinstance(obj, list): |
45 | 44 | new_obj = [] |
46 | | - memo[id(obj)] = new_obj |
| 45 | + |
47 | 46 | for v in obj: |
48 | | - new_obj.append(safe_deepcopy(v, memo)) |
| 47 | + new_obj.append(safe_deepcopy(v)) |
49 | 48 | return new_obj |
50 | 49 |
|
51 | 50 | # Handle tuples (immutable, but might contain mutable objects) |
52 | 51 | elif isinstance(obj, tuple): |
53 | | - new_obj = tuple(safe_deepcopy(v, memo) for v in obj) |
54 | | - memo[id(obj)] = new_obj |
| 52 | + new_obj = tuple(safe_deepcopy(v) for v in obj) |
| 53 | + |
55 | 54 | return new_obj |
56 | 55 |
|
57 | 56 | # Handle frozensets (immutable, but might contain mutable objects) |
58 | 57 | elif isinstance(obj, frozenset): |
59 | | - new_obj = frozenset(safe_deepcopy(v, memo) for v in obj) |
60 | | - memo[id(obj)] = new_obj |
| 58 | + new_obj = frozenset(safe_deepcopy(v) for v in obj) |
61 | 59 | return new_obj |
62 | 60 |
|
63 | 61 | # Handle objects with attributes |
64 | 62 | elif hasattr(obj, "__dict__"): |
65 | 63 | new_obj = obj.__new__(obj.__class__) |
66 | 64 | for attr in obj.__dict__: |
67 | | - setattr(new_obj, attr, safe_deepcopy(getattr(obj, attr), memo)) |
68 | | - memo[id(obj)] = new_obj |
| 65 | + setattr(new_obj, attr, safe_deepcopy(getattr(obj, attr))) |
| 66 | + |
69 | 67 | return new_obj |
70 | | - |
| 68 | + |
71 | 69 | # Attempt shallow copy as a fallback |
72 | 70 | try: |
73 | 71 | return copy.copy(obj) |
74 | 72 | except (TypeError, AttributeError): |
75 | | - pass |
76 | | - |
77 | | - # If all else fails, return the original object |
78 | | - return obj |
| 73 | + raise TypeError(f"Failed to create a deep copy obj") from e |
0 commit comments