Skip to content

Commit 7e18e66

Browse files
committed
fix: handle key error in translate_to
close #638 * Unsupported `fmt` in `translate_to` should raise `NotImplementedError`
1 parent a50b9fb commit 7e18e66

2 files changed

Lines changed: 11 additions & 18 deletions

File tree

src/ga4gh/vrs/extras/translator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ def translate_to(self, vo: models._VariationBase, fmt: str, **kwargs) -> list[st
142142
kwargs:
143143
ref_seq_limit Optional(int):
144144
If vo.state is a ReferenceLengthExpression, and `ref_seq_limit` is specified, and `fmt` is `spdi`, the reference sequence is included in the SPDI expression if it is below the limit Otherwise only the length of the reference sequence is included. If the limit is None, the reference sequence is always included. In all cases, the alt sequence is included. Default is 0 (never include reference sequence).
145+
:raise NotImplementedError: If `fmt` is not supported
145146
"""
146-
t = self.to_translators[fmt]
147+
try:
148+
t = self.to_translators[fmt]
149+
except KeyError as e:
150+
msg = f"{fmt} is not supported"
151+
raise NotImplementedError(msg) from e
152+
147153
return t(vo, **kwargs)
148154

149155
############################################################################

tests/extras/test_allele_translator.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -978,20 +978,7 @@ def test_normalize_microsatellite_counts(tlr, case):
978978
)
979979

980980

981-
# TODO: Readd these tests
982-
# @pytest.mark.vcr
983-
# def test_errors(tlr):
984-
# with pytest.raises(ValueError):
985-
# tlr._from_beacon("bogus")
986-
#
987-
# with pytest.raises(ValueError):
988-
# tlr._from_gnomad("NM_182763.2:c.688+403C>T")
989-
#
990-
# with pytest.raises(ValueError):
991-
# tlr._from_hgvs("NM_182763.2:c.688+403C>T")
992-
#
993-
# with pytest.raises(ValueError):
994-
# tlr._from_hgvs("NM_182763.2:c.688_690inv")
995-
#
996-
# with pytest.raises(ValueError):
997-
# tlr._from_spdi("NM_182763.2:c.688+403C>T")
981+
@pytest.mark.vcr
982+
def test_translate_to_invalid_fmt(tlr):
983+
with pytest.raises(NotImplementedError, match="gnomad is not supported"):
984+
tlr.translate_to(models.Allele.model_validate(snv_output), fmt="gnomad")

0 commit comments

Comments
 (0)