Skip to content

Commit bbf3a01

Browse files
authored
Merge pull request #163 from higlass/kerpedji/deepcopy
fix: Use model_copy instead of model_dump to copy view
2 parents c4a8685 + 350e134 commit bbf3a01

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## [v1.0.2](https://github.com/higlass/higlass-python/compare/v1.0.2...v1.0.1)
22

33
- Calculate file pointer hash for track uids for tileset tracks
4+
- Fix view copy behavior to preserve specific plugin track class vars
45

56
## [v1.0.1](https://github.com/higlass/higlass-python/compare/v1.0.1...v1.0.0)
67

src/higlass/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def ensure_list(x: T | list[T] | None) -> list[T]:
7575

7676
def copy_unique(model: ModelT) -> ModelT:
7777
"""Creates a deep copy of a pydantic BaseModel with new UID."""
78-
copy = model.__class__(**model.model_dump())
78+
copy = model.model_copy(deep=True)
7979
if hasattr(copy, "uid"):
8080
setattr(copy, "uid", uid())
8181
return copy

test/test_api.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import ClassVar, Literal
4+
35
import pytest
46

57
import higlass as hg
@@ -205,3 +207,37 @@ def test_options_mixin():
205207
assert track is other
206208
assert track.uid == other.uid
207209
assert track.options and track.options["foo"] == "bar"
210+
211+
212+
def test_plugin_track():
213+
"""Test that plugin track attributes are maintained after a copy."""
214+
some_url = "https://some_url"
215+
216+
# Here we'll create a custom plugin track.
217+
class PileupTrack(hg.PluginTrack):
218+
type: Literal["pileup"] = "pileup"
219+
plugin_url: ClassVar[str] = some_url
220+
221+
# Specify the track-specific data
222+
pileup_data = {
223+
"type": "bam",
224+
"url": "https://some_url/sorted.bam",
225+
"chromSizesUrl": "https://some_url/sorted.chromsizes.bam",
226+
"options": {"maxTileWidth": 30000},
227+
}
228+
229+
# Create and use the custom track
230+
pileup_track = PileupTrack(data=pileup_data)
231+
232+
view = hg.view((pileup_track, "top"))
233+
uid1 = view.uid
234+
assert view.tracks.top[0].plugin_url == some_url
235+
236+
# The .domain() function creates a copy of the view. We want to make sure
237+
# that the plugin_url attribute of the PluginTrack is maintained
238+
view = view.domain(x=[0, 10])
239+
uid2 = view.uid
240+
assert view.tracks.top[0].plugin_url == some_url
241+
242+
# Check to make sure the copy behavior changed the uid as expected
243+
assert uid1 != uid2

0 commit comments

Comments
 (0)