Skip to content

Commit 43db278

Browse files
authored
Migrate to higlass-schema v0.2.0 (pydantic v2) (#155)
1 parent c16e2b5 commit 43db278

File tree

4 files changed

+65
-31
lines changed

4 files changed

+65
-31
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dynamic = ["version"]
2727
readme = "README.md"
2828
dependencies = [
2929
"servir>=0.0.5",
30-
"higlass-schema>=0.0.6,<0.2.0",
30+
"higlass-schema>=0.2.0",
3131
"anywidget>=0.9.0",
3232
"jinja2",
3333
"jupyter-server-proxy>=3.0",

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.dict())
78+
copy = model.__class__(**model.model_dump())
7979
if hasattr(copy, "uid"):
8080
setattr(copy, "uid", uid())
8181
return copy

src/higlass/api.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@
1414
)
1515

1616
import higlass_schema as hgs
17-
from pydantic import BaseModel
17+
from pydantic import RootModel
1818

1919
import higlass._display as display
2020
import higlass._utils as utils
2121

2222
__all__ = [
23+
"CombinedTrack",
2324
"EnumTrack",
2425
"HeatmapTrack",
2526
"IndependentViewportProjectionTrack",
26-
"CombinedTrack",
2727
"PluginTrack",
2828
"TrackT",
2929
"View",
3030
"ViewT",
3131
"Viewconf",
32+
"combine",
3233
"concat",
34+
"divide",
3335
"hconcat",
34-
"vconcat",
36+
"lock",
3537
"track",
38+
"vconcat",
3639
"view",
37-
"combine",
38-
"divide",
39-
"lock",
4040
]
4141

4242
if TYPE_CHECKING:
@@ -383,13 +383,13 @@ def _repr_mimebundle_(self, include=None, exclude=None):
383383
""" "Displays the view config in an IPython environment."""
384384
renderer = display.renderers.get()
385385
plugin_urls = [] if self.views is None else gather_plugin_urls(self.views)
386-
return renderer(self.dict(), plugin_urls=plugin_urls)
386+
return renderer(self.model_dump(), plugin_urls=plugin_urls)
387387

388388
def widget(self, **kwargs):
389389
"""Create a Jupyter Widget display for this view config."""
390390
from higlass._widget import HiGlassWidget
391391

392-
return HiGlassWidget(self.dict(), **kwargs)
392+
return HiGlassWidget(self.model_dump(), **kwargs)
393393

394394
@classmethod
395395
def from_url(cls, url: str, **kwargs):
@@ -416,7 +416,7 @@ def from_url(cls, url: str, **kwargs):
416416
with urllib.urlopen(request) as response:
417417
raw = response.read()
418418

419-
return cls.parse_raw(raw)
419+
return cls.model_validate_json(raw)
420420

421421
def locks(
422422
self,
@@ -603,7 +603,7 @@ def mapper(view):
603603
raise ValueError("concat method must be 'vertical' or 'horizontal'.")
604604

605605
# gather views and adjust layout
606-
views = [v.copy(deep=True) for v in b.views]
606+
views = [v.model_copy(deep=True) for v in b.views]
607607
offset = 0 if a.views is None else max(map(mapper, a.views))
608608
for view in views:
609609
curr = getattr(view.layout, field)
@@ -616,7 +616,7 @@ def mapper(view):
616616
locks = getattr(b, lockattr)
617617
if locks:
618618
if getattr(a, lockattr) is None:
619-
setattr(a, lockattr, locks.copy(deep=True))
619+
setattr(a, lockattr, locks.model_copy(deep=True))
620620
else:
621621
getattr(a, lockattr).locksByViewUid.update(locks.locksByViewUid)
622622
getattr(a, lockattr).locksDict.update(locks.locksDict)
@@ -634,18 +634,18 @@ def mapper(view):
634634
# TODO: register plugins globally to work here?
635635

636636

637-
class _TrackCreator(BaseModel):
637+
class _TrackCreator(RootModel):
638638
"""Create track instances from their track type.
639639
640640
Used internally by `hg.track` to leverage pydantic's ability to get
641641
the appropriate base model by the track type.
642642
643643
Example:
644644
-------
645-
>>> assert isinstance(_TrackCreator(type="heatmap").__root__, HeatmapTrack)
645+
>>> assert isinstance(_TrackCreator(type="heatmap").root, HeatmapTrack)
646646
"""
647647

648-
__root__: Track
648+
root: Track
649649

650650

651651
@overload
@@ -688,7 +688,7 @@ def track(
688688
if uid is None:
689689
uid = utils.uid()
690690
data = dict(type=type_, uid=uid, **kwargs)
691-
return _TrackCreator.parse_obj(data).__root__
691+
return _TrackCreator.model_validate(data).root
692692

693693

694694
def view(
@@ -745,16 +745,16 @@ def view(
745745
if layout is None:
746746
layout = hgs.Layout(x=x, y=y, w=width, h=height)
747747
else:
748-
layout = hgs.Layout(**layout.dict())
748+
layout = hgs.Layout(**layout.model_dump())
749749

750750
if tracks is None:
751751
data = defaultdict(list)
752752
else:
753-
data = defaultdict(list, tracks.dict())
753+
data = defaultdict(list, tracks.model_dump())
754754

755755
for track in _tracks:
756756
if isinstance(track, hgs.Tracks):
757-
track = track.dict()
757+
track = track.model_dump()
758758
for position, track_list in track.items():
759759
data[position].extend(track_list)
760760
else:
@@ -806,16 +806,16 @@ def combine(t1: Track, t2: Track, uid: str | None = None, **kwargs) -> CombinedT
806806
uid = utils.uid()
807807

808808
if isinstance(t1, CombinedTrack):
809-
copy = CombinedTrack(**t1.dict())
810-
copy.contents.append(t2.__class__(**t2.dict()))
809+
copy = CombinedTrack(**t1.model_dump())
810+
copy.contents.append(t2.__class__(**t2.model_dump()))
811811
for key, val in kwargs.items():
812812
setattr(copy, key, val)
813813
return copy
814814

815815
return CombinedTrack(
816816
type="combined",
817817
uid=uid,
818-
contents=[track.__class__(**track.dict()) for track in (t1, t2)],
818+
contents=[track.__class__(**track.model_dump()) for track in (t1, t2)],
819819
**kwargs,
820820
)
821821

uv.lock

Lines changed: 42 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)