Commit a0cab1a5 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Changed set None to become an unset operation in the metainfo. #455

parent 570a0e6b
Pipeline #89558 passed with stages
in 24 minutes and 6 seconds
......@@ -946,6 +946,12 @@ class MSection(metaclass=MObjectMeta): # TODO find a way to make this a subclas
if quantity_def.derived is not None:
raise MetainfoError('The quantity %s is derived and cannot be set.' % quantity_def)
if value is None:
# This implements the implicit "unset" semantics of assigned None as a
# value
self.__dict__.pop(quantity_def.name, None)
return
if type(quantity_def.type) == np.dtype:
value = self.__to_np(quantity_def, value)
......@@ -2172,6 +2178,11 @@ class PrimitiveQuantity(Quantity):
def __set__(self, obj, value):
obj.m_mod_count += 1
if value is None:
obj.__dict__.pop(self.name, None)
return
if self._list:
if not isinstance(value, list):
if hasattr(value, 'tolist'):
......@@ -2187,7 +2198,7 @@ class PrimitiveQuantity(Quantity):
'The value %s with type %s for quantity %s is not of type %s' %
(value, type(value), self, self.type))
elif value is not None and type(value) != self._type:
elif type(value) != self._type:
raise TypeError(
'The value %s with type %s for quantity %s is not of type %s' %
(value, type(value), self, self.type))
......
......@@ -333,12 +333,23 @@ class TestM1:
assert_section_instance(system)
def test_set_none(self):
def run_test(section, quantity):
assert getattr(section, quantity.name) is not None
assert section.m_is_set(quantity)
for _ in range(0, 2):
setattr(section, quantity.name, None)
assert not section.m_is_set(quantity)
assert getattr(section, quantity.name) is None
assert quantity.name not in section.m_to_dict()
run = Run()
run.code_name = 'test'
assert run.code_name is not None
run_test(run, Run.code_name)
run.code_name = None
assert run.code_name is None
system = System()
system.atom_positions = [[0, 0, 0]]
run_test(system, System.atom_positions)
def test_set_subsection(self):
run = Run()
......@@ -713,7 +724,7 @@ class TestDatatypes:
obj.datetime = None
assert obj.datetime is None
assert obj.m_to_dict()['datetime'] is None
assert 'datetime' not in obj.m_to_dict()
def test_json(self):
class TestSection(MSection):
......@@ -729,7 +740,7 @@ class TestDatatypes:
obj.json = None
assert obj.json is None
assert obj.m_to_dict()['json'] is None
assert 'json' not in obj.m_to_dict()
class TestEnvironment:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment