Skip to content

Commit b29dacf

Browse files
valentin-pinkauvalentin-pinkau
andauthored
Change the default path type to UPath (#1326)
* changed the default path type to UPath * remove isinstance check * lint * implement feedback * add Changelog.md --------- Co-authored-by: valentin-pinkau <[email protected]>
1 parent ebac5d9 commit b29dacf

File tree

12 files changed

+172
-159
lines changed

12 files changed

+172
-159
lines changed

webknossos/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ For upgrade instructions, please check the respective _Breaking Changes_ section
1717
### Added
1818

1919
### Changed
20+
- Changed the default path type to `UPath` [#1326](https://github.com/scalableminds/webknossos-libs/pull/1326)
2021

2122
### Fixed
2223

webknossos/tests/conftest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gc
22
import os
33
import warnings
4-
from collections.abc import Generator
4+
from collections.abc import Generator, Iterator
55
from os import makedirs
66
from pathlib import Path
77
from shutil import rmtree, unpack_archive
@@ -11,6 +11,7 @@
1111
import httpx
1212
import pytest
1313
from hypothesis import strategies as st
14+
from upath import UPath
1415

1516
import webknossos as wk
1617
from webknossos.client._upload_dataset import _cached_get_upload_datastore
@@ -19,6 +20,11 @@
1920
from .constants import TESTDATA_DIR, TESTOUTPUT_DIR
2021

2122

23+
@pytest.fixture()
24+
def tmp_upath(tmp_path: Path) -> Iterator[UPath]:
25+
yield UPath(tmp_path)
26+
27+
2228
def pytest_make_parametrize_id(config: Any, val: Any, argname: str) -> Any:
2329
del config
2430
del argname

webknossos/tests/constants.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import sys
55
from collections.abc import Iterator
66
from contextlib import contextmanager
7-
from pathlib import Path
87
from time import sleep
98

109
from upath import UPath
1110

1211
from webknossos.utils import rmtree
1312

14-
TESTDATA_DIR = Path(__file__).parent.parent / "testdata"
15-
TESTOUTPUT_DIR = Path(__file__).parent.parent / "testoutput"
13+
TESTDATA_DIR = UPath(__file__).parent.parent / "testdata"
14+
TESTOUTPUT_DIR = UPath(__file__).parent.parent / "testoutput"
1615

1716

1817
MINIO_ROOT_USER = "TtnuieannGt2rGuie2t8Tt7urarg5nauedRndrur"
@@ -31,7 +30,7 @@
3130
def use_minio() -> Iterator[None]:
3231
"""Minio is an S3 clone and is used as local test server"""
3332
if sys.platform == "darwin":
34-
minio_path = Path("testoutput_minio")
33+
minio_path = UPath("testoutput_minio")
3534
rmtree(minio_path)
3635
minio_process = subprocess.Popen(
3736
shlex.split(f"minio server --address :8000 ./{minio_path}"),

webknossos/tests/dataset/test_attachments.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from webknossos.geometry import BoundingBox
1616

1717

18-
def make_dataset(dataset_path: Path) -> tuple[Dataset, SegmentationLayer]:
18+
def make_dataset(dataset_path: UPath) -> tuple[Dataset, SegmentationLayer]:
1919
dataset = Dataset(dataset_path / "test_attachments", voxel_size=(10, 10, 10))
2020
seg_layer = dataset.add_layer(
2121
"seg",
@@ -26,8 +26,8 @@ def make_dataset(dataset_path: Path) -> tuple[Dataset, SegmentationLayer]:
2626
return dataset, seg_layer
2727

2828

29-
def test_attachments(tmp_path: Path) -> None:
30-
dataset, seg_layer = make_dataset(tmp_path)
29+
def test_attachments(tmp_upath: UPath) -> None:
30+
dataset, seg_layer = make_dataset(tmp_upath)
3131

3232
# meshes
3333
seg_layer.attachments.add_mesh(
@@ -142,14 +142,14 @@ def test_attachments(tmp_path: Path) -> None:
142142
assert "attachments" not in properties_json["dataLayers"][0]
143143

144144

145-
def test_copy_layer(tmp_path: Path) -> None:
146-
dataset, seg_layer = make_dataset(tmp_path)
145+
def test_copy_layer(tmp_upath: UPath) -> None:
146+
dataset, seg_layer = make_dataset(tmp_upath)
147147

148148
mesh_path = dataset.path / "seg" / "meshes" / "meshfile.hdf5"
149149
mesh_path.parent.mkdir(parents=True, exist_ok=True)
150150
mesh_path.write_text("test")
151151

152-
agglomerate_path = (tmp_path / "agglomerate_view_15").resolve()
152+
agglomerate_path = (tmp_upath / "agglomerate_view_15").resolve()
153153
agglomerate_path.mkdir(parents=True, exist_ok=True)
154154
(agglomerate_path / "zarr.json").write_text("test")
155155

@@ -164,7 +164,7 @@ def test_copy_layer(tmp_path: Path) -> None:
164164
data_format=AttachmentDataFormat.Zarr3,
165165
)
166166

167-
copy_dataset = Dataset(tmp_path / "test_copy", voxel_size=(10, 10, 10))
167+
copy_dataset = Dataset(tmp_upath / "test_copy", voxel_size=(10, 10, 10))
168168
copy_layer = copy_dataset.add_copy_layer(seg_layer).as_segmentation_layer()
169169

170170
assert (
@@ -189,14 +189,14 @@ def test_copy_layer(tmp_path: Path) -> None:
189189
)
190190

191191

192-
def test_fs_copy_layer(tmp_path: Path) -> None:
193-
dataset, seg_layer = make_dataset(tmp_path)
192+
def test_fs_copy_layer(tmp_upath: UPath) -> None:
193+
dataset, seg_layer = make_dataset(tmp_upath)
194194

195195
mesh_path = dataset.path / "seg" / "meshes" / "meshfile.hdf5"
196196
mesh_path.parent.mkdir(parents=True, exist_ok=True)
197197
mesh_path.write_text("test")
198198

199-
agglomerate_path = (tmp_path / "agglomerate_view_15").resolve()
199+
agglomerate_path = (tmp_upath / "agglomerate_view_15").resolve()
200200
agglomerate_path.mkdir(parents=True, exist_ok=True)
201201
(agglomerate_path / "zarr.json").write_text("test")
202202

@@ -212,7 +212,7 @@ def test_fs_copy_layer(tmp_path: Path) -> None:
212212
data_format=AttachmentDataFormat.Zarr3,
213213
)
214214

215-
copy_dataset = Dataset(tmp_path / "test_copy", voxel_size=(10, 10, 10))
215+
copy_dataset = Dataset(tmp_upath / "test_copy", voxel_size=(10, 10, 10))
216216
copy_layer = copy_dataset.add_fs_copy_layer(seg_layer).as_segmentation_layer()
217217

218218
# has been copied
@@ -229,14 +229,14 @@ def test_fs_copy_layer(tmp_path: Path) -> None:
229229
)
230230

231231

232-
def test_symlink_layer(tmp_path: Path) -> None:
233-
dataset, seg_layer = make_dataset(tmp_path)
232+
def test_symlink_layer(tmp_upath: UPath) -> None:
233+
dataset, seg_layer = make_dataset(tmp_upath)
234234

235235
mesh_path = dataset.path / "seg" / "meshes" / "meshfile.hdf5"
236236
mesh_path.parent.mkdir(parents=True, exist_ok=True)
237237
mesh_path.write_text("test")
238238

239-
agglomerate_path = (tmp_path / "agglomerate_view_15").resolve()
239+
agglomerate_path = (tmp_upath / "agglomerate_view_15").resolve()
240240
agglomerate_path.mkdir(parents=True, exist_ok=True)
241241
(agglomerate_path / "zarr.json").write_text("test")
242242

@@ -252,7 +252,7 @@ def test_symlink_layer(tmp_path: Path) -> None:
252252
data_format=AttachmentDataFormat.Zarr3,
253253
)
254254

255-
copy_dataset = Dataset(tmp_path / "test_copy", voxel_size=(10, 10, 10))
255+
copy_dataset = Dataset(tmp_upath / "test_copy", voxel_size=(10, 10, 10))
256256
copy_layer = copy_dataset.add_symlink_layer(
257257
seg_layer, make_relative=True
258258
).as_segmentation_layer()
@@ -276,8 +276,8 @@ def test_symlink_layer(tmp_path: Path) -> None:
276276
)
277277

