From 7bee159ac9d479475d41e001ecbda2b7aec2f518 Mon Sep 17 00:00:00 2001 From: developer-rpai Date: Wed, 23 Sep 2026 23:07:55 -0700 Subject: [PATCH] fix: resolve VRS model class via getattr in from-VRS translation _from_vrs subscripted the ga4gh.vrs.models module with var["type"], raising TypeError ('module' object is not subscriptable) for any VRS dict input. Resolve the model class with getattr(models, var["type"], None) and return None gracefully for unknown types, preserving the original intent of the KeyError guard. Closes #489 --- src/ga4gh/vrs/extras/translator.py | 5 ++--- tests/extras/test_allele_translator.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ga4gh/vrs/extras/translator.py b/src/ga4gh/vrs/extras/translator.py index 91c45532..b10b62b6 100644 --- a/src/ga4gh/vrs/extras/translator.py +++ b/src/ga4gh/vrs/extras/translator.py @@ -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) diff --git a/tests/extras/test_allele_translator.py b/tests/extras/test_allele_translator.py index 15f87a89..02f0d56c 100644 --- a/tests/extras/test_allele_translator.py +++ b/tests/extras/test_allele_translator.py @@ -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