Skip to content

Commit 31e21d0

Browse files
authored
Merge pull request #445 from andlaus/deepcopy_cleanup
remove unnecessary `__deepcopy__()` methods
2 parents 30b3316 + eaae3d5 commit 31e21d0

File tree

8 files changed

+8
-141
lines changed

8 files changed

+8
-141
lines changed

odxtools/diaglayers/basevariant.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-License-Identifier: MIT
22
from collections.abc import Iterable
3-
from copy import deepcopy
43
from dataclasses import dataclass
5-
from typing import Any, cast
4+
from typing import cast
65
from xml.etree import ElementTree
76

87
from typing_extensions import override
@@ -82,23 +81,6 @@ def __post_init__(self) -> None:
8281
"The raw diagnostic layer passed to BaseVariant "
8382
"must be a BaseVariantRaw")
8483

85-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
86-
"""Create a deep copy of the base variant
87-
88-
Note that the copied diagnostic layer is not fully
89-
initialized, so `_finalize_init()` should to be called on it
90-
before it can be used normally.
91-
"""
92-
93-
result = super().__deepcopy__(memo)
94-
95-
# note that the self.base_variant_raw object is *not* copied at
96-
# this place because the attribute points to the same object
97-
# as self.diag_layer_raw.
98-
result.base_variant_raw = deepcopy(self.base_variant_raw, memo)
99-
100-
return result
101-
10284
@override
10385
def _compute_value_inheritance(self, odxlinks: OdxLinkDatabase) -> None:
10486
super()._compute_value_inheritance(odxlinks)

