Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/anyvlm/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from anyvar.utils.liftover_utils import ReferenceAssembly
from ga4gh.va_spec.base import CohortAlleleFrequencyStudyResult
from pydantic import BaseModel, BeforeValidator, StringConstraints
from pydantic import AfterValidator, BaseModel, BeforeValidator, StringConstraints


class AncillaryResults(BaseModel):
Expand Down Expand Up @@ -93,12 +93,12 @@ def _normalize_chromosome_name(chromosome_name: str) -> str:
return chromosome_name

raise ValueError(
"Invalid chromosome. Must be either a number between 1-22, or "
"'one of the values 'X', 'Y', or 'MT'; optionally prefixed with 'chr'."
"Invalid chromosome name. Must be a string consisting of either a number between 1-22, "
"or one of the values 'X', 'Y', or 'MT'; optionally prefixed with 'chr'."
)


ChromosomeName = Annotated[str, BeforeValidator(_normalize_chromosome_name)]
ChromosomeName = Annotated[str, AfterValidator(_normalize_chromosome_name)]


class Zygosity(StrEnum):
Expand Down
39 changes: 21 additions & 18 deletions tests/unit/test_types → tests/unit/test_types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests for types and validator functions found in src/anyvlm/utils/types.py"""

from pydantic import TypeAdapter, ValidationError
import pytest
from pydantic import TypeAdapter, ValidationError

from anyvlm.utils.types import ChromosomeName, _normalize_chromosome_name


@pytest.fixture
def valid_chromosomes():
def valid_chromosomes() -> list[tuple[str, str]]:
"""List of tuples of unnormalized chromosome names & expected normalized values"""
return [
("1", "1"),
("22", "22"),
Expand All @@ -19,23 +21,14 @@ def valid_chromosomes():
("chrMT", "MT"),
]


@pytest.fixture
def invalid_chromosomes():
return [
"0",
"23",
"chr23",
"M",
"chrM",
"XY",
"",
"chr",
"1a",
None
]
def invalid_chromosomes() -> list[str | None]:
return ["0", "23", "chr23", "M", "chrM", "XY", "", "chr", "1a", None]


@pytest.fixture
def chromosome_adapter():
def chromosome_adapter() -> TypeAdapter[ChromosomeName]:
return TypeAdapter(ChromosomeName)


Expand All @@ -47,8 +40,18 @@ def test_normalize_chromosome_name_valid(valid_chromosomes):

def test_normalize_chromosome_name_invalid(invalid_chromosomes):
for chromosome_name in invalid_chromosomes:
with pytest.raises(ValueError):
_normalize_chromosome_name(chromosome_name)
if chromosome_name is not None:
with pytest.raises(
ValueError,
match=(
"Invalid chromosome name. Must be a string consisting of either a number between 1-22, "
"or one of the values 'X', 'Y', or 'MT'; optionally prefixed with 'chr'."
),
):
_normalize_chromosome_name(chromosome_name)
else:
with pytest.raises(AttributeError):
_normalize_chromosome_name(chromosome_name)


### Test ChromosomeName annotated type ###
Expand Down