Skip to content

Commit ae44c06

Browse files
committed
fix: Use deepcopy instead of model_dump to copy view
1 parent c4a8685 commit ae44c06

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import uuid
44
from typing import Literal, TypeVar, Union
5-
5+
from copy import deepcopy
66
import higlass_schema as hgs
77
from pydantic import BaseModel
88

@@ -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 = deepcopy(model)
7979
if hasattr(copy, "uid"):
8080
setattr(copy, "uid", uid())
8181
return copy

test/test_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import higlass as hg
6+
from typing import Literal, ClassVar
67

78

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

0 commit comments

Comments
 (0)