diff --git a/src/ga4gh/va_spec/acmg_2015/models.py b/src/ga4gh/va_spec/acmg_2015/models.py index ac35bb4..0862eb3 100644 --- a/src/ga4gh/va_spec/acmg_2015/models.py +++ b/src/ga4gh/va_spec/acmg_2015/models.py @@ -6,6 +6,7 @@ from enum import Enum from pydantic import Field, field_validator, model_validator +from typing_extensions import Self from ga4gh.core.models import MappableConcept, iriReference from ga4gh.va_spec.base.core import ( @@ -127,22 +128,20 @@ def validate_strength_of_evidence_provided( mc_is_required=False, ) - @model_validator(mode="before") - def validate_model(cls, values: dict) -> dict: # noqa: N805 + @model_validator(mode="after") + def validate_model(self) -> Self: """Validate ``evidenceOutcome`` and ``directionOfEvidenceProvided`` properties - :param values: Input values :raises ValueError: If ``evidenceOutcome`` exists and is invalid - :return: Validated input values. If ``evidenceOutcome`` exists, then it will be - validated and converted to a ``MappableConcept``. + :return: Validated input values. If ``evidenceOutcome`` exists. Or if ``strengthOfEvidenceProvided`` is not provided when ``directionOfEvidenceProvided`` is supports or disputes or if ``strengthOfEvidenceProvided`` is provided when ``directionOfEvidenceProvided`` is neutral """ - cls._validate_direction_of_evidence_provided(values) + self._validate_direction_of_evidence_provided() acmg_code_pattern = r"^((?:PVS1)(?:_(?:not_met|(?:strong|moderate|supporting)))?|(?:PS[1-4]|BS[1-4])(?:_(?:not_met|(?:very_strong|moderate|supporting)))?|BA1(?:_not_met)?|(?:PM[1-6])(?:_(?:not_met|(?:very_strong|strong|supporting)))?|(PP[1-5]|BP[1-7])(?:_(?:not_met|very_strong|strong|moderate))?)$" - return cls._validate_evidence_outcome(values, SYSTEM, acmg_code_pattern) + return self._validate_evidence_outcome(SYSTEM, acmg_code_pattern) class VariantPathogenicityStatement(Statement): diff --git a/src/ga4gh/va_spec/base/core.py b/src/ga4gh/va_spec/base/core.py index 8fbf3be..b6f1200 100644 --- a/src/ga4gh/va_spec/base/core.py +++ b/src/ga4gh/va_spec/base/core.py @@ -17,6 +17,7 @@ ValidationError, field_validator, ) +from typing_extensions import Self from ga4gh.cat_vrs.models import CategoricalVariant from ga4gh.core.models import ( @@ -648,54 +649,48 @@ def validate_has_evidence_items( raise ValueError(err_msg) return evidence_items - @staticmethod - def _validate_evidence_outcome( - values: dict, system: System, code_pattern: str - ) -> dict: + def _validate_evidence_outcome(self, system: System, code_pattern: str) -> Self: """Validate ``evidenceOutcome`` property if it exists - :param values: Input values :param system: System that should be used for ``primaryCoding.system`` :param code_pattern: The regex pattern that should be used for ``primaryCoding.code`` :raises ValueError: If ``evidenceOutcome`` exists and is invalid - :return: Validated input values. If ``evidenceOutcome`` exists, then it will be - validated and converted to a ``MappableConcept`` """ - if "evidenceOutcome" in values: - mc = MappableConcept(**values["evidenceOutcome"]) - values["evidenceOutcome"] = mc + if evidence_outcome := self.evidenceOutcome: validate_mappable_concept( - mc, system, code_pattern=code_pattern, mc_is_required=False + evidence_outcome, + system, + code_pattern=code_pattern, + mc_is_required=False, ) - return values + return self - @staticmethod - def _validate_direction_of_evidence_provided(values: dict) -> dict: + def _validate_direction_of_evidence_provided(self) -> Self: """Validate conditional requirements for ``directionOfEvidenceProvided`` - :param values: Input values :raises ValueError: If ``strengthOfEvidenceProvided`` is not provided when ``directionOfEvidenceProvided`` is supports or disputes or if ``strengthOfEvidenceProvided`` is provided when ``directionOfEvidenceProvided`` is neutral - :return: Validated input values """ - direction_of_evidence_provided = values.get("directionOfEvidenceProvided") + direction_of_evidence_provided = self.directionOfEvidenceProvided + strength_of_evidence_provided = self.strengthOfEvidenceProvided if ( direction_of_evidence_provided in (Direction.SUPPORTS, Direction.DISPUTES) - and values.get("strengthOfEvidenceProvided") is None + and strength_of_evidence_provided is None ): err_msg = f"`strengthOfEvidenceProvided` is required when `directionOfEvidenceProvided` is '{Direction.SUPPORTS.value}' or '{Direction.DISPUTES.value}'." raise ValueError(err_msg) - if direction_of_evidence_provided == Direction.NEUTRAL and values.get( - "strengthOfEvidenceProvided" + if ( + direction_of_evidence_provided == Direction.NEUTRAL + and strength_of_evidence_provided ): err_msg = f"`strengthOfEvidenceProvided` is not allowed when `directionOfEvidenceProvided` is '{Direction.NEUTRAL.value}'." raise ValueError(err_msg) - return values + return self @field_validator("specifiedBy") @classmethod diff --git a/src/ga4gh/va_spec/ccv_2022/models.py b/src/ga4gh/va_spec/ccv_2022/models.py index d24d48d..cd133f8 100644 --- a/src/ga4gh/va_spec/ccv_2022/models.py +++ b/src/ga4gh/va_spec/ccv_2022/models.py @@ -6,6 +6,7 @@ from enum import Enum from pydantic import Field, field_validator, model_validator +from typing_extensions import Self from ga4gh.core.models import MappableConcept, iriReference from ga4gh.va_spec.base.core import ( @@ -98,22 +99,19 @@ def validate_strength_of_evidence_provided( mc_is_required=False, ) - @model_validator(mode="before") - def validate_model(cls, values: dict) -> dict: # noqa: N805 + @model_validator(mode="after") + def validate_model(self) -> Self: """Validate ``evidenceOutcome`` and ``directionOfEvidenceProvided`` properties - :param values: Input values - :raises ValueError: If ``evidenceOutcome`` exists and is invalid - :return: Validated input values. If ``evidenceOutcome`` exists, then it will be - validated and converted to a ``MappableConcept``. + :raises ValueError: If ``evidenceOutcome`` exists and is invalid. Or if ``strengthOfEvidenceProvided`` is not provided when ``directionOfEvidenceProvided`` is supports or disputes or if ``strengthOfEvidenceProvided`` is provided when ``directionOfEvidenceProvided`` is neutral """ - cls._validate_direction_of_evidence_provided(values) + self._validate_direction_of_evidence_provided() ccv_code_pattern = r"^((?:OVS1|SBVS1)(?:_(?:not_met|(?:strong|moderate|supporting)))?|(?:OS[1-3]|SBS[1-2])(?:_(?:not_met|(?:very_strong|moderate|supporting)))?|(?:OM[1-4])(?:_(?:not_met|(?:very_strong|strong|supporting)))?|(OP[1-4]|SBP[1-2])(?:_(?:not_met|very_strong|strong|moderate))?)$" - return cls._validate_evidence_outcome(values, SYSTEM, ccv_code_pattern) + return self._validate_evidence_outcome(SYSTEM, ccv_code_pattern) class VariantOncogenicityStudyStatement(Statement):