Skip to content

Commit

Permalink
Merge pull request #3227 from mashehu/fix-pydantic-dump
Browse files Browse the repository at this point in the history
Fix pydantic dump
  • Loading branch information
mashehu authored Oct 22, 2024
2 parents 170623f + 930b170 commit 02259f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
29 changes: 23 additions & 6 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 Down Expand Up @@ -1094,27 +1094,44 @@ 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
""" 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."""
""" 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)

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]]:
"""
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

0 comments on commit 02259f9

Please sign in to comment.