278278

279-
def test_remote_layer(tmp_path: Path) -> None:
280-
dataset, seg_layer = make_dataset(tmp_path)
279+
def test_remote_layer(tmp_upath: UPath) -> None:
280+
dataset, seg_layer = make_dataset(tmp_upath)
281281

282282
mesh_path = UPath(
283283
"s3://bucket/meshfile.zarr",
@@ -290,7 +290,7 @@ def test_remote_layer(tmp_path: Path) -> None:
290290
data_format=AttachmentDataFormat.Zarr3,
291291
)
292292

293-
copy_dataset = Dataset(tmp_path / "test_copy", voxel_size=(10, 10, 10))
293+
copy_dataset = Dataset(tmp_upath / "test_copy", voxel_size=(10, 10, 10))
294294
copy_layer = copy_dataset.add_remote_layer(seg_layer).as_segmentation_layer()
295295

296296
assert copy_layer.attachments.meshes[0].path == mesh_path
@@ -301,8 +301,8 @@ def test_remote_layer(tmp_path: Path) -> None:
301301
)
302302

303303

304-
def test_upload_fail(tmp_path: Path) -> None:
305-
dataset, seg_layer = make_dataset(tmp_path)
304+
def test_upload_fail(tmp_upath: UPath) -> None:
305+
dataset, seg_layer = make_dataset(tmp_upath)
306306
seg_layer.attachments.add_mesh(
307307
dataset.path / "seg" / "meshfile",
308308
name="meshfile",
@@ -313,8 +313,8 @@ def test_upload_fail(tmp_path: Path) -> None:
313313
dataset.upload()
314314

315315

316-
def test_unique_attachment_names(tmp_path: Path) -> None:
317-
dataset, seg_layer = make_dataset(tmp_path)
316+
def test_unique_attachment_names(tmp_upath: UPath) -> None:
317+
dataset, seg_layer = make_dataset(tmp_upath)
318318

319319
seg_layer.attachments.add_mesh(
320320
UPath("http://example.com/meshfile.zarr"),
@@ -329,8 +329,8 @@ def test_unique_attachment_names(tmp_path: Path) -> None:
329329
)
330330

331331

332-
def test_acceptable_attachment_names(tmp_path: Path) -> None:
333-
dataset, seg_layer = make_dataset(tmp_path)
332+
def test_acceptable_attachment_names(tmp_upath: UPath) -> None:
333+
dataset, seg_layer = make_dataset(tmp_upath)
334334

335335
with pytest.raises(ValueError):
336336
seg_layer.attachments.add_mesh(
@@ -356,8 +356,8 @@ def test_remote_dataset() -> None:
356356
assert seg_layer.attachments.meshes[0]._properties.path == "seg/meshes/meshfile"
357357

358358

359-
def test_add_attachments(tmp_path: Path) -> None:
360-
dataset, seg_layer = make_dataset(tmp_path)
359+
def test_add_attachments(tmp_upath: UPath) -> None:
360+
dataset, seg_layer = make_dataset(tmp_upath)
361361

362362
mesh = MeshAttachment.from_path_and_name(
363363
dataset.path / "seg" / "meshes" / "meshfile",
@@ -370,11 +370,11 @@ def test_add_attachments(tmp_path: Path) -> None:
370370
assert seg_layer.attachments.meshes[0].name == "meshfile_4-4-1"
371371

372372

373-
def test_add_copy_attachments(tmp_path: Path) -> None:
374-
dataset, seg_layer = make_dataset(tmp_path)
373+
def test_add_copy_attachments(tmp_upath: UPath) -> None:
374+
dataset, seg_layer = make_dataset(tmp_upath)
375375

376376
# meshes
377-
mesh_path = tmp_path / "meshfile"
377+
mesh_path = tmp_upath / "meshfile"
378378
mesh_path.write_text("test")
379379

380380
mesh = MeshAttachment.from_path_and_name(
@@ -393,7 +393,7 @@ def test_add_copy_attachments(tmp_path: Path) -> None:
393393
assert mesh_path.exists()
394394

395395
# segment index (has camel-casing)
396-
segment_index_path = tmp_path / "segment_index"
396+
segment_index_path = tmp_upath / "segment_index"
397397
segment_index_path.write_text("test")
398398

399399
segment_index = SegmentIndexAttachment.from_path_and_name(
@@ -408,11 +408,11 @@ def test_add_copy_attachments(tmp_path: Path) -> None:
408408
)
409409

410410

411-
def test_add_symlink_attachments(tmp_path: Path) -> None:
412-
dataset, seg_layer = make_dataset(tmp_path)
411+
def test_add_symlink_attachments(tmp_upath: UPath) -> None:
412+
dataset, seg_layer = make_dataset(tmp_upath)
413413

414414
# meshes
415-
mesh_path = tmp_path.resolve() / "meshfile"
415+
mesh_path = tmp_upath.resolve() / "meshfile"
416416
mesh_path.write_text("test")
417417

418418
mesh = MeshAttachment.from_path_and_name(
@@ -430,7 +430,7 @@ def test_add_symlink_attachments(tmp_path: Path) -> None:
430430
assert mesh_path.exists()
431431

432432
# segment index (has camel-casing)
433-
segment_index_path = tmp_path / "segment_index"
433+
segment_index_path = tmp_upath / "segment_index"
434434
segment_index_path.write_text("test")
435435

436436
segment_index = SegmentIndexAttachment.from_path_and_name(
@@ -450,8 +450,8 @@ def test_add_symlink_attachments(tmp_path: Path) -> None:
450450
).resolve() == segment_index_path
451451

452452

453-
def test_detect_legacy_attachments(tmp_path: Path) -> None:
454-
_, seg_layer = make_dataset(tmp_path)
453+
def test_detect_legacy_attachments(tmp_upath: UPath) -> None:
454+
_, seg_layer = make_dataset(tmp_upath)
455455

456456
# legacy meshes
457457
mesh_path = seg_layer.path / "meshes" / "meshfile_4-4-1.hdf5"

0 commit comments

Comments
 (0)