odxtools/diaglayers/diaglayer.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: MIT
22
from collections.abc import Callable, Iterable
3-
from copy import copy, deepcopy
3+
from copy import copy
44
from dataclasses import dataclass
55
from functools import cached_property
66
from itertools import chain
@@ -166,21 +166,6 @@ def _compute_available_objects(
166166
"""
167167
return get_local_objects(self)
168168

169-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
170-
"""Create a deep copy of the diagnostic layer
171-
172-
Note that the copied diagnostic layer is not fully
173-
initialized, so `_finalize_init()` should to be called on it
174-
before it can be used normally.
175-
"""
176-
cls = self.__class__
177-
result = cls.__new__(cls)
178-
memo[id(self)] = result
179-
180-
result.diag_layer_raw = deepcopy(self.diag_layer_raw, memo)
181-
182-
return result
183-
184169
#####
185170
# <convenience functionality>
186171
#####

odxtools/diaglayers/ecushareddata.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: MIT
2-
from copy import deepcopy
32
from dataclasses import dataclass
4-
from typing import TYPE_CHECKING, Any, cast
3+
from typing import TYPE_CHECKING, cast
54
from xml.etree import ElementTree
65

76
from ..diagvariable import DiagVariable
@@ -77,20 +76,3 @@ def _finalize_init(self, database: "Database", odxlinks: OdxLinkDatabase) -> Non
7776
context.diag_layer = self
7877
self._resolve_snrefs(context)
7978
context.diag_layer = None
80-
81-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
82-
"""Create a deep copy of the protocol layer
83-
84-
Note that the copied diagnostic layer is not fully
85-
initialized, so `_finalize_init()` should to be called on it
86-
before it can be used normally.
87-
"""
88-
89-
result = super().__deepcopy__(memo)
90-
91-
# note that the self.ecu_shared_data_raw object is *not* copied at
92-
# this place because the attribute points to the same object
93-
# as self.diag_layer_raw.
94-
result.ecu_shared_data_raw = deepcopy(self.ecu_shared_data_raw, memo)
95-
96-
return result

odxtools/diaglayers/ecuvariant.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-License-Identifier: MIT
22
from collections.abc import Iterable
3-
from copy import deepcopy
43
from dataclasses import dataclass
5-
from typing import Any, cast
4+
from typing import cast
65
from xml.etree import ElementTree
76

87
from typing_extensions import override
@@ -82,23 +81,6 @@ def __post_init__(self) -> None:
8281
"The raw diagnostic layer passed to EcuVariant "
8382
"must be a EcuVariantRaw")
8483

85-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
86-
"""Create a deep copy of the ECU variant
87-
88-
Note that the copied diagnostic layer is not fully
89-
initialized, so `_finalize_init()` should to be called on it
90-
before it can be used normally.
91-
"""
92-
93-
result = super().__deepcopy__(memo)
94-
95-
# note that the self.ecu_variant_raw object is *not* copied at
96-
# this place because the attribute points to the same object
97-
# as self.diag_layer_raw.
98-
result.ecu_variant_raw = deepcopy(self.ecu_variant_raw, memo)
99-
100-
return result
101-
10284
@override
10385
def _compute_value_inheritance(self, odxlinks: OdxLinkDatabase) -> None:
10486
super()._compute_value_inheritance(odxlinks)

odxtools/diaglayers/functionalgroup.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-License-Identifier: MIT
22
from collections.abc import Iterable
3-
from copy import deepcopy
43
from dataclasses import dataclass
5-
from typing import Any, cast
4+
from typing import cast
65
from xml.etree import ElementTree
76

87
from typing_extensions import override
@@ -92,20 +91,3 @@ def not_inherited_fn(parent_ref: ParentRef) -> list[str]:
9291
return []
9392

9493
return self._compute_available_objects(get_local_objects_fn, not_inherited_fn)
95-
96-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
97-
"""Create a deep copy of the functional group layer
98-
99-
Note that the copied diagnostic layer is not fully
100-
initialized, so `_finalize_init()` should to be called on it
101-
before it can be used normally.
102-
"""
103-
104-
result = super().__deepcopy__(memo)
105-
106-
# note that the self.functional_group_raw object is *not* copied at
107-
# this place because the attribute points to the same object
108-
# as self.diag_layer_raw.
109-
result.functional_group_raw = deepcopy(self.functional_group_raw, memo)
110-
111-
return result

odxtools/diaglayers/hierarchyelement.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import re
33
import warnings
44
from collections.abc import Callable, Iterable
5-
from copy import deepcopy
65
from dataclasses import dataclass
76
from functools import cached_property
87
from typing import TYPE_CHECKING, Any, TypeVar, Union, cast
@@ -73,23 +72,6 @@ def _resolve_odxlinks(self, odxlinks: OdxLinkDatabase) -> None:
7372
def _resolve_snrefs(self, context: SnRefContext) -> None:
7473
super()._resolve_snrefs(context)
7574

76-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
77-
"""Create a deep copy of the hierarchy element
78-
79-
Note that the copied diagnostic layer is not fully
80-
initialized, so `_finalize_init()` should to be called on it
81-
before it can be used normally.
82-
"""
83-
84-
new_he = super().__deepcopy__(memo)
85-
86-
# note that the self.hierarchy_element_raw object is *not*
87-
# copied at this place because the attribute points to the
88-
# same object as self.diag_layer_raw.
89-
new_he.hierarchy_element_raw = deepcopy(self.hierarchy_element_raw)
90-
91-
return new_he
92-
9375
def _finalize_init(self, database: "Database", odxlinks: OdxLinkDatabase) -> None:
9476
"""This method deals with everything inheritance related and
9577
-- after the final set of objects covered by the diagnostic

odxtools/diaglayers/protocol.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# SPDX-License-Identifier: MIT
2-
from copy import deepcopy
32
from dataclasses import dataclass
4-
from typing import Any, cast
3+
from typing import cast
54
from xml.etree import ElementTree
65

76
from ..comparamspec import ComparamSpec
@@ -59,20 +58,3 @@ def __post_init__(self) -> None:
5958
isinstance(self.diag_layer_raw, ProtocolRaw),
6059
"The raw diagnostic layer passed to Protocol "
6160
"must be a ProtocolRaw")
62-
63-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
64-
"""Create a deep copy of the protocol layer
65-
66-
Note that the copied diagnostic layer is not fully
67-
initialized, so `_finalize_init()` should to be called on it
68-
before it can be used normally.
69-
"""
70-
71-
result = super().__deepcopy__(memo)
72-
73-
# note that the self.protocol_raw object is *not* copied at
74-
# this place because the attribute points to the same object
75-
# as self.diag_layer_raw.
76-
result.protocol_raw = deepcopy(self.protocol_raw, memo)
77-
78-
return result

odxtools/nameditemlist.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import abc
33
import typing
44
from collections.abc import Collection, Iterable
5-
from copy import deepcopy
65
from keyword import iskeyword
76
from typing import Any, SupportsIndex, TypeVar, overload, runtime_checkable
87

@@ -177,20 +176,11 @@ def __repr__(self) -> str:
177176
return f"{type(self).__name__}([{', '.join([repr(x) for x in self])}])"
178177

179178
def __copy__(self) -> Any:
180-
return self.__class__(list(self))
181-
182-
def __deepcopy__(self, memo: dict[int, Any]) -> Any:
183-
cls = self.__class__
184-
result = cls.__new__(cls)
185-
memo[id(self)] = result
186-
result._item_dict = {}
187-
for x in self:
188-
result.append(deepcopy(x, memo))
189-
190-
return result
179+
return self.__class__(self)
191180

192181
def __reduce__(self) -> tuple[Any, ...]:
193182
"""Support for Python's pickle protocol.
183+
194184
This method ensures that the object can be reconstructed with its current state,
195185
using its class and the list of items it contains.
196186
It returns a tuple containing the reconstruction function (the class)

0 commit comments

Comments
 (0)