From 93f39fdacf3ae8cb6736927f325bf1088fb61b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Therese=20Natter=C3=B8y?= <61694854+tnatt@users.noreply.github.com> Date: Mon, 27 May 2024 18:00:12 +0200 Subject: [PATCH] MAINT: Move workflow conversion to silence warning (#661) --- src/fmu/dataio/dataio.py | 16 +--------- src/fmu/dataio/providers/_fmu.py | 9 ++++-- tests/test_units/test_fmuprovider_class.py | 36 ++++++++++++---------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/fmu/dataio/dataio.py b/src/fmu/dataio/dataio.py index 1aaa30438..2dc108f2e 100644 --- a/src/fmu/dataio/dataio.py +++ b/src/fmu/dataio/dataio.py @@ -31,7 +31,7 @@ from .aggregation import AggregatedData from .case import InitializeCase from .datastructure.configuration import global_configuration -from .datastructure.meta import enums, meta +from .datastructure.meta import enums from .providers._fmu import FmuProvider, get_fmu_context_from_environment # DATAIO_EXAMPLES: Final = dataio_examples() @@ -406,7 +406,6 @@ def __post_init__(self) -> None: self.config = some_config_from_env(GLOBAL_ENVNAME) or {} self._validate_and_establish_fmucontext() - self._validate_workflow_key() # check state of global config self._config_is_valid = global_configuration.is_valid(self.config) @@ -583,17 +582,6 @@ def _show_deprecations_or_notimplemented(self) -> None: UserWarning, ) - def _validate_workflow_key(self) -> None: - if self.workflow: - if isinstance(self.workflow, str): - workflow = meta.Workflow(reference=self.workflow) - elif isinstance(self.workflow, dict): - workflow = meta.Workflow.model_validate(self.workflow) - else: - raise TypeError("'workflow' should be string.") - - self.workflow = workflow.model_dump(mode="json", exclude_none=True) - def _validate_and_establish_fmucontext(self) -> None: """ Validate the given 'fmu_context' input. if not explicitly given it @@ -668,7 +656,6 @@ def _update_check_settings(self, newsettings: dict) -> None: logger.info("New setting OK for %s", setting) self._show_deprecations_or_notimplemented() - self._validate_workflow_key() self._validate_and_establish_fmucontext() self._rootpath = self._establish_rootpath() @@ -740,7 +727,6 @@ def _check_process_object(self, obj: types.Inferrable) -> None: def _get_fmu_provider(self) -> FmuProvider: assert isinstance(self.fmu_context, FmuContext) - assert isinstance(self.workflow, dict) or self.workflow is None return FmuProvider( model=self.config.get("model"), fmu_context=self.fmu_context, diff --git a/src/fmu/dataio/providers/_fmu.py b/src/fmu/dataio/providers/_fmu.py index 63e47fa60..6dfe24a77 100644 --- a/src/fmu/dataio/providers/_fmu.py +++ b/src/fmu/dataio/providers/_fmu.py @@ -33,7 +33,7 @@ from dataclasses import dataclass, field from enum import Enum, auto from pathlib import Path -from typing import TYPE_CHECKING, Final, Optional +from typing import TYPE_CHECKING, Final, Optional, Union from warnings import warn from fmu.config import utilities as ut @@ -109,7 +109,7 @@ class FmuProvider(Provider): fmu_context: FmuContext = FmuContext.REALIZATION include_ertjobs: bool = True casepath_proposed: Optional[Path] = None - workflow: Optional[dict[str, str]] = None + workflow: Optional[Union[str, dict[str, str]]] = None # private properties for this class _runpath: Optional[Path] = field(default_factory=Path, init=False) @@ -321,4 +321,7 @@ def _get_fmumodel_meta(self) -> meta.FMUModel: return meta.FMUModel.model_validate(self.model) def _get_workflow_meta(self) -> meta.Workflow: - return meta.Workflow.model_validate(self.workflow) + assert self.workflow is not None + if isinstance(self.workflow, dict): + return meta.Workflow.model_validate(self.workflow) + return meta.Workflow(reference=self.workflow) diff --git a/tests/test_units/test_fmuprovider_class.py b/tests/test_units/test_fmuprovider_class.py index 35ea9f6c9..ac1c6d203 100644 --- a/tests/test_units/test_fmuprovider_class.py +++ b/tests/test_units/test_fmuprovider_class.py @@ -331,33 +331,35 @@ def test_fmuprovider_workflow_reference(fmurun_w_casemetadata): os.chdir(fmurun_w_casemetadata) # workflow input is a string - edata = dataio.ExportData(workflow="workflow as string") - # check that the conversion to dict works - assert edata.workflow == {"reference": "workflow as string"} myfmu_meta = FmuProvider( - model=GLOBAL_CONFIG_MODEL, workflow=edata.workflow + model=GLOBAL_CONFIG_MODEL, workflow="workflow as string" ).get_metadata() assert myfmu_meta.workflow is not None - assert ( - myfmu_meta.workflow.model_dump(mode="json", exclude_none=True) == edata.workflow - ) + assert myfmu_meta.workflow.model_dump(mode="json") == { + "reference": "workflow as string" + } - # workflow input is a correct dict + # workflow as a dict should give future warning with pytest.warns(FutureWarning, match="The 'workflow' argument"): - edata = dataio.ExportData(workflow={"reference": "workflow as dict"}) - assert edata.workflow == {"reference": "workflow as dict"} + dataio.ExportData(workflow={"reference": "workflow as dict"}) + + # test that workflow as a dict still gives valid results myfmu_meta = FmuProvider( - model=GLOBAL_CONFIG_MODEL, workflow=edata.workflow + model=GLOBAL_CONFIG_MODEL, workflow={"reference": "workflow as dict"} ).get_metadata() assert myfmu_meta.workflow is not None - assert ( - myfmu_meta.workflow.model_dump(mode="json", exclude_none=True) == edata.workflow - ) + assert myfmu_meta.workflow.model_dump(mode="json") == { + "reference": "workflow as dict" + } # workflow input is non-correct dict with pytest.raises(pydantic.ValidationError): - dataio.ExportData(workflow={"wrong": "workflow as dict"}) + FmuProvider( + model=GLOBAL_CONFIG_MODEL, workflow={"wrong": "workflow as dict"} + ).get_metadata() # workflow input is other types - shall fail - with pytest.raises(TypeError): - dataio.ExportData(workflow=123.4) + with pytest.raises( + pydantic.ValidationError, match="Input should be a valid string" + ): + FmuProvider(model=GLOBAL_CONFIG_MODEL, workflow=123.4).get_metadata()