Skip to content

Commit 5661de7

Browse files
committedMar 12, 2025·
Fix file loading
1 parent 0aff61b commit 5661de7

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed
 

‎CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3131
#### Bug Fixes
3232

3333
- Fix the random silent failure bug when ibis creates tables.
34-
- Remove a bunch of dead code with vulture
34+
- Remove a bunch of dead code with vulture
3535

3636
## [0.5.0](https://github.com/superduper-io/superduper/compare/0.5.0...0.4.0]) (2024-Nov-02)
3737

‎superduper/base/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def decode(r, db: t.Optional['Datalayer'] = None):
213213
from superduper.misc.importing import import_object
214214

215215
cls = import_object(r['_path'])
216+
216217
r = Document.decode(r, schema=cls.class_schema, db=db)
217218
return cls.from_dict(r, db=None)
218219

@@ -234,7 +235,7 @@ def encode(
234235
for k, v in kwargs.items():
235236
setattr(context, k, v)
236237

237-
r = self.dict() # metadata=context.metadata, defaults=context.defaults)
238+
r = self.dict()
238239
r = self.class_schema.encode_data(r, context=context)
239240

240241
def _replace_loads_with_references(record, lookup):
@@ -273,7 +274,6 @@ def _replace_uuids_with_keys(record):
273274
del r['uuid']
274275
r = _replace_uuids_with_keys(r)
275276

276-
# TODO deprecate this wrapper (not needed)
277277
return {
278278
**r,
279279
KEY_BUILDS: context.builds,

‎superduper/base/datatype.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def _decode_leaf(r, builds, db: t.Optional['Datalayer'] = None):
211211
db=db,
212212
)
213213
else:
214-
assert issubclass(cls, Base)
214+
mro = [f'{x.__module__}.{x.__name__}' for x in cls.__mro__]
215+
216+
assert issubclass(cls, Base) or 'superduper.base.base.Base' in mro
215217
dict_ = cls.class_schema.decode_data(dict_, builds=builds, db=db)
216218
out = cls.from_dict(dict_, db=db)
217219

@@ -272,7 +274,7 @@ def encode_data(self, item, context):
272274
:param context: A context object containing caches.
273275
"""
274276
assert isinstance(item, list)
275-
return [
277+
out = [
276278
(
277279
ComponentType().encode_data(
278280
r,
@@ -283,6 +285,7 @@ def encode_data(self, item, context):
283285
)
284286
for r in item
285287
]
288+
return out
286289

287290
def decode_data(self, item, builds, db):
288291
"""Decode the item from `bytes`.
@@ -310,7 +313,8 @@ def encode_data(self, item, context):
310313
:param context: A context object containing caches.
311314
"""
312315
assert isinstance(item, dict)
313-
return {k: File().encode_data(v, context) for k, v in item.items()}
316+
out = {k: File().encode_data(v, context) for k, v in item.items()}
317+
return out
314318

315319
def decode_data(self, item, builds, db):
316320
"""Decode the item from `bytes`.
@@ -633,9 +637,11 @@ def encode_data(self, item, context):
633637
:param context: A context object containing caches.
634638
"""
635639
if isinstance(item, FileItem):
636-
return item.reference
637-
assert os.path.exists(item)
638-
file = FileItem(identifier=self.hash(item), path=item)
640+
file = item
641+
else:
642+
assert os.path.exists(item)
643+
file = FileItem(identifier=self.hash(item), path=item)
644+
639645
context.files[file.identifier] = file.path
640646
return file.reference
641647

@@ -646,7 +652,9 @@ def decode_data(self, item, builds, db):
646652
:param builds: The build cache.
647653
:param db: Datalayer.
648654
"""
649-
return FileItem(identifier=item.split(':')[-1], db=db)
655+
file_id = item.split(':')[-1]
656+
path = db.artifact_store.get_file(file_id)
657+
return FileItem(identifier=file_id, path=path, db=db)
650658

651659
@classmethod
652660
def hash(cls, item):

‎superduper/base/schema.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import json
44
import re
55
import typing as t
6-
from functools import cached_property
76

87
from superduper import CFG, logging
98
from superduper.base.datatype import BaseDataType
109
from superduper.base.encoding import EncodeContext
10+
from superduper.misc.importing import isreallyinstance
1111
from superduper.misc.special_dicts import dict_to_ascii_table
1212

1313
if t.TYPE_CHECKING:
@@ -44,7 +44,9 @@ def __add__(self, other: 'Schema'):
4444
@property
4545
def trivial(self):
4646
"""Determine if the schema contains only trivial fields."""
47-
return not any([isinstance(v, BaseDataType) for v in self.fields.values()])
47+
return not any(
48+
[isreallyinstance(v, BaseDataType) for v in self.fields.values()]
49+
)
4850

4951
def __repr__(self):
5052
return dict_to_ascii_table(self.fields)
@@ -138,6 +140,7 @@ def is_ref(x):
138140
):
139141
assert isinstance(value, str), msg
140142
value = json.loads(value)
143+
141144
decoded[k] = field.decode_data(value, builds=builds, db=db)
142145

143146
return decoded

‎superduper/misc/importing.py

+11
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,14 @@ def import_object(path):
2323
module = '.'.join(path.split('.')[:-1])
2424
cls = path.split('.')[-1]
2525
return getattr(importlib.import_module(module), cls)
26+
27+
28+
def isreallyinstance(this, cls):
29+
"""Check if the component is an instance of a class.
30+
31+
:param this: The component to check.
32+
:param cls: The class to check.
33+
"""
34+
# no idea why this is sometimes necessary - may be IPython autoreload related
35+
mro = [f'{o.__module__}.{o.__name__}' for o in this.__class__.__mro__]
36+
return isinstance(this, cls) or f'{cls.__module__}.{cls.__name__}' in mro

‎test/unittest/component/test_schema.py

-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ def test_schema_with_file(db, tmp_file):
9696
# loaded document contains a pointer to the file
9797
assert isinstance(r['my_file'], FileItem)
9898

99-
# however the path has not been populated
100-
assert not r['my_file'].path
101-
10299
# unpacking the document copies the file to the artifact-store
103100
rr = r.unpack()
104101

0 commit comments

Comments
 (0)
Please sign in to comment.