Coverage for python/lsst/daf/butler/_deferredDatasetHandle.py: 93%

34 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-19 09:07 +0000

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This software is dual licensed under the GNU General Public License and also 

10# under a 3-clause BSD license. Recipients may choose which of these licenses 

11# to use; please see the files gpl-3.0.txt and/or bsd_license.txt, 

12# respectively. If you choose the GPL option then the following text applies 

13# (but note that there is still no warranty even if you opt for BSD instead): 

14# 

15# This program is free software: you can redistribute it and/or modify 

16# it under the terms of the GNU General Public License as published by 

17# the Free Software Foundation, either version 3 of the License, or 

18# (at your option) any later version. 

19# 

20# This program is distributed in the hope that it will be useful, 

21# but WITHOUT ANY WARRANTY; without even the implied warranty of 

22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

23# GNU General Public License for more details. 

24# 

25# You should have received a copy of the GNU General Public License 

26# along with this program. If not, see <http://www.gnu.org/licenses/>. 

27 

28"""Module containing classes used with deferring dataset loading.""" 

29 

30from __future__ import annotations 

31 

32__all__ = ("DeferredDatasetHandle",) 

33 

34import dataclasses 

35from typing import TYPE_CHECKING, Any 

36 

37if TYPE_CHECKING: 

38 from ._dataset_ref import DatasetRef 

39 from ._limited_butler import LimitedButler 

40 from ._storage_class import StorageClass 

41 from .dimensions import DataCoordinate 

42 

43 

44@dataclasses.dataclass(frozen=True) 

45class DeferredDatasetHandle: 

46 """Proxy class that provides deferred loading of datasets from a butler.""" 

47 

48 def get( 

49 self, 

50 *, 

51 component: str | None = None, 

52 parameters: dict | None = None, 

53 storageClass: str | StorageClass | None = None, 

54 ) -> Any: 

55 """Retrieve the dataset pointed to by this handle. 

56 

57 This handle may be used multiple times, possibly with different 

58 parameters. 

59 

60 Parameters 

61 ---------- 

62 component : `str` or None 

63 If the deferred object is a component dataset type, this parameter 

64 may specify the name of the component to use in the get operation. 

65 The component is looked up in the storage class given when this 

66 object was created, if there was one, and not in the storage class 

67 the dataset was written with. 

68 parameters : `dict` or None 

69 The parameters argument will be passed to the butler get method. 

70 It defaults to None. If the value is not None, this dict will 

71 be merged with the parameters dict used to construct the 

72 `DeferredDatasetHandle` class. 

73 storageClass : `StorageClass` or `str`, optional 

74 The storage class to be used to override the Python type 

75 returned by this method. By default the returned type matches 

76 the dataset type definition for this dataset or the storage 

77 class specified when this object was created. Specifying a 

78 read `StorageClass` can force a different type to be returned. 

79 This type must be compatible with the original type. If 

80 ``component`` is given this overrides the type of the component 

81 rather than that of the composite. 

82 

83 Returns 

84 ------- 

85 return : `Object` 

86 The dataset pointed to by this handle. 

87 """ 

88 if self.parameters is not None: 

89 mergedParameters = self.parameters.copy() 

90 if parameters is not None: 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 mergedParameters.update(parameters) 

92 elif parameters is not None: 

93 mergedParameters = parameters 

94 else: 

95 mergedParameters = {} 

96 

97 if component is None: 

98 # No component so apply the storage class override that was either 

99 # given explicitly or provided to the constructor. 

100 if storageClass is None: 

101 storageClass = self.storageClass 

102 return self.butler.get(self.ref, parameters=mergedParameters, storageClass=storageClass) 

103 

104 # We have a component request. 

105 # The instance storageClass refers to the composite and so should 

106 # not be applied to the component. If set we apply it to the 

107 # requested ref before looking for components. 

108 ref = self.ref 

109 if self.storageClass is not None: 

110 ref = ref.overrideStorageClass(self.storageClass) 

111 

112 # Now the component request goes through the overridden composite. 

113 ref = ref.makeComponentRef(component) 

114 

115 # And run get with the override storage class if given. 

116 return self.butler.get(ref, parameters=mergedParameters, storageClass=storageClass) 

117 

118 @property 

119 def dataId(self) -> DataCoordinate: 

120 """The full data ID associated with the dataset 

121 (`DataCoordinate`). 

122 

123 Guaranteed to contain records. 

124 """ 

125 return self.ref.dataId 

126 

127 butler: LimitedButler 

128 """The butler that will be used to fetch the dataset (`LimitedButler`). 

129 """ 

130 

131 ref: DatasetRef 

132 """Reference to the dataset (`DatasetRef`). 

133 """ 

134 

135 parameters: dict | None 

136 """Optional parameters that may be used to specify a subset of the dataset 

137 to be loaded (`dict` or `None`). 

138 """ 

139 

140 storageClass: str | StorageClass | None = None 

141 """Optional storage class override that can be applied on ``get()``."""