structtype 0.1.0__cp314-cp314-pyemscripten_2026_0_wasm32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
structtype/__init__.py ADDED
@@ -0,0 +1,17 @@
1
+ from ._core import (
2
+ NODEFAULT,
3
+ UNSET,
4
+ DecodeError,
5
+ EncodeError,
6
+ Field,
7
+ Raw,
8
+ Struct,
9
+ StructConfig,
10
+ StructMeta,
11
+ UnsetType,
12
+ ValidationError,
13
+ )
14
+ from ._adapter import StructAdapter
15
+ from ._inspect import FieldInfo, fields
16
+ from ._json_schema import json_schema, json_schema_dump, json_schema_components
17
+ from ._version import __version__
@@ -0,0 +1,361 @@
1
+ import enum
2
+ from collections.abc import Callable, Iterable, Mapping
3
+ from inspect import Signature
4
+ from typing import (
5
+ Any,
6
+ ClassVar,
7
+ Final,
8
+ Literal,
9
+ Optional,
10
+ Tuple,
11
+ Type,
12
+ TypeVar,
13
+ Union,
14
+ final,
15
+ overload,
16
+ )
17
+
18
+ from typing_extensions import Buffer, Self, TypeAlias, dataclass_transform
19
+
20
+ from . import _inspect
21
+
22
+ # PEP 673 explicitly rejects using Self in metaclass definitions:
23
+ # https://peps.python.org/pep-0673/#valid-locations-for-self
24
+ #
25
+ # Typeshed works around this by using a type variable as well:
26
+ # https://github.com/python/typeshed/blob/17bde1bd5e556de001adde3c2f340ba1c3581bd2/stdlib/abc.pyi#L14-L19
27
+ _SM = TypeVar("_SM", bound="StructMeta")
28
+
29
+ class StructMeta(type):
30
+ __struct_fields__: ClassVar[tuple[str, ...]]
31
+ __struct_defaults__: ClassVar[tuple[Any, ...]]
32
+ __struct_encode_fields__: ClassVar[tuple[str, ...]]
33
+ __match_args__: ClassVar[tuple[str, ...]]
34
+ @property
35
+ def __signature__(self) -> Signature: ...
36
+ @property
37
+ def __struct_config__(self) -> structs.StructConfig: ...
38
+ def __new__(
39
+ mcls: type[_SM],
40
+ name: str,
41
+ bases: tuple[type, ...],
42
+ namespace: dict[str, Any],
43
+ /,
44
+ *,
45
+ tag: bool | str | int | Callable[[str], str | int] | None = None,
46
+ tag_field: str | None = None,
47
+ rename: (
48
+ None
49
+ | Literal["lower", "upper", "camel", "pascal", "kebab"]
50
+ | Callable[[str], str | None]
51
+ | Mapping[str, str]
52
+ ) = None,
53
+ omit_defaults: bool = False,
54
+ forbid_unknown_fields: bool = False,
55
+ frozen: bool = False,
56
+ eq: bool = True,
57
+ order: bool = False,
58
+ kw_only: bool = False,
59
+ repr_omit_defaults: bool = False,
60
+ array_like: bool = False,
61
+ gc: bool = True,
62
+ weakref: bool = False,
63
+ dict: bool = False,
64
+ cache_hash: bool = False,
65
+ validate_on_init: bool = False,
66
+ ) -> _SM: ...
67
+
68
+ _T = TypeVar("_T")
69
+
70
+ @final
71
+ class UnsetType(enum.Enum):
72
+ UNSET = "UNSET"
73
+ def __bool__(self) -> Literal[False]: ...
74
+
75
+ UNSET: Final = UnsetType.UNSET
76
+
77
+ @final
78
+ class _NoDefault(enum.Enum):
79
+ NODEFAULT = "NODEFAULT"
80
+
81
+ NODEFAULT: Final = _NoDefault.NODEFAULT
82
+
83
+ @dataclass_transform(field_specifiers=(Field,))
84
+ class Struct(metaclass=StructMeta):
85
+ __struct_fields__: ClassVar[tuple[str, ...]]
86
+ __struct_config__: ClassVar[StructConfig]
87
+ __struct_encode_fields__: ClassVar[tuple[str, ...]]
88
+ __match_args__: ClassVar[tuple[str, ...]]
89
+ # A default __init__ so that Structs with unknown field types
90
+ # won't error on every call to `__init__`
91
+ def __init__(self, *args: Any, **kwargs: Any) -> None: ...
92
+ def __init_subclass__(
93
+ cls,
94
+ tag: bool | str | int | Callable[[str], str | int] | None = None,
95
+ tag_field: str | None = None,
96
+ rename: (
97
+ None
98
+ | Literal["lower", "upper", "camel", "pascal", "kebab"]
99
+ | Callable[[str], str | None]
100
+ | Mapping[str, str]
101
+ ) = None,
102
+ omit_defaults: bool = False,
103
+ forbid_unknown_fields: bool = False,
104
+ frozen: bool = False,
105
+ eq: bool = True,
106
+ order: bool = False,
107
+ kw_only: bool = False,
108
+ repr_omit_defaults: bool = False,
109
+ array_like: bool = False,
110
+ gc: bool = True,
111
+ weakref: bool = False,
112
+ dict: bool = False,
113
+ cache_hash: bool = False,
114
+ validate_on_init: bool = False,
115
+ ) -> None: ...
116
+ def __rich_repr__(self) -> list[tuple[str, Any]]: ...
117
+ def __replace__(self, **changes: Any) -> Self: ...
118
+ def struct_dump_json(
119
+ self,
120
+ *,
121
+ enc_hook: Callable[[Any], Any] | None = None,
122
+ decimal_format: Literal["string", "number"] | Callable[[Any], Any] | None = None,
123
+ uuid_format: Literal["canonical", "hex"] | None = None,
124
+ order: Literal["deterministic", "sorted"] | None = None,
125
+ ) -> bytes: ...
126
+ def struct_dump(self) -> dict[str, Any]: ...
127
+ def struct_to_dict(self) -> dict[str, Any]: ...
128
+ def struct_to_tuple(self) -> tuple[Any, ...]: ...
129
+ def struct_force_setattr(self, name: str, value: Any) -> None: ...
130
+ def struct_check(
131
+ self,
132
+ *,
133
+ strict: bool = True,
134
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
135
+ ) -> None: ...
136
+ @classmethod
137
+ def struct_validate_json(
138
+ cls: type[_T],
139
+ buf: str | Buffer,
140
+ *,
141
+ strict: bool = True,
142
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
143
+ ) -> _T: ...
144
+ @classmethod
145
+ def struct_validate(
146
+ cls: type[_T],
147
+ obj: Any,
148
+ *,
149
+ strict: bool = True,
150
+ from_attributes: bool = False,
151
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
152
+ ) -> _T: ...
153
+ @classmethod
154
+ def struct_validate_jsonln(
155
+ cls: type[_T],
156
+ buf: str | Buffer,
157
+ *,
158
+ strict: bool = True,
159
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
160
+ ) -> list[_T]: ...
161
+ @classmethod
162
+ def struct_dump_jsonln(
163
+ cls,
164
+ items: Iterable[Any],
165
+ *,
166
+ enc_hook: Callable[[Any], Any] | None = None,
167
+ decimal_format: Literal["string", "number"] | Callable[[Any], Any] | None = None,
168
+ uuid_format: Literal["canonical", "hex"] | None = None,
169
+ order: Literal["deterministic", "sorted"] | None = None,
170
+ ) -> bytes: ...
171
+
172
+ # Lie and say `Raw` is a subclass of `bytes`, so mypy will accept it in most
173
+ # places where an object that implements the buffer protocol is valid
174
+ @final
175
+ class Raw(bytes):
176
+ @overload
177
+ def __new__(cls) -> "Raw": ...
178
+ @overload
179
+ def __new__(cls, msg: Buffer | str) -> "Raw": ...
180
+ def copy(self) -> "Raw": ...
181
+
182
+ #: We can't represent this in types, only via a name:
183
+ _NonNegativeInt: TypeAlias = int
184
+
185
+ @final
186
+ class Field:
187
+ # Numeric:
188
+ @overload
189
+ def __init__(
190
+ self,
191
+ *,
192
+ gt: int | float | None = None,
193
+ lt: int | float | None = None,
194
+ multiple_of: int | float | None = None,
195
+ default: Any = NODEFAULT,
196
+ default_factory: Callable[[], Any] | None = None,
197
+ alias: str | None = None,
198
+ title: str | None = None,
199
+ description: str | None = None,
200
+ json_schema_extra: dict[str, Any] | None = None,
201
+ examples: list[Any] | None = None,
202
+ ) -> None: ...
203
+ @overload
204
+ def __init__(
205
+ self,
206
+ *,
207
+ gt: int | float | None = None,
208
+ le: int | float | None = None,
209
+ multiple_of: int | float | None = None,
210
+ default: Any = NODEFAULT,
211
+ default_factory: Callable[[], Any] | None = None,
212
+ alias: str | None = None,
213
+ title: str | None = None,
214
+ description: str | None = None,
215
+ json_schema_extra: dict[str, Any] | None = None,
216
+ examples: list[Any] | None = None,
217
+ ) -> None: ...
218
+ @overload
219
+ def __init__(
220
+ self,
221
+ *,
222
+ ge: int | float | None = None,
223
+ lt: int | float | None = None,
224
+ multiple_of: int | float | None = None,
225
+ default: Any = NODEFAULT,
226
+ default_factory: Callable[[], Any] | None = None,
227
+ alias: str | None = None,
228
+ title: str | None = None,
229
+ description: str | None = None,
230
+ json_schema_extra: dict[str, Any] | None = None,
231
+ examples: list[Any] | None = None,
232
+ ) -> None: ...
233
+ @overload
234
+ def __init__(
235
+ self,
236
+ *,
237
+ ge: int | float | None = None,
238
+ le: int | float | None = None,
239
+ multiple_of: int | float | None = None,
240
+ default: Any = NODEFAULT,
241
+ default_factory: Callable[[], Any] | None = None,
242
+ alias: str | None = None,
243
+ title: str | None = None,
244
+ description: str | None = None,
245
+ json_schema_extra: dict[str, Any] | None = None,
246
+ examples: list[Any] | None = None,
247
+ ) -> None: ...
248
+ # Other (string/datetime):
249
+ @overload
250
+ def __init__(
251
+ self,
252
+ *,
253
+ pattern: str | None = None,
254
+ min_length: _NonNegativeInt | None = None,
255
+ max_length: _NonNegativeInt | None = None,
256
+ tz: bool | None = None,
257
+ default: Any = NODEFAULT,
258
+ default_factory: Callable[[], Any] | None = None,
259
+ alias: str | None = None,
260
+ title: str | None = None,
261
+ description: str | None = None,
262
+ json_schema_extra: dict[str, Any] | None = None,
263
+ examples: list[Any] | None = None,
264
+ ) -> None: ...
265
+ default: Final[Any]
266
+ default_factory: Final[Callable[[], Any] | None]
267
+ alias: Final[str | None]
268
+ gt: Final[int | float | None]
269
+ ge: Final[int | float | None]
270
+ lt: Final[int | float | None]
271
+ le: Final[int | float | None]
272
+ multiple_of: Final[int | float | None]
273
+ pattern: Final[str | None]
274
+ min_length: Final[int | None]
275
+ max_length: Final[int | None]
276
+ tz: Final[int | None]
277
+ title: Final[str | None]
278
+ description: Final[str | None]
279
+ examples: Final[list[Any] | None]
280
+ json_schema_extra: Final[dict[str, Any] | None]
281
+ def __rich_repr__(self) -> list[tuple[str, Any]]: ...
282
+
283
+ class StructConfig:
284
+ frozen: bool
285
+ eq: bool
286
+ order: bool
287
+ array_like: bool
288
+ gc: bool
289
+ repr_omit_defaults: bool
290
+ omit_defaults: bool
291
+ forbid_unknown_fields: bool
292
+ validate_on_init: bool
293
+ weakref: bool
294
+ dict: bool
295
+ cache_hash: bool
296
+ tag: str | int | None
297
+ tag_field: str | None
298
+
299
+ class FieldInfo(Struct):
300
+ name: str
301
+ encode_name: str
302
+ type: Any
303
+ default: Any = NODEFAULT
304
+ default_factory: Any = NODEFAULT
305
+
306
+ @property
307
+ def required(self) -> bool: ...
308
+
309
+ def fields(type_or_instance: Struct | type[Struct]) -> tuple[FieldInfo, ...]: ...
310
+ def json_schema(
311
+ type: Any,
312
+ *,
313
+ schema_hook: Callable[[type[Any]], dict[str, Any]] | None = None,
314
+ ref_template: str = "#/$defs/{name}",
315
+ ) -> dict[str, Any]: ...
316
+ def json_schema_dump(
317
+ type: Any,
318
+ *,
319
+ schema_hook: Callable[[type[Any]], dict[str, Any]] | None = None,
320
+ ref_template: str = "#/$defs/{name}",
321
+ ) -> bytes: ...
322
+ def json_schema_components(
323
+ types: Iterable[Any],
324
+ *,
325
+ schema_hook: Callable[[type[Any]], dict[str, Any]] | None = None,
326
+ ref_template: str = "#/$defs/{name}",
327
+ ) -> tuple[tuple[dict[str, Any], ...], dict[str, Any]]: ...
328
+
329
+ class StructAdapter:
330
+ def __init__(self, type: Any): ...
331
+ def struct_validate_json(
332
+ self,
333
+ buf: str | Buffer,
334
+ *,
335
+ strict: bool = True,
336
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
337
+ ) -> Any: ...
338
+ def struct_dump_json(
339
+ self,
340
+ obj: Any,
341
+ *,
342
+ enc_hook: Callable[[Any], Any] | None = None,
343
+ decimal_format: Literal["string", "number"] | Callable[[Any], Any] | None = None,
344
+ uuid_format: Literal["canonical", "hex"] | None = None,
345
+ order: Literal["deterministic", "sorted"] | None = None,
346
+ ) -> bytes: ...
347
+ def struct_validate(
348
+ self,
349
+ obj: Any,
350
+ *,
351
+ strict: bool = True,
352
+ dec_hook: Callable[[type[Any], Any], Any] | None = None,
353
+ from_attributes: bool = False,
354
+ ) -> Any: ...
355
+ def struct_dump(self, obj: Any) -> Any: ...
356
+
357
+ class EncodeError(ValueError): ...
358
+ class DecodeError(ValueError): ...
359
+ class ValidationError(ValueError): ...
360
+
361
+ __version__: str
structtype/_adapter.py ADDED
@@ -0,0 +1,34 @@
1
+ from typing import Any
2
+
3
+ from ._core import _convert, json_decode, json_encode
4
+
5
+
6
+ class StructAdapter:
7
+ __slots__ = ("_type",)
8
+
9
+ def __init__(self, type: Any):
10
+ self._type = type
11
+
12
+ def struct_validate_json(self, buf, *, strict=True, dec_hook=None):
13
+ return json_decode(buf, type=self._type, strict=strict, dec_hook=dec_hook)
14
+
15
+ def struct_dump_json(self, obj, *, enc_hook=None, decimal_format=None, uuid_format=None, order=None):
16
+ return json_encode(obj, enc_hook=enc_hook, decimal_format=decimal_format, uuid_format=uuid_format, order=order)
17
+
18
+ def struct_validate(
19
+ self, obj, *, strict=True, dec_hook=None, from_attributes=False
20
+ ):
21
+ return _convert(
22
+ obj,
23
+ self._type,
24
+ strict=strict,
25
+ dec_hook=dec_hook,
26
+ from_attributes=from_attributes,
27
+ )
28
+
29
+ def struct_dump(self, obj):
30
+ if hasattr(obj, "struct_dump"):
31
+ return obj.struct_dump()
32
+ if hasattr(obj, "model_dump"):
33
+ return obj.model_dump()
34
+ return obj