fix: Use deepcopy instead of model_dump to copy view#163
Merged
pkerpedjiev merged 1 commit intomainfrom Jan 27, 2025
Merged
Conversation
4370b05 to
1aec781
Compare
nvictus
reviewed
Jan 25, 2025
src/higlass/_utils.py
Outdated
| 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) |
Member
There was a problem hiding this comment.
Not familiar enough to know the consequences of deepcopy on a pydantic model, but pydantic appears to have this API:
Suggested change
| copy = deepcopy(model) | |
| copy = model.copy(deep=True) |
Member
Author
There was a problem hiding this comment.
So that works too. I don't know what the difference is either but I switched over to model.model_copy (deep=True) (model.copy is deprecated).
1aec781 to
95d3c3f
Compare
95d3c3f to
350e134
Compare
manzt
approved these changes
Jan 27, 2025
Member
manzt
left a comment
There was a problem hiding this comment.
Looks good to me! I'm surprised by the hack I had there before (rather than using .copy(deep=True). This is certainly an improvement.
Comment on lines
+212
to
+243
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Calling the
.domain()function on a view creates a copy of the view. The current copy method dumps the view model and creates a new one from the view. Because views expect PluginTrack types instead of specific subclasses, the dumped model does not contain the class vars of the subclasses (e.g. PileupTrack). Switching todeepcopypreserves the class vars of the subclasses.Here's the error that occurs in the added test without this change:
Fixes #___
Checklist