diff --git a/CHANGELOG.md b/CHANGELOG.md index 95b7757c0..5b8483b01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Include .nf-core.yml in `nf-core pipelines bump-version` ([#3220](https://github.com/nf-core/tools/pull/3220)) - create: add shortcut to toggle all switches ([#3226](https://github.com/nf-core/tools/pull/3226)) +- Remove unrelated values when saving `.nf-core` file ([#3227](https://github.com/nf-core/tools/pull/3227)) - chore(deps): update pre-commit hook pre-commit/mirrors-mypy to v1.12.0 ([#3230](https://github.com/nf-core/tools/pull/3230)) - chore(deps): update pre-commit hook astral-sh/ruff-pre-commit to v0.7.0 ([#3229](https://github.com/nf-core/tools/pull/3229)) - Update python:3.12-slim Docker digest to 032c526 ([#3232](https://github.com/nf-core/tools/pull/3232)) diff --git a/nf_core/utils.py b/nf_core/utils.py index 0b3c53f3a..16125aed3 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -19,7 +19,7 @@ import time from contextlib import contextmanager from pathlib import Path -from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union +from typing import Any, Callable, Dict, Generator, List, Literal, Optional, Tuple, Union import git import prompt_toolkit.styles @@ -1094,10 +1094,10 @@ def get(self, item: str, default: Any = None) -> Any: class NFCoreYamlConfig(BaseModel): """.nf-core.yml configuration file schema""" - repository_type: str - """ Type of repository: pipeline or modules """ + repository_type: Optional[Literal["pipeline", "modules"]] = None + """ Type of repository """ nf_core_version: Optional[str] = None - """ Version of nf-core/tools used to create/update the pipeline""" + """ Version of nf-core/tools used to create/update the pipeline """ org_path: Optional[str] = None """ Path to the organisation's modules repository (used for modules repo_type only) """ lint: Optional[LintConfigType] = None @@ -1105,9 +1105,9 @@ class NFCoreYamlConfig(BaseModel): template: Optional[NFCoreTemplateConfig] = None """ Pipeline template configuration """ bump_version: Optional[Dict[str, bool]] = None - """ Disable bumping of the version for a module/subworkflow (when repository_type is modules). See https://nf-co.re/docs/nf-core-tools/modules/bump-versions for more information.""" + """ Disable bumping of the version for a module/subworkflow (when repository_type is modules). See https://nf-co.re/docs/nf-core-tools/modules/bump-versions for more information. """ update: Optional[Dict[str, Union[str, bool, Dict[str, Union[str, Dict[str, Union[str, bool]]]]]]] = None - """ Disable updating specific modules/subworkflows (when repository_type is pipeline). See https://nf-co.re/docs/nf-core-tools/modules/update for more information.""" + """ Disable updating specific modules/subworkflows (when repository_type is pipeline). See https://nf-co.re/docs/nf-core-tools/modules/update for more information. """ def __getitem__(self, item: str) -> Any: return getattr(self, item) @@ -1115,6 +1115,23 @@ def __getitem__(self, item: str) -> Any: def get(self, item: str, default: Any = None) -> Any: return getattr(self, item, default) + def model_dump(self, **kwargs) -> Dict[str, Any]: + # Get the initial data + config = super().model_dump(**kwargs) + + if self.repository_type == "modules": + # Fields to exclude for modules + fields_to_exclude = ["template", "update"] + else: # pipeline + # Fields to exclude for pipeline + fields_to_exclude = ["bump_version", "org_path"] + + # Remove the fields based on repository_type + for field in fields_to_exclude: + config.pop(field, None) + + return config + def load_tools_config(directory: Union[str, Path] = ".") -> Tuple[Optional[Path], Optional[NFCoreYamlConfig]]: """ diff --git a/tests/pipelines/lint/test_nfcore_yml.py b/tests/pipelines/lint/test_nfcore_yml.py index 955c00da8..b49b60436 100644 --- a/tests/pipelines/lint/test_nfcore_yml.py +++ b/tests/pipelines/lint/test_nfcore_yml.py @@ -33,7 +33,9 @@ def test_nfcore_yml_fail_repo_type(self): with open(self.nf_core_yml, "w") as fh: fh.write(new_content) lint_obj = nf_core.pipelines.lint.PipelineLint(self.new_pipeline) - lint_obj._load() + # assert that it raises assertion error + with self.assertRaises(AssertionError): + lint_obj._load() results = lint_obj.nfcore_yml() assert "Repository type in `.nf-core.yml` is not valid." in str(results["failed"]) assert len(results.get("warned", [])) == 0