diff --git a/src/ga4gh/va_spec/acmg_2015/models.py b/src/ga4gh/va_spec/acmg_2015/models.py index 28f3957..b3d32e4 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 ( @@ -132,22 +133,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 fdf3afb..b02755e 100644 --- a/src/ga4gh/va_spec/base/core.py +++ b/src/ga4gh/va_spec/base/core.py @@ -14,6 +14,7 @@ StringConstraints, field_validator, ) +from typing_extensions import Self from ga4gh.cat_vrs.models import CategoricalVariant from ga4gh.core.models import ( @@ -603,54 +604,48 @@ class EvidenceLine(InformationEntity, BaseModelForbidExtra): description="A term summarizing the overall outcome of the evidence assessment represented by the Evidence Line, in terms of the direction and strength of support it provides for or against the target Proposition.", ) - @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 ad01cba..a04849c 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 ( @@ -109,22 +110,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 VariantOncogenicityStatement(Statement):