Commit b6f6cf4
committed
Unified artifact serializer registration via setup_imports()
Replace the two-path registration scheme (ARTIFACT_SERIALIZERS_DESC +
register_serializer_for_type) with a single declarative mechanism.
Extensions ship one serializer file per serializer; heavy imports
live in a setup_imports() classmethod whose ModuleNotFoundError
parks the serializer on an import hook and retries once the awaited
module appears in sys.modules.
Author-facing API
-----------------
class TorchSerializer(ArtifactSerializer):
TYPE = "torch"
PRIORITY = 50
@classmethod
def setup_imports(cls, context=None):
cls.lazy_import("torch")
cls.lazy_import("pyarrow", alias="pa")
@classmethod
def can_serialize(cls, obj):
return isinstance(obj, cls.torch.Tensor)
...
# mfextinit_myext.py
ARTIFACT_SERIALIZERS_DESC = [
("torch", "my_ext.serializers.torch_serializer.TorchSerializer"),
]
Mechanism
---------
- SerializerStore.bootstrap() walks every extension's
ARTIFACT_SERIALIZERS_DESC directly (the serializer category owns its
own lifecycle rather than flowing through resolve_plugins), applies
ENABLED_ARTIFACT_SERIALIZER +/- toggles, and drives each entry
through a state machine:
known → importing → class_loaded → importing_deps → active
with terminal states `broken` / `disabled` and the retry state
`pending_on_imports`.
- lazy_import(module_path, alias=None) imports the module, stashes it
on cls, returns it. Rejects reserved names (TYPE, PRIORITY, dispatch
methods, leading underscore) and double-assignment within one
setup_imports() call.
- A sys.meta_path interceptor watches the pending modules; when any
imports, SerializerStore._on_module_imported re-runs the lifecycle
for every parked entry waiting on that module. Loop guard: the same
ImportError.name raising twice → broken.
- Dispatch reads _active_serializers only. Exceptions raised by
can_serialize / can_deserialize on a currently-active serializer are
caught, counted in the record's dispatch_error_count, and the
serializer is skipped for that artifact only — preserving the
PickleSerializer fallback.
- On PRIORITY ties, last-registered wins; a lexicographic tiebreak on
class_path provides cross-environment reproducibility.
Diagnostic API
--------------
metaflow.datastore.artifacts.list_serializer_status() returns a list
of per-entry dicts with name, class_path, state, awaiting_modules,
last_error, priority, type, import_trigger, and dispatch_error_count.
Primary use case: answering "why isn't my custom serializer active?"
without reading source.
Removed
-------
- register_serializer_for_type() (public API)
- SerializerConfig, register_serializer_config, iter_registered_configs,
load_serializer_class, plus the backing declarative-config plumbing
inside lazy_registry.py — nothing in the unified path uses them.
- ARTIFACT_SERIALIZERS = resolve_plugins("artifact_serializer") in
metaflow/plugins/__init__.py (no readers in the codebase).
- "artifact_serializer" from _plugin_categories in
metaflow/extension_support/plugins.py.
Testing
-------
- 102 unit tests across test_artifact_serializer, test_pickle_serializer,
test_serializer_integration, test_serializer_lifecycle, and
test_serializer_public_api. Coverage: state-machine transitions,
retry and loop guard, disabled toggle, dispatch exception handling,
lazy_import reserved-name + collision guards, setup_imports signature
compatibility (both def setup_imports(cls) and
def setup_imports(cls, context=None) are supported), subclass
inheritance, and public API surface assertions.
- SerializerStore._reset_for_tests() clears registry + interceptor state
and walks MRO to delattr lazy-imported attributes for test isolation.1 parent 3cdb08c commit b6f6cf4
13 files changed
Lines changed: 1910 additions & 489 deletions
File tree
- metaflow
- datastore
- artifacts
- extension_support
- plugins
- test/unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 8 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
20 | 22 | | |
21 | 23 | | |
22 | 24 | | |
| |||
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | 30 | | |
107 | 31 | | |
108 | 32 | | |
| |||
131 | 55 | | |
132 | 56 | | |
133 | 57 | | |
134 | | - | |
135 | | - | |
136 | | - | |
| 58 | + | |
| 59 | + | |
137 | 60 | | |
138 | 61 | | |
139 | 62 | | |
140 | | - | |
141 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
142 | 69 | | |
143 | 70 | | |
144 | | - | |
145 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
146 | 75 | | |
147 | 76 | | |
148 | | - | |
| 77 | + | |
149 | 78 | | |
150 | 79 | | |
151 | 80 | | |
| |||
167 | 96 | | |
168 | 97 | | |
169 | 98 | | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
174 | 109 | | |
175 | 110 | | |
176 | 111 | | |
| |||
182 | 117 | | |
183 | 118 | | |
184 | 119 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | 120 | | |
210 | 121 | | |
211 | | - | |
212 | | - | |
213 | | - | |
| 122 | + | |
214 | 123 | | |
215 | 124 | | |
216 | 125 | | |
0 commit comments