Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pydantic dump #3227

Merged
merged 11 commits into from
Oct 22, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
57 changes: 41 additions & 16 deletions nf_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -1094,27 +1094,52 @@ 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"
mashehu marked this conversation as resolved.
Show resolved Hide resolved
)
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)

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"]
mashehu marked this conversation as resolved.
Show resolved Hide resolved

# 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]]:
"""
Expand Down
4 changes: 3 additions & 1 deletion tests/pipelines/lint/test_nfcore_yml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading