From 0c77671679bacf0aa337d4fd73941f73578a32f2 Mon Sep 17 00:00:00 2001 From: nourinmohd Date: Thu, 2 Jul 2026 20:25:08 +0200 Subject: [PATCH] DEP: Deprecate warning for ExportData.fmu-context --- .../export_preprocessed_surface.py | 11 ++++-- pyproject.toml | 1 + .../dataio/_export/_export_config_resolver.py | 19 ++++++++-- src/fmu/dataio/dataio.py | 2 +- tests/test_units/test_dataio.py | 8 ++++ tests/test_units/test_fmu_context.py | 37 +++++++++++++++++-- 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/examples/example_exports/export_rms_data/export_preprocessed_surface.py b/examples/example_exports/export_rms_data/export_preprocessed_surface.py index afa92b5b1..f55bb3e5c 100644 --- a/examples/example_exports/export_rms_data/export_preprocessed_surface.py +++ b/examples/example_exports/export_rms_data/export_preprocessed_surface.py @@ -7,19 +7,22 @@ from fmu.dataio.dataio import ExportData -CFG = ut.yaml_load("../../fmuconfig/output/global_variables.yml") +SCRIPT_DIR = Path(__file__).resolve().parent +EXAMPLES_ROOT = SCRIPT_DIR.parents[1] -DEPTH_FILE = Path("../output/maps/structure/") / "topvolantis--ds_extract_geogrid.gri" +CFG = ut.yaml_load(EXAMPLES_ROOT / "fmuconfig/output/global_variables.yml") + +DEPTH_FILE = ( + SCRIPT_DIR.parent / "output/maps/structure/topvolantis--ds_extract_geogrid.gri" +) def export_preprocessed_surface(): """Export a preprocessed surface with metadata.""" - export_data = ExportData( config=CFG, preprocessed=True, name="preprocessedmap", - fmu_context="case", content="depth", is_observation=True, subfolder="mysub", diff --git a/pyproject.toml b/pyproject.toml index 168a81e32..8cf8286d7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -132,6 +132,7 @@ filterwarnings = [ "ignore:The value of 'vertical_domain' is 'depth'.*vertical_domain.*will be set to 'time':UserWarning", "ignore:Setting 'rep_include' from the config is deprecated:FutureWarning", "ignore:Providing input settings through environment variables is deprecated:FutureWarning", + "ignore:The 'fmu_context' argument is deprecated:FutureWarning", # Ignore mutation deprecation warnings (tested in test_dataio.py::test_mutation_*) "ignore:Mutating ExportData\\..* after initialization is deprecated:FutureWarning", # Ignore importlib numpy size incompatibility warning diff --git a/src/fmu/dataio/_export/_export_config_resolver.py b/src/fmu/dataio/_export/_export_config_resolver.py index ff3406fc6..8cf2433f2 100644 --- a/src/fmu/dataio/_export/_export_config_resolver.py +++ b/src/fmu/dataio/_export/_export_config_resolver.py @@ -358,6 +358,13 @@ def _resolve_fmu_context( ) effective_context = _determine_effective_fmu_context(fmu_context_input, env_context) + if ( + preprocessed + and fmu_context_input is None + and effective_context == FMUContext.realization + ): + effective_context = FMUContext.case + _validate_fmu_context_combination(effective_context, preprocessed) return effective_context, preprocessed @@ -389,6 +396,13 @@ def _handle_fmu_context_deprecations( Returns: Tuple of (transformed_fmu_context, transformed_preprocessed). """ + if fmu_context_input is not None and fmu_context_input != "preprocessed": + warnings.warn( + "The 'fmu_context' argument is deprecated and will be removed in the " + "future. fmu-dataio now infers the FMU context from the environment.", + FutureWarning, + ) + if fmu_context_input == "preprocessed": warnings.warn( "Using the 'fmu_context' argument with value 'preprocessed' is " @@ -468,9 +482,8 @@ def _validate_fmu_context_combination( if preprocessed and context == FMUContext.realization: raise ValueError( "Can't export preprocessed data in a fmu_context='realization'. " - "Preprocessed data should be exported with fmu_context='case' or " - "outside of FMU entirely, and then re-exported using " - "ExportPreprocessedData." + "Preprocessed data should be exported in case context or outside of " + "FMU entirely, and then re-exported using ExportPreprocessedData." ) diff --git a/src/fmu/dataio/dataio.py b/src/fmu/dataio/dataio.py index 77285fb7d..50f067ee1 100644 --- a/src/fmu/dataio/dataio.py +++ b/src/fmu/dataio/dataio.py @@ -496,7 +496,7 @@ class ExportData: # Does not appear to have been used for anything. fmu_context: str | None = None - # Optional string with value ``realization`` or ``case``. + # Deprecated. fmu-dataio infers this from the environment. # # .. tip:: # You most likely do not need to set this. fmu-dataio infers this by itself. diff --git a/tests/test_units/test_dataio.py b/tests/test_units/test_dataio.py index 99928131d..fb894ebf2 100644 --- a/tests/test_units/test_dataio.py +++ b/tests/test_units/test_dataio.py @@ -37,6 +37,14 @@ def convert_datestr_to_isoformat(value: str, format: str = "%Y%m%d") -> str: return datetime.strptime(value, format).isoformat() +def test_exportdata_fmu_context_argument_is_deprecated( + mock_global_config: dict[str, Any], +) -> None: + """Setting fmu_context on ExportData emits a deprecation warning.""" + with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"): + ExportData(config=mock_global_config, content="depth", fmu_context="case") + + def test_generate_metadata_simple(mock_global_config: dict[str, Any]) -> None: """Test generating metadata""" diff --git a/tests/test_units/test_fmu_context.py b/tests/test_units/test_fmu_context.py index 4a9a9ebdc..71ba3134c 100644 --- a/tests/test_units/test_fmu_context.py +++ b/tests/test_units/test_fmu_context.py @@ -110,13 +110,29 @@ def test_fmu_context_preprocessed_emits_deprecation_warning() -> None: assert fmu_context is None +@pytest.mark.parametrize("explicit_context", ["realization", "case", "ensemble"]) +def test_fmu_context_argument_emits_deprecation_warning( + explicit_context: str, +) -> None: + """Using fmu_context explicitly triggers deprecation warning.""" + with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"): + fmu_context, preprocessed = _handle_fmu_context_deprecations( + fmu_context_input=explicit_context, + preprocessed_input=False, + ) + + assert fmu_context == explicit_context + assert preprocessed is False + + @pytest.mark.parametrize("iteration_variant", ["iteration", "ITERATION", "Iteration"]) def test_iteration_converted_to_ensemble(iteration_variant: str) -> None: """Using "iteration" context is converted to "ensemble".""" - fmu_context, preprocessed = _handle_fmu_context_deprecations( - fmu_context_input=iteration_variant, - preprocessed_input=False, - ) + with pytest.warns(FutureWarning, match="'fmu_context' argument is deprecated"): + fmu_context, preprocessed = _handle_fmu_context_deprecations( + fmu_context_input=iteration_variant, + preprocessed_input=False, + ) assert fmu_context == "ensemble" @@ -214,3 +230,16 @@ def test_resolve_fmu_context_integration_with_preprocessed( preprocessed_input=True, ) assert preprocessed is True + + +def test_resolve_preprocessed_uses_case_context_from_realization_env( + runpath_no_dotfmu: Path, +) -> None: + """Preprocessed exports in realization env are written at case level.""" + context, preprocessed = _resolve_fmu_context( + fmu_context_input=None, + preprocessed_input=True, + ) + + assert context == FMUContext.case + assert preprocessed is True