Skip to content

Commit

Permalink
Add StorageClassDelegate.copy() method
Browse files Browse the repository at this point in the history
This allows for a future where in-memory datastore can decide
whether to do copying when returning an object.
  • Loading branch information
timj committed Apr 19, 2023
1 parent 634aed3 commit d563741
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
38 changes: 38 additions & 0 deletions python/lsst/daf/butler/core/storageClassDelegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@
__all__ = ("DatasetComponent", "StorageClassDelegate")

import collections.abc
import copy
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Dict, Iterable, Mapping, Optional, Set, Tuple, Type

from lsst.utils.introspection import get_full_type_name

if TYPE_CHECKING:
from .storageClass import StorageClass

Expand Down Expand Up @@ -396,3 +399,38 @@ def selectResponsibleComponent(cls, derivedComponent: str, fromComponents: Set[O
from the supplied options.
"""
raise NotImplementedError("This delegate does not support derived components")

def copy(self, inMemoryDataset: Any) -> Any:
"""Copy the supplied python type and return the copy.
Parameters
----------
inMemoryDataset : `object`
Object to copy.
Returns
-------
copied : `object`
A copy of the supplied object. Can be the same object if the
object is known to be read-only.
Raises
------
NotImplementedError
Raised if none of the default methods for copying work.
Notes
-----
The default implementation uses `copy.deepcopy()`.
It is generally expected that this method is the equivalent of a deep
copy. Subclasses can override this method if they already know the
optimal approach for deep copying.
"""

try:
return copy.deepcopy(inMemoryDataset)
except Exception as e:

Check warning on line 432 in python/lsst/daf/butler/core/storageClassDelegate.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/core/storageClassDelegate.py#L432

Added line #L432 was not covered by tests
raise NotImplementedError(
f"Unable to deep copy the supplied python type ({get_full_type_name(inMemoryDataset)}) "
f"using default methods ({e})"
)
7 changes: 7 additions & 0 deletions tests/test_storageClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ def testCreation(self):
# Ensure that we have a delegate
self.assertIsInstance(scc.delegate(), StorageClassDelegate)

# Check that delegate copy() works.
list1 = [1, 2, 3]
list2 = scc.delegate().copy(list1)
self.assertEqual(list1, list2)
list2.append(4)
self.assertNotEqual(list1, list2)

# Check we can create a storageClass using the name of an importable
# type.
sc2 = StorageClass("TestImage2", "lsst.daf.butler.core.storageClass.StorageClassFactory")
Expand Down

0 comments on commit d563741

Please sign in to comment.