diff --git a/docs/changes/newsfragments/191.bugfix b/docs/changes/newsfragments/191.bugfix new file mode 100644 index 000000000..d2c3dede9 --- /dev/null +++ b/docs/changes/newsfragments/191.bugfix @@ -0,0 +1 @@ +Fix a bug in which relative paths in the YAML ``with`` directive would be computed relative to the current working directory of the process instead of the location of the YAML file by `Fede Raimondo`_. \ No newline at end of file diff --git a/docs/changes/newsfragments/214.enh b/docs/changes/newsfragments/214.enh deleted file mode 100644 index dcd629ae6..000000000 --- a/docs/changes/newsfragments/214.enh +++ /dev/null @@ -1 +0,0 @@ -Add missing ``abstractmethod`` decorators for ``get_valid_inputs`` methods of :class:`junifer.markers.BaseMarker` and :class:`junifer.preprocess.BasePreprocessor` by `Synchon Mandal`_ \ No newline at end of file diff --git a/junifer/api/parser.py b/junifer/api/parser.py index 18d37552c..8111f39f6 100644 --- a/junifer/api/parser.py +++ b/junifer/api/parser.py @@ -6,7 +6,6 @@ import importlib import importlib.util -import os import sys from pathlib import Path from typing import Dict, Union @@ -56,7 +55,8 @@ def parse_yaml(filepath: Union[str, Path]) -> Dict: for t_module in to_load: if t_module.endswith(".py"): logger.debug(f"Importing file: {t_module}") - file_path = Path(os.getcwd()) / t_module + # This resolves both absolute and relative paths + file_path = filepath.parent / t_module if not file_path.exists(): raise_error( f"File in 'with' section does not exist: {file_path}" diff --git a/junifer/api/tests/test_parser.py b/junifer/api/tests/test_parser.py index e61ff9fdb..6585a1809 100644 --- a/junifer/api/tests/test_parser.py +++ b/junifer/api/tests/test_parser.py @@ -75,3 +75,63 @@ def test_parse_yaml_failure_with_multi_module_autoload(tmp_path: Path) -> None: # Check test file with pytest.raises(ImportError, match="wrong_config"): parse_yaml(fname) + + +def test_parse_yaml_relative_path(tmp_path: Path) -> None: + """Test YAML parsing with relative paths in with. + + Parameters + ---------- + tmp_path : pathlib.Path + The path to the test directory. + + """ + t_tmp_path = tmp_path / "test_relative_with" + + # Write .py to include + py_path = t_tmp_path / "external" + py_path.mkdir(exist_ok=True, parents=True) + py_fname = py_path / "first.py" + py_fname.write_text("import numpy as np\n") + + # Write yaml that includes a relative path + yaml_path = t_tmp_path / "yamls" + yaml_path.mkdir(exist_ok=True, parents=True) + yaml_fname = yaml_path / "test_parse_yaml_relative_path.yaml" + + yaml_fname.write_text( + "foo: bar\nwith:\n - ../external/first.py\n - scipy\n" + ) + + # Check test file + parse_yaml(yaml_fname) + + +def test_parse_yaml_absolute_path(tmp_path: Path) -> None: + """Test YAML parsing with absolute paths in with. + + Parameters + ---------- + tmp_path : pathlib.Path + The path to the test directory. + + """ + t_tmp_path = tmp_path / "test_relative_with" + + # Write .py to include + py_path = t_tmp_path / "external" + py_path.mkdir(exist_ok=True, parents=True) + py_fname = py_path / "first.py" + py_fname.write_text("import numpy as np\n") + + # Write yaml that includes a relative path + yaml_path = t_tmp_path / "yamls" + yaml_path.mkdir(exist_ok=True, parents=True) + yaml_fname = yaml_path / "test_parse_yaml_relative_path.yaml" + + yaml_fname.write_text( + f"foo: bar\nwith:\n - {py_fname.absolute()}\n - scipy\n" + ) + + # Check test file + parse_yaml(yaml_fname) diff --git a/junifer/markers/base.py b/junifer/markers/base.py index c78097c5d..ba1c03da2 100644 --- a/junifer/markers/base.py +++ b/junifer/markers/base.py @@ -45,7 +45,6 @@ class BaseMarker(ABC, PipelineStepMixin, UpdateMetaMixin): raise ValueError(f"{self.name} cannot be computed on {wrong_on}") self._on = on - @abstractmethod def get_valid_inputs(self) -> List[str]: """Get valid data types for input. diff --git a/junifer/preprocess/base.py b/junifer/preprocess/base.py index 2423cc251..c9f8b97e9 100644 --- a/junifer/preprocess/base.py +++ b/junifer/preprocess/base.py @@ -79,7 +79,6 @@ class BasePreprocessor(ABC, PipelineStepMixin, UpdateMetaMixin): klass=NotImplementedError, ) - @abstractmethod def get_valid_inputs(self) -> List[str]: """Get valid data types for input.