From 1ac66a4dddaed203383bc9e9435935974ee74698 Mon Sep 17 00:00:00 2001 From: Julian Geiger Date: Wed, 2 Oct 2024 14:41:45 +0200 Subject: [PATCH] Catch `AttributeError` in `_prepare_yaml` of `orm.PortableCode`. Otherwise, the `_exportcontent` method of `orm.Data` will fail, due to the discrepancy of the properties defined in the pydantic model and what is actually being set as attributes of the class. --- src/aiida/orm/nodes/data/code/abstract.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/aiida/orm/nodes/data/code/abstract.py b/src/aiida/orm/nodes/data/code/abstract.py index 812d111ae..72c0a48d8 100644 --- a/src/aiida/orm/nodes/data/code/abstract.py +++ b/src/aiida/orm/nodes/data/code/abstract.py @@ -11,6 +11,7 @@ from __future__ import annotations import abc +import contextlib import functools import pathlib import typing as t @@ -382,23 +383,24 @@ def get_builder(self) -> 'ProcessBuilder': return builder - def _prepare_yml(self, *args, **kwargs): - """Export code to a yml file.""" + def _prepare_yaml(self, *args, **kwargs): + """Export code to a YAML file.""" import yaml code_data = {} sort = kwargs.get('sort', False) for key in self.Model.model_fields.keys(): - value = getattr(self, key).label if key == 'computer' else getattr(self, key) + with contextlib.suppress(AttributeError): + value = getattr(self, key).label if key == 'computer' else getattr(self, key) - # If the attribute is not set, for example ``with_mpi`` do not export it, so that there are no null-values - # in the resulting YAML file - if value is not None: - code_data[key] = str(value) + # If the attribute is not set, for example ``with_mpi`` do not export it + # so that there are no null-values in the resulting YAML file + if value is not None: + code_data[key] = str(value) return yaml.dump(code_data, sort_keys=sort, encoding='utf-8'), {} - def _prepare_yaml(self, *args, **kwargs): + def _prepare_yml(self, *args, **kwargs): """Also allow for export as .yaml""" - return self._prepare_yml(*args, **kwargs) + return self._prepare_yaml(*args, **kwargs)