Skip to content

Commit 36e74b4

Browse files
committed
conditions: switch from PyYAML to ruamel.yaml
PyYAML is not a project dependency; raven-python uses ruamel.yaml (already pulled in via cobra) everywhere else. The conditions module and its tests still imported PyYAML, which broke pytest collection on clean CI runners with 'No module named yaml'. Both apply.py and the test now use a YAML(typ='safe') instance from ruamel.yaml — same plain-dict semantics as PyYAML's safe_load / safe_dump, no new dependency.
1 parent bfe395f commit 36e74b4

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

src/raven_python/conditions/apply.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@
4949
from typing import Any
5050

5151
import cobra
52-
import yaml
52+
from ruamel.yaml import YAML
53+
54+
# A safe loader keeps the parsed document as plain dict / list / scalars,
55+
# which matches what callers expect from ``load_condition``. ruamel.yaml
56+
# is already a transitive dependency via cobra, so we don't take on
57+
# PyYAML on top.
58+
_SAFE_YAML = YAML(typ="safe")
5359

5460
#: ``prelude.reset_exchanges`` puts every exchange reaction at this
5561
#: upper bound (and lower bound = 0).
@@ -62,7 +68,7 @@ def load_condition(path: str | Path) -> dict[str, Any]:
6268
if not path.exists():
6369
raise FileNotFoundError(f"Condition file not found: {path}")
6470
with open(path) as f:
65-
return yaml.safe_load(f)
71+
return _SAFE_YAML.load(f)
6672

6773

6874
def apply_condition(

tests/test_conditions.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
"""Tests for raven_python.conditions.apply (generic condition mechanism)."""
22
from __future__ import annotations
33

4+
import io
5+
46
import cobra
57
import pytest
6-
import yaml
8+
from ruamel.yaml import YAML
79

810
from raven_python.conditions import (
911
apply_condition,
1012
load_condition,
1113
set_reaction_bounds,
1214
)
1315

16+
_SAFE_YAML = YAML(typ="safe")
17+
18+
19+
def _yaml_dump(cfg: dict) -> str:
20+
"""Helper: dump a dict to YAML via ruamel.yaml (PyYAML.safe_dump
21+
equivalent). Used here because PyYAML isn't a project dependency."""
22+
buf = io.StringIO()
23+
_SAFE_YAML.dump(cfg, buf)
24+
return buf.getvalue()
25+
1426

1527
def _toy_model() -> cobra.Model:
1628
m = cobra.Model("toy")
@@ -164,7 +176,7 @@ def test_expected_uptake_count_match_silent(recwarn):
164176
def test_apply_condition_accepts_yaml_path(tmp_path):
165177
cfg = {"bounds": [{"rxn": "EX_glc", "lb": -42}]}
166178
path = tmp_path / "cond.yml"
167-
path.write_text(yaml.safe_dump(cfg))
179+
path.write_text(_yaml_dump(cfg))
168180
m = _toy_model()
169181
apply_condition(m, path)
170182
assert m.reactions.get_by_id("EX_glc").lower_bound == -42
@@ -173,7 +185,7 @@ def test_apply_condition_accepts_yaml_path(tmp_path):
173185
def test_load_condition_round_trip(tmp_path):
174186
cfg = {"name": "x", "bounds": [{"rxn": "r1", "lb": 0}]}
175187
path = tmp_path / "cond.yml"
176-
path.write_text(yaml.safe_dump(cfg))
188+
path.write_text(_yaml_dump(cfg))
177189
assert load_condition(path) == cfg
178190

179191

0 commit comments

Comments
 (0)