From 6ddd824fd0fcbae83579075eaf72393c8d3d71e5 Mon Sep 17 00:00:00 2001 From: mashehu Date: Wed, 16 Oct 2024 11:13:19 +0200 Subject: [PATCH 1/8] write only relevant fields in `.nf-core.yml` --- nf_core/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/nf_core/utils.py b/nf_core/utils.py index 87dd307e7..bad733cd4 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -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 + data = 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"] + + # Remove the fields based on repository_type + for field in fields_to_exclude: + data.pop(field, None) + + return data + def load_tools_config(directory: Union[str, Path] = ".") -> Tuple[Optional[Path], Optional[NFCoreYamlConfig]]: """ From 313a4d1b20f4bab6c19a78b4d6fe2028348b9a2c Mon Sep 17 00:00:00 2001 From: mashehu Date: Wed, 16 Oct 2024 13:59:08 +0200 Subject: [PATCH 2/8] switch from docstrings to Field descsriptions --- nf_core/utils.py | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index bad733cd4..6ba1c8c5c 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 @@ -30,7 +30,7 @@ import rich.markup import yaml from packaging.version import Version -from pydantic import BaseModel, ValidationError, field_validator +from pydantic import BaseModel, Field, ValidationError, field_validator from rich.live import Live from rich.spinner import Spinner @@ -1094,20 +1094,28 @@ 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 """ - nf_core_version: Optional[str] = None - """ 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 - """ Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation """ - 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.""" - 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.""" + repository_type: Literal["pipeline", "modules"] = Field(..., description="Type of repository") + nf_core_version: Optional[str] = Field( + None, description="Version of nf-core/tools used to create/update the pipeline" + ) + org_path: Optional[str] = Field( + None, description="Path to the organisation's modules repository (used for modules repo_type only)" + ) + lint: Optional[LintConfigType] = Field( + None, + description="Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation", + ) + template: Optional[NFCoreTemplateConfig] = Field( + None, description="Pipeline template configuration", exclude=repository_type == "modules" + ) + bump_version: Optional[Dict[str, bool]] = Field( + None, + description="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]]]]]]] = Field( + None, + description="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) @@ -1117,7 +1125,7 @@ def get(self, item: str, default: Any = None) -> Any: def model_dump(self, **kwargs) -> Dict[str, Any]: # Get the initial data - data = super().model_dump(**kwargs) + config = super().model_dump(**kwargs) if self.repository_type == "modules": # Fields to exclude for modules @@ -1128,9 +1136,9 @@ def model_dump(self, **kwargs) -> Dict[str, Any]: # Remove the fields based on repository_type for field in fields_to_exclude: - data.pop(field, None) + config.pop(field, None) - return data + return config def load_tools_config(directory: Union[str, Path] = ".") -> Tuple[Optional[Path], Optional[NFCoreYamlConfig]]: From 16e6ff29e376aa5b0e08818862cc403db66508cb Mon Sep 17 00:00:00 2001 From: nf-core-bot Date: Wed, 16 Oct 2024 12:00:53 +0000 Subject: [PATCH 3/8] [automated] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c19a34ee..337321732 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,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)) +- Fix pydantic dump ([#3227](https://github.com/nf-core/tools/pull/3227)) ## [v3.0.2 - Titanium Tapir Patch](https://github.com/nf-core/tools/releases/tag/3.0.2) - [2024-10-11] From 87ab7eefc3fbb3d4fd0f4b206675f3f37e18a36a Mon Sep 17 00:00:00 2001 From: mashehu Date: Wed, 16 Oct 2024 15:51:30 +0200 Subject: [PATCH 4/8] update test to new error --- tests/pipelines/lint/test_nfcore_yml.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From 4abbc4a141d096b357010443c8d20363206f7491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20H=C3=B6rtenhuber?= Date: Tue, 22 Oct 2024 15:31:39 +0200 Subject: [PATCH 5/8] Update nf_core/utils.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: JĂșlia Mir Pedrol --- nf_core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 6ba1c8c5c..b5129a368 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -1132,7 +1132,7 @@ def model_dump(self, **kwargs) -> Dict[str, Any]: fields_to_exclude = ["template", "update"] else: # pipeline # Fields to exclude for pipeline - fields_to_exclude = ["bump_version"] + fields_to_exclude = ["bump_version", "org_path"] # Remove the fields based on repository_type for field in fields_to_exclude: From ff27b29a84fabdf9f69a35ddf51595e55ac39880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20H=C3=B6rtenhuber?= Date: Tue, 22 Oct 2024 15:32:29 +0200 Subject: [PATCH 6/8] Update nf_core/utils.py --- nf_core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index b5129a368..9ef3cff2e 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -1106,7 +1106,7 @@ class NFCoreYamlConfig(BaseModel): description="Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation", ) template: Optional[NFCoreTemplateConfig] = Field( - None, description="Pipeline template configuration", exclude=repository_type == "modules" + None, description="Pipeline template configuration" ) bump_version: Optional[Dict[str, bool]] = Field( None, From fdef700925a783e4fa2410704f9d7364e61d1d69 Mon Sep 17 00:00:00 2001 From: nf-core-bot Date: Tue, 22 Oct 2024 14:01:04 +0000 Subject: [PATCH 7/8] [automated] Fix code linting --- nf_core/utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 6ba329cbb..8a6bc0841 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -1105,9 +1105,7 @@ class NFCoreYamlConfig(BaseModel): None, description="Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation", ) - template: Optional[NFCoreTemplateConfig] = Field( - None, description="Pipeline template configuration" - ) + template: Optional[NFCoreTemplateConfig] = Field(None, description="Pipeline template configuration") bump_version: Optional[Dict[str, bool]] = Field( None, description="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.", From bc37fb0716b78d2a7802a1737aa25522fb41e4f5 Mon Sep 17 00:00:00 2001 From: mashehu Date: Tue, 22 Oct 2024 16:05:12 +0200 Subject: [PATCH 8/8] no need for Field value --- nf_core/utils.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/nf_core/utils.py b/nf_core/utils.py index 9ef3cff2e..bbbf7f178 100644 --- a/nf_core/utils.py +++ b/nf_core/utils.py @@ -30,7 +30,7 @@ import rich.markup import yaml from packaging.version import Version -from pydantic import BaseModel, Field, ValidationError, field_validator +from pydantic import BaseModel, ValidationError, field_validator from rich.live import Live from rich.spinner import Spinner @@ -1094,28 +1094,20 @@ def get(self, item: str, default: Any = None) -> Any: class NFCoreYamlConfig(BaseModel): """.nf-core.yml configuration file schema""" - repository_type: Literal["pipeline", "modules"] = Field(..., description="Type of repository") - nf_core_version: Optional[str] = Field( - None, description="Version of nf-core/tools used to create/update the pipeline" - ) - org_path: Optional[str] = Field( - None, description="Path to the organisation's modules repository (used for modules repo_type only)" - ) - lint: Optional[LintConfigType] = Field( - None, - description="Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation", - ) - template: Optional[NFCoreTemplateConfig] = Field( - None, description="Pipeline template configuration" - ) - bump_version: Optional[Dict[str, bool]] = Field( - None, - description="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]]]]]]] = Field( - None, - description="Disable updating specific modules/subworkflows (when repository_type is pipeline). See https://nf-co.re/docs/nf-core-tools/modules/update for more information.", - ) + 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 """ + org_path: Optional[str] = None + """ Path to the organisation's modules repository (used for modules repo_type only) """ + lint: Optional[LintConfigType] = None + """ Pipeline linting configuration, see https://nf-co.re/docs/nf-core-tools/pipelines/lint#linting-config for examples and documentation """ + 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. """ + 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. """ def __getitem__(self, item: str) -> Any: return getattr(self, item)