Commit 50ae1617 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed reference assignment for references defined with ProxySection.

parent 961de290
Pipeline #79125 canceled with stages
in 20 minutes and 5 seconds
import json
from nomad.datamodel.metainfo import public
# A simple example that demonstrates how to set references
run = public.section_run()
scc = run.m_create(public.section_single_configuration_calculation)
system = run.m_create(public.section_system)
scc.single_configuration_calculation_to_system_ref = system
assert scc.single_configuration_calculation_to_system_ref == system
print(json.dumps(run.m_to_dict(), indent=2))
......@@ -133,7 +133,7 @@ class SectionProxy(MProxy):
def m_proxy_resolve(self):
if self.m_proxy_section and not self.m_proxy_resolved:
root = self.m_proxy_section
while root is not None and not isinstance(root, Package):
while root.m_parent is not None and not isinstance(root, Package):
root = root.m_parent
if isinstance(root, Package):
......@@ -344,7 +344,7 @@ class Reference(DataType):
def set_normalize(self, section: 'MSection', quantity_def: 'Quantity', value: Any) -> Any:
if isinstance(self.target_section_def, MProxy):
proxy = self.target_section_def
proxy.m_proxy_section = section
proxy.m_proxy_section = section.m_def
proxy.m_proxy_quantity = quantity_def
self.target_section_def = proxy.m_proxy_resolve()
......@@ -34,6 +34,7 @@ class Normalizer(metaclass=ABCMeta):
def __init__(self, backend: Backend) -> None:
self._backend = backend
self.entry_archive = backend.entry_archive
self.section_run = backend.entry_archive.section_run[0]
except (AttributeError, IndexError):
......@@ -20,7 +20,7 @@ import datetime
from nomad.metainfo.metainfo import (
MSection, MCategory, Section, Quantity, SubSection, Definition, Package, DeriveError,
MetainfoError, Environment, MResource, Datetime, Annotation, SectionAnnotation,
DefinitionAnnotation, Reference, MProxy, derived)
DefinitionAnnotation, Reference, MProxy, derived, SectionProxy)
from nomad.metainfo.example import Run, VaspRun, System, SystemHash, Parsing, SCC, m_package as example_package
from nomad import utils
from nomad.units import ureg
......@@ -585,6 +585,24 @@ class TestM1:
obj = ReferencingSection.m_from_dict(obj.m_to_dict(with_meta=True))
assert == 'test_value'
def test_ref_with_section_proxy(self):
package = Package(name='test_package')
class OtherSection(MSection):
name = Quantity(type=str)
class ReferencingSection(MSection):
reference = Quantity(type=Reference(SectionProxy('OtherSection')))
package.m_add_sub_section(Package.section_definitions, OtherSection.m_def)
package.m_add_sub_section(Package.section_definitions, ReferencingSection.m_def)
referencing = ReferencingSection()
reference = OtherSection()
referencing.reference = reference
assert referencing.reference == reference
def test_copy(self):
run = Run()
run.m_create(Parsing).parser_name = 'test'
Supports Markdown
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