Skip to content

Commit 16f2ca1

Browse files
committed
feat: replace Digest constants in misctypes.py with cache funcs
Replace`DUMMY_DANDI_ETAG` and `DUMMY_DANDI_ZARR_CHECKSUM` in `dandi.misctypes` with caching functions. This change is needed to delay the import of `dandischema.models` in `dandi.misctypes` and in this package in general
1 parent b151c39 commit 16f2ca1

5 files changed

Lines changed: 20 additions & 13 deletions

File tree

dandi/files/bases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ class LocalAsset(DandiFile):
175175

176176
@staticmethod
177177
def _get_dummy_digest() -> Digest:
178-
from dandi.misctypes import DUMMY_DANDI_ETAG
178+
from dandi.misctypes import get_dummy_dandi_etag
179179

180-
return DUMMY_DANDI_ETAG
180+
return get_dummy_dandi_etag()
181181

182182
@abstractmethod
183183
def get_digest(self) -> Digest:

dandi/files/zarr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,9 @@ class ZarrAsset(LocalDirectoryAsset[LocalZarrEntry]):
365365

366366
@staticmethod
367367
def _get_dummy_digest() -> Digest:
368-
from dandi.misctypes import DUMMY_DANDI_ZARR_CHECKSUM
368+
from dandi.misctypes import get_dummy_dandi_zarr_checksum
369369

370-
return DUMMY_DANDI_ZARR_CHECKSUM
370+
return get_dummy_dandi_zarr_checksum()
371371

372372
@property
373373
def filetree(self) -> LocalZarrEntry:

dandi/metadata/nwb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .. import get_logger
1414
from ..consts import metadata_all_fields
1515
from ..files import bids, dandi_file, find_bids_dataset_description
16-
from ..misctypes import DUMMY_DANDI_ETAG, Digest, LocalReadableFile, Readable
16+
from ..misctypes import Digest, LocalReadableFile, Readable, get_dummy_dandi_etag
1717
from ..pynwb_utils import (
1818
_get_pynwb_metadata,
1919
get_neurodata_types,
@@ -68,7 +68,7 @@ def get_metadata(
6868
)
6969
assert isinstance(df, bids.BIDSAsset)
7070
if not digest:
71-
digest = DUMMY_DANDI_ETAG
71+
digest = get_dummy_dandi_etag()
7272
path_metadata = df.get_metadata(digest=digest)
7373
meta["bids_version"] = df.get_validation_bids_version()
7474
# there might be a more elegant way to do this:

dandi/misctypes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from dataclasses import dataclass
1212
from datetime import datetime
1313
from fnmatch import fnmatchcase
14+
from functools import cache
1415
import os.path
1516
from pathlib import Path
1617
from typing import IO, TypeVar, cast
@@ -54,11 +55,15 @@ def asdict(self) -> dict[DigestType, str]:
5455

5556
#: Placeholder digest used in some situations where a digest is required but
5657
#: not actually relevant and would be too expensive to calculate
57-
DUMMY_DANDI_ETAG = Digest(algorithm=DigestType.dandi_etag, value=32 * "d" + "-1")
58-
DUMMY_DANDI_ZARR_CHECKSUM = Digest(
59-
algorithm=DigestType.dandi_zarr_checksum,
60-
value=32 * "d" + "-1--1",
61-
)
58+
@cache
59+
def get_dummy_dandi_etag() -> Digest:
60+
return Digest(algorithm=DigestType.dandi_etag, value=32 * "d" + "-1")
61+
62+
63+
@cache
64+
def get_dummy_dandi_zarr_checksum() -> Digest:
65+
return Digest(algorithm=DigestType.dandi_zarr_checksum, value=32 * "d" + "-1--1")
66+
6267

6368
P = TypeVar("P", bound="BasePath")
6469

dandi/tests/test_metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
species_map,
5050
timedelta2duration,
5151
)
52-
from ..misctypes import DUMMY_DANDI_ETAG
52+
from ..misctypes import get_dummy_dandi_etag
5353
from ..utils import ensure_datetime
5454

5555
METADATA_DIR = Path(__file__).with_name("data") / "metadata"
@@ -836,7 +836,9 @@ def test_ndtypes(ndtypes, asset_dict):
836836
def test_nwb2asset(simple2_nwb: Path) -> None:
837837
# Classes with ANY_AWARE_DATETIME fields need to be constructed with
838838
# model_construct()
839-
assert nwb2asset(simple2_nwb, digest=DUMMY_DANDI_ETAG) == BareAsset.model_construct(
839+
assert nwb2asset(
840+
simple2_nwb, digest=get_dummy_dandi_etag()
841+
) == BareAsset.model_construct(
840842
schemaKey="Asset",
841843
schemaVersion=DANDI_SCHEMA_VERSION,
842844
keywords=["keyword1", "keyword 2"],

0 commit comments

Comments
 (0)