Skip to content

Commit

Permalink
fix: Use deepcopy instead of model_dump to copy view
Browse files Browse the repository at this point in the history
  • Loading branch information
pkerpedjiev committed Jan 25, 2025
1 parent c4a8685 commit 1aec781
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [v1.0.2](https://github.com/higlass/higlass-python/compare/v1.0.2...v1.0.1)

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/higlass/_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import uuid
from copy import deepcopy
from typing import Literal, TypeVar, Union

import higlass_schema as hgs
Expand Down Expand Up @@ -75,7 +76,7 @@ def ensure_list(x: T | list[T] | None) -> list[T]:

def copy_unique(model: ModelT) -> ModelT:
"""Creates a deep copy of a pydantic BaseModel with new UID."""
copy = model.__class__(**model.model_dump())
copy = deepcopy(model)
if hasattr(copy, "uid"):
setattr(copy, "uid", uid())
return copy
36 changes: 36 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from typing import ClassVar, Literal

import pytest

import higlass as hg
Expand Down Expand Up @@ -205,3 +207,37 @@ def test_options_mixin():
assert track is other
assert track.uid == other.uid
assert track.options and track.options["foo"] == "bar"


def test_plugin_track():
"""Test that plugin track attributes are maintained after a copy."""
some_url = "https://some_url"

# Here we'll create a custom plugin track.
class PileupTrack(hg.PluginTrack):
type: Literal["pileup"] = "pileup"
plugin_url: ClassVar[str] = some_url

# Specify the track-specific data
pileup_data = {
"type": "bam",
"url": "https://some_url/sorted.bam",
"chromSizesUrl": "https://some_url/sorted.chromsizes.bam",
"options": {"maxTileWidth": 30000},
}

# Create and use the custom track
pileup_track = PileupTrack(data=pileup_data)

view = hg.view((pileup_track, "top"))
uid1 = view.uid
assert view.tracks.top[0].plugin_url == some_url

# The .domain() function creates a copy of the view. We want to make sure
# that the plugin_url attribute of the PluginTrack is maintained
view = view.domain(x=[0, 10])
uid2 = view.uid
assert view.tracks.top[0].plugin_url == some_url

# Check to make sure the copy behavior changed the uid as expected
assert uid1 != uid2

0 comments on commit 1aec781

Please sign in to comment.