Skip to content

fix: dereference list attributes such as CisPhasedBlock.members - #698

Open
syahra712 wants to merge 1 commit into
ga4gh:mainfrom
syahra712:fix/deref-list-references
Open

syahra712 wants to merge 1 commit into
ga4gh:mainfrom
syahra712:fix/deref-list-references

Conversation

@syahra712

Copy link
Copy Markdown
Contributor

vrs_deref fails on any object whose referable attribute is a list, so a CisPhasedBlock can't be round-tripped through vrs_enref / vrs_deref:

from ga4gh.vrs import models, vrs_deref, vrs_enref

store = {}
enreffed = vrs_enref(cis_phased_block, object_store=store)  # members -> ga4gh:VA... ids
vrs_deref(enreffed, object_store=store)
# KeyError: "root='ga4gh:VA.gDM6r-orysWPX-4NuEOx-KaWcyrlTv_t'"

ga4gh_deref copies the object first, which turns the member ids into iriReference models. The list branch then looks each one up with object_store[str(curie)], and str() of a pydantic RootModel is "root='ga4gh:VA...'", not the id. The single-value branch already unwraps with get_pydantic_root. The list branch also assumed every item was a reference, so a list of members that were already inlined raised KeyError too.

The list branch now handles each item like the single-value branch does. A GA4GH identifier is unwrapped with get_pydantic_root and looked up in the store, and an inlined object is passed through as-is. CisPhasedBlock.members, Adjacency.adjoinedSequences and DerivativeMolecule.components all go through this path.

Tests

test_enref_deref_list_attribute in tests/test_vrs.py round-trips the existing cpb_431012 block and checks that already-inlined members pass through. It fails on main with the KeyError above and passes with this change. ruff check and ruff format --check (v0.9.4) are clean. The rest of the suite passes locally, except tests that need a local SeqRepo at /usr/local/share/seqrepo (test_dataproxy, test_vrs_normalize), which I don't have; this change doesn't touch them.

@arieleven arieleven left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right to me. members is list[Allele | iriReference], so on main an already-inlined member hits object_store[str(allele)] and raises KeyError; resolving only GA4GH identifiers and passing the rest through fixes it, and the round-trip test covers both forms.

Two notes:

  1. This pairs with #699, which fixes the enref side of the same list branch. They touch neighbouring lines in enderef.py and both append to the end of tests/test_vrs.py, so they'll conflict trivially. Whichever lands second could add a mixed case: a block with one inlined Allele and one existing ga4gh:VA... reference, enref'd then deref'd.
  2. A pre-existing behaviour this extends to lists: _deref works in place on the objects it takes from object_store. After ga4gh_deref, the store's member alleles are themselves dereffed and are the same objects as the returned members, so mutating the result mutates the store. Probably fine for the in-memory dict use, but pydantic_copy on the looked-up object (or a note in the docstring) would avoid surprises. While there, _deref's docstring says it returns the id, but it returns the object.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants