|
6 | 6 |
|
7 | 7 | import enum |
8 | 8 | import importlib |
| 9 | +from collections.abc import Mapping |
9 | 10 | from dataclasses import asdict, dataclass |
10 | | -from typing import Dict, Optional |
| 11 | +from typing import Any, Dict, Optional |
11 | 12 |
|
12 | 13 | from pydantic import StringConstraints |
13 | 14 | from typing_extensions import Annotated |
14 | 15 |
|
15 | | -from ..utils import OneShotCachedMap |
| 16 | +from ..utils import OneShotCachedMap, filter_known_kwargs |
16 | 17 |
|
17 | 18 |
|
18 | 19 | class StructureFamily(str, enum.Enum): |
@@ -50,30 +51,48 @@ def dict(self) -> Dict[str, Optional[str]]: |
50 | 51 |
|
51 | 52 | model_dump = dict # For easy interoperability with pydantic 2.x models |
52 | 53 |
|
| 54 | + @classmethod |
| 55 | + def from_json(cls, data: Mapping[str, Any]) -> "Spec": |
| 56 | + return cls(**filter_known_kwargs(cls, data)) |
| 57 | + |
53 | 58 |
|
54 | 59 | # TODO: make type[Structure] after #1036 |
55 | 60 | STRUCTURE_TYPES = OneShotCachedMap[StructureFamily, type]( |
56 | 61 | { |
57 | | - StructureFamily.array: lambda: importlib.import_module( |
58 | | - "...structures.array", StructureFamily.__module__ |
59 | | - ).ArrayStructure, |
60 | | - StructureFamily.awkward: lambda: importlib.import_module( |
61 | | - "...structures.awkward", StructureFamily.__module__ |
62 | | - ).AwkwardStructure, |
63 | | - StructureFamily.table: lambda: importlib.import_module( |
64 | | - "...structures.table", StructureFamily.__module__ |
65 | | - ).TableStructure, |
66 | | - StructureFamily.sparse: lambda: importlib.import_module( |
67 | | - "...structures.sparse", StructureFamily.__module__ |
68 | | - ).SparseStructure, |
69 | | - StructureFamily.ragged: lambda: importlib.import_module( |
70 | | - "...structures.ragged", StructureFamily.__module__ |
71 | | - ).RaggedStructure, |
72 | | - StructureFamily.container: lambda: importlib.import_module( |
73 | | - "...structures.container", StructureFamily.__module__ |
74 | | - ).ContainerStructure, |
75 | | - StructureFamily.bytes: lambda: importlib.import_module( |
76 | | - "...structures.bytes", StructureFamily.__module__ |
77 | | - ).BytesStructure, |
| 62 | + StructureFamily.array: lambda: ( |
| 63 | + importlib.import_module( |
| 64 | + "...structures.array", StructureFamily.__module__ |
| 65 | + ).ArrayStructure |
| 66 | + ), |
| 67 | + StructureFamily.awkward: lambda: ( |
| 68 | + importlib.import_module( |
| 69 | + "...structures.awkward", StructureFamily.__module__ |
| 70 | + ).AwkwardStructure |
| 71 | + ), |
| 72 | + StructureFamily.table: lambda: ( |
| 73 | + importlib.import_module( |
| 74 | + "...structures.table", StructureFamily.__module__ |
| 75 | + ).TableStructure |
| 76 | + ), |
| 77 | + StructureFamily.sparse: lambda: ( |
| 78 | + importlib.import_module( |
| 79 | + "...structures.sparse", StructureFamily.__module__ |
| 80 | + ).SparseStructure |
| 81 | + ), |
| 82 | + StructureFamily.ragged: lambda: ( |
| 83 | + importlib.import_module( |
| 84 | + "...structures.ragged", StructureFamily.__module__ |
| 85 | + ).RaggedStructure |
| 86 | + ), |
| 87 | + StructureFamily.container: lambda: ( |
| 88 | + importlib.import_module( |
| 89 | + "...structures.container", StructureFamily.__module__ |
| 90 | + ).ContainerStructure |
| 91 | + ), |
| 92 | + StructureFamily.bytes: lambda: ( |
| 93 | + importlib.import_module( |
| 94 | + "...structures.bytes", StructureFamily.__module__ |
| 95 | + ).BytesStructure |
| 96 | + ), |
78 | 97 | } |
79 | 98 | ) |
0 commit comments