Skip to content

Commit 150e85f

Browse files
committed
correctly handle None value in setters
1 parent 793290f commit 150e85f

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

pystac/extensions/file.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ def apply(self,
6565
self.nodata = nodata
6666
self.checksum = checksum
6767

68+
def _set_property(self, key, value, asset):
69+
target = self.item.properties if asset is None else asset.properties
70+
if value is None:
71+
target.pop(key, None)
72+
else:
73+
target[key] = value
74+
6875
@property
6976
def data_type(self):
7077
"""Get or sets the data_type of the file.
@@ -90,7 +97,7 @@ def get_data_type(self, asset=None):
9097
if asset is not None and 'file:data_type' in asset.properties:
9198
data_type = asset.properties.get('file:data_type')
9299
else:
93-
data_type = self.item.properties.get('file_data_type')
100+
data_type = self.item.properties.get('file:data_type')
94101

95102
if data_type is not None:
96103
return FileDataType(data_type)
@@ -101,10 +108,7 @@ def set_data_type(self, data_type, asset=None):
101108
If an Asset is supplied, sets the property on the Asset.
102109
Otherwise sets the Item's value.
103110
"""
104-
if asset is not None:
105-
asset.properties['file:data_type'] = data_type.value
106-
else:
107-
self.item.properties['file:data_type'] = data_type.value
111+
self._set_property('file:data_type', data_type.value, asset)
108112

109113
@property
110114
def size(self):
@@ -139,10 +143,7 @@ def set_size(self, size, asset=None):
139143
If an Asset is supplied, sets the property on the Asset.
140144
Otherwise sets the Item's value.
141145
"""
142-
if asset is None:
143-
self.item.properties['file:size'] = size
144-
else:
145-
asset.properties['file:size'] = size
146+
self._set_property('file:size', size, asset)
146147

147148
@property
148149
def nodata(self):
@@ -177,10 +178,7 @@ def set_nodata(self, nodata, asset=None):
177178
If an Asset is supplied, sets the property on the Asset.
178179
Otherwise sets the Item's value.
179180
"""
180-
if asset is None:
181-
self.item.properties['file:nodata'] = nodata
182-
else:
183-
asset.properties['file:nodata'] = nodata
181+
self._set_property('file:nodata', nodata, asset)
184182

185183
@property
186184
def checksum(self):
@@ -191,7 +189,7 @@ def checksum(self):
191189
"""
192190
return self.get_checksum()
193191

194-
@nodata.setter
192+
@checksum.setter
195193
def checksum(self, v):
196194
self.set_checksum(v)
197195

@@ -215,10 +213,7 @@ def set_checksum(self, checksum, asset=None):
215213
If an Asset is supplied, sets the property on the Asset.
216214
Otherwise sets the Item's value.
217215
"""
218-
if asset is None:
219-
self.item.properties['file:checksum'] = checksum
220-
else:
221-
asset.properties['file:checksum'] = checksum
216+
self._set_property('file:checksum', checksum, asset)
222217

223218
def __repr__(self):
224219
return '<FileItemExt Item id={}>'.format(self.item.id)

0 commit comments

Comments
 (0)