Skip to content
Open
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
5 changes: 2 additions & 3 deletions src/ga4gh/vrs/extras/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ def _from_vrs(self, var: dict, **kwargs) -> models._VariationBase | None: # noq
return None
if "type" not in var:
return None
try:
model = models[var["type"]]
except KeyError:
model = getattr(models, var["type"], None)
if model is None:
return None
return model(**var)

Expand Down
23 changes: 23 additions & 0 deletions tests/extras/test_allele_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,3 +982,26 @@ def test_normalize_microsatellite_counts(tlr, case):
def test_translate_to_invalid_fmt(tlr):
with pytest.raises(NotImplementedError, match="gnomad is not supported"):
tlr.translate_to(models.Allele.model_validate(snv_output), fmt="gnomad")


def test_from_vrs_dict():
"""Regression test for ga4gh/vrs-python#489.

Translating a VRS dict must resolve the model class from the `models`
module via getattr (modules are not subscriptable) instead of crashing
with TypeError; unknown types return None gracefully.
"""
tlr = AlleleTranslator(data_proxy=None, identify=False)

# valid VRS dict translates to a VRS object (previously raised TypeError)
allele = tlr.translate_from(snv_output, fmt="vrs")
assert isinstance(allele, models.Allele)
assert allele.type == "Allele"
assert allele.location.start == snv_output["location"]["start"]
assert allele.location.end == snv_output["location"]["end"]

# unknown type returns None rather than raising
assert tlr._from_vrs({"type": "NotARealModel"}) is None
# non-dict and missing-type inputs still return None
assert tlr._from_vrs("NC_000019.10:g.44908822C>T") is None
assert tlr._from_vrs({"location": {}}) is None