Skip to content

Commit 9537e00

Browse files
dweindlclaude
andcommitted
Raise clear error for PEtab YAML missing problems section
`Problem.from_yaml` raised a raw `KeyError`/`IndexError` when the `problems` list was missing or empty, instead of a helpful message. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 02fd94e commit 9537e00

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎petab/v1/problem.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ def get_path(filename):
311311
config = ProblemConfig(
312312
**yaml_config, base_path=base_path, filepath=filepath
313313
)
314+
if not config.problems:
315+
raise ValueError(
316+
"The PEtab problem YAML file is missing a 'problems' section."
317+
)
314318
problem0 = config.problems[0]
315319
# currently required for handling PEtab v2 in here
316320
problem0_ = yaml_config["problems"][0]

‎petab/v1/yaml.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def is_composite_problem(yaml_config: dict | str | Path) -> bool:
205205
yaml_config: PEtab configuration as dictionary or YAML file name
206206
"""
207207
yaml_config = load_yaml(yaml_config)
208-
return len(yaml_config[PROBLEMS]) > 1
208+
return len(yaml_config.get(PROBLEMS, [])) > 1
209209

210210

211211
def assert_single_condition_and_sbml_file(problem_config: dict) -> None:

‎tests/v1/test_petab.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,18 @@ def test_problem_from_yaml_v1_empty():
824824
petab.Problem.from_yaml(yaml_config)
825825

826826

827+
def test_problem_from_yaml_v1_missing_problems():
828+
"""Test loading PEtab version 1 yaml without a `problems` section
829+
raises a helpful error instead of a raw KeyError/IndexError"""
830+
yaml_config = """
831+
format_version: 1
832+
parameter_file:
833+
"""
834+
yaml_config = safe_load(StringIO(yaml_config))
835+
with pytest.raises(ValueError, match="problems"):
836+
petab.Problem.from_yaml(yaml_config)
837+
838+
827839
def test_problem_from_yaml_v1_multiple_files():
828840
"""Test loading PEtab version 1 yaml with multiple condition / measurement
829841
/ observable files

0 commit comments

Comments
 (0)