Skip to content

Commit af10d2c

Browse files
committed
feat: delay import of dandischema.models in dandi.files.bases
1 parent 9a9a7a3 commit af10d2c

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

dandi/files/bases.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pathlib import Path
1111
import re
1212
from threading import Lock
13-
from typing import IO, TYPE_CHECKING, Any, Generic
13+
from typing import IO, TYPE_CHECKING, Any, Generic, TypeVar
1414
from xml.etree.ElementTree import fromstring
1515

1616
import dandischema
@@ -24,7 +24,6 @@
2424
import dandi
2525
from dandi.dandiapi import RemoteAsset, RemoteDandiset, RESTFullAPIClient
2626
from dandi.metadata.core import get_default_metadata
27-
from dandi.misctypes import DUMMY_DANDI_ETAG, Digest, LocalReadableFile, P
2827
from dandi.utils import post_upload_size_check, pre_upload_size_check, yaml_load
2928
from dandi.validate_types import (
3029
ORIGIN_INTERNAL_DANDI,
@@ -42,6 +41,11 @@
4241
from dandischema.models import BareAsset, CommonModel
4342
from dandischema.models import Dandiset as DandisetMeta
4443

44+
# noinspection PyUnresolvedReferences
45+
from dandi.misctypes import BasePath, Digest, LocalReadableFile
46+
47+
P = TypeVar("P", bound="BasePath")
48+
4549
lgr = dandi.get_logger()
4650

4751
# TODO -- should come from schema. This is just a simplistic example for now
@@ -153,6 +157,8 @@ def as_readable(self) -> LocalReadableFile:
153157
154158
Returns a `Readable` instance wrapping the local file
155159
"""
160+
from dandi.misctypes import LocalReadableFile
161+
156162
return LocalReadableFile(self.filepath)
157163

158164

@@ -167,7 +173,11 @@ class LocalAsset(DandiFile):
167173
#: (i.e., relative to the Dandiset's root)
168174
path: str
169175

170-
_DUMMY_DIGEST = DUMMY_DANDI_ETAG
176+
@staticmethod
177+
def _get_dummy_digest() -> Digest:
178+
from dandi.misctypes import DUMMY_DANDI_ETAG
179+
180+
return DUMMY_DANDI_ETAG
171181

172182
@abstractmethod
173183
def get_digest(self) -> Digest:
@@ -202,7 +212,7 @@ def get_validation_errors(
202212
f"Unsupported schema version: {schema_version}; expected {current_version}"
203213
)
204214
try:
205-
asset = self.get_metadata(digest=self._DUMMY_DIGEST)
215+
asset = self.get_metadata(digest=self._get_dummy_digest())
206216
BareAsset(**asset.model_dump())
207217
except ValidationError as e:
208218
if devel_debug:
@@ -317,6 +327,7 @@ def get_metadata(
317327
def get_digest(self) -> Digest:
318328
"""Calculate a dandi-etag digest for the asset"""
319329
# Avoid heavy import by importing within function:
330+
from dandi.misctypes import Digest
320331
from dandi.support.digests import get_digest
321332

322333
value = get_digest(self.filepath, digest="dandi-etag")
@@ -484,6 +495,8 @@ def as_readable(self) -> LocalReadableFile:
484495
485496
Returns a `Readable` instance wrapping the local file
486497
"""
498+
from dandi.misctypes import LocalReadableFile
499+
487500
return LocalReadableFile(self.filepath)
488501

489502

0 commit comments

Comments
 (0)