Skip to content

Commit

Permalink
Adds explicit left to right validation metric models (#4165)(patch)
Browse files Browse the repository at this point in the history
## Description

Closes #4078
States explicitly that pydantic works with backward compatibility for validating union typing in the `NextflowAnalysis` class.

### Added

### Changed

- the union typing of the `NextflowAnalysis` attribute `smaple_metrics` and explicitly specify to choose union models according to `left-to-right`
  • Loading branch information
diitaz93 authored Feb 3, 2025
1 parent 9ad92da commit e0e725e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
11 changes: 7 additions & 4 deletions cg/models/analysis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, Field

from cg.models.raredisease.raredisease import RarediseaseQCMetrics
from cg.models.rnafusion.rnafusion import RnafusionQCMetrics
Expand All @@ -15,6 +15,9 @@ class AnalysisModel(BaseModel):
class NextflowAnalysis(AnalysisModel):
"""Nextflow's analysis results model."""

sample_metrics: dict[
str, RarediseaseQCMetrics | RnafusionQCMetrics | TaxprofilerQCMetrics | TomteQCMetrics
]
sample_metrics: (
dict[str, RarediseaseQCMetrics]
| dict[str, RnafusionQCMetrics]
| dict[str, TaxprofilerQCMetrics]
| dict[str, TomteQCMetrics]
) = Field(union_mode="left_to_right")
5 changes: 3 additions & 2 deletions cg/models/taxprofiler/taxprofiler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from pathlib import Path

from pydantic import BaseModel, Field
from pydantic import Field

from cg.constants.sequencing import SequencingPlatform
from cg.models.nf_analysis import NextflowSampleSheetEntry, WorkflowParameters
from cg.models.qc_metrics import QCMetrics


class TaxprofilerQCMetrics(BaseModel):
class TaxprofilerQCMetrics(QCMetrics):
"""Taxprofiler QC metrics."""

after_filtering_gc_content: float
Expand Down
2 changes: 1 addition & 1 deletion tests/meta/workflow/test_rnafusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_parse_analysis(
qc_metrics: list[MetricsBase] = analysis_api.get_multiqc_json_metrics(case_id=rnafusion_case_id)

# WHEN extracting the analysis model
analysis_model: NextflowAnalysis = analysis_api.parse_analysis(qc_metrics_raw=qc_metrics)
analysis_model: NextflowAnalysis = analysis_api.parse_analysis(qc_metrics)

# THEN the analysis model and its content should have been correctly extracted
assert analysis_model.sample_metrics[sample_id].model_dump() == rnafusion_metrics
Expand Down

0 comments on commit e0e725e

Please sign in to comment.