omlish 0.0.0.dev453__py3-none-any.whl → 0.0.0.dev455__py3-none-any.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.
Files changed (56) hide show
  1. omlish/__about__.py +2 -2
  2. omlish/collections/identity.py +1 -0
  3. omlish/dataclasses/__init__.py +2 -0
  4. omlish/funcs/guard.py +27 -16
  5. omlish/lang/__init__.py +1 -0
  6. omlish/lang/functions.py +2 -2
  7. omlish/lang/iterables.py +8 -0
  8. omlish/lite/attrops.py +2 -0
  9. omlish/lite/dataclasses.py +30 -0
  10. omlish/marshal/__init__.py +16 -11
  11. omlish/marshal/base/contexts.py +33 -20
  12. omlish/marshal/base/funcs.py +8 -11
  13. omlish/marshal/base/options.py +8 -0
  14. omlish/marshal/base/types.py +38 -14
  15. omlish/marshal/composite/iterables.py +33 -20
  16. omlish/marshal/composite/literals.py +16 -18
  17. omlish/marshal/composite/mappings.py +36 -23
  18. omlish/marshal/composite/maybes.py +29 -19
  19. omlish/marshal/composite/newtypes.py +16 -16
  20. omlish/marshal/composite/optionals.py +14 -14
  21. omlish/marshal/composite/special.py +15 -15
  22. omlish/marshal/composite/unions/__init__.py +0 -0
  23. omlish/marshal/composite/unions/literals.py +93 -0
  24. omlish/marshal/composite/unions/primitives.py +103 -0
  25. omlish/marshal/factories/invalidate.py +18 -68
  26. omlish/marshal/factories/method.py +26 -0
  27. omlish/marshal/factories/moduleimport/factories.py +15 -56
  28. omlish/marshal/factories/multi.py +13 -25
  29. omlish/marshal/factories/recursive.py +42 -56
  30. omlish/marshal/factories/typecache.py +20 -77
  31. omlish/marshal/factories/typemap.py +42 -43
  32. omlish/marshal/objects/dataclasses.py +129 -106
  33. omlish/marshal/objects/marshal.py +18 -14
  34. omlish/marshal/objects/namedtuples.py +48 -42
  35. omlish/marshal/objects/unmarshal.py +19 -15
  36. omlish/marshal/polymorphism/marshal.py +9 -11
  37. omlish/marshal/polymorphism/unions.py +17 -11
  38. omlish/marshal/polymorphism/unmarshal.py +9 -10
  39. omlish/marshal/singular/enums.py +14 -18
  40. omlish/marshal/standard.py +8 -8
  41. omlish/marshal/trivial/any.py +1 -1
  42. omlish/marshal/trivial/forbidden.py +21 -26
  43. omlish/os/forkhooks.py +4 -0
  44. omlish/specs/jsonrpc/_marshal.py +33 -24
  45. omlish/specs/openapi/_marshal.py +30 -21
  46. omlish/typedvalues/marshal.py +83 -57
  47. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/METADATA +1 -1
  48. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/RECORD +52 -52
  49. omlish/funcs/match.py +0 -229
  50. omlish/marshal/composite/unions.py +0 -213
  51. omlish/marshal/factories/match.py +0 -34
  52. omlish/marshal/factories/simple.py +0 -28
  53. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/WHEEL +0 -0
  54. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/entry_points.txt +0 -0
  55. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/licenses/LICENSE +0 -0
  56. {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev455.dist-info}/top_level.txt +0 -0
@@ -1,213 +0,0 @@
1
- import typing as ta
2
-
3
- from ... import check
4
- from ... import collections as col
5
- from ... import dataclasses as dc
6
- from ... import lang
7
- from ... import reflect as rfl
8
- from ...funcs import match as mfs
9
- from ..base.contexts import MarshalContext
10
- from ..base.contexts import UnmarshalContext
11
- from ..base.types import Marshaler
12
- from ..base.types import Unmarshaler
13
- from ..base.values import Value
14
- from ..factories.simple import SimpleMarshalerFactory
15
- from ..factories.simple import SimpleUnmarshalerFactory
16
-
17
-
18
- ##
19
-
20
-
21
- class MatchUnionMarshaler(Marshaler):
22
- mmf: mfs.MultiMatchFn[[UnmarshalContext, Value], ta.Any]
23
-
24
- def marshal(self, ctx: MarshalContext, o: ta.Any) -> Value:
25
- try:
26
- m = self.mmf.match(ctx, o)
27
- except mfs.AmbiguousMatchesError:
28
- raise ValueError(o) # noqa
29
- return m.fn(ctx, o)
30
-
31
-
32
- class MatchUnionUnmarshaler(Unmarshaler):
33
- mmf: mfs.MultiMatchFn[[UnmarshalContext, Value], ta.Any]
34
-
35
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
36
- try:
37
- m = self.mmf.match(ctx, v)
38
- except mfs.AmbiguousMatchesError:
39
- raise ValueError(v) # noqa
40
- return m.fn(ctx, v)
41
-
42
-
43
- ##
44
-
45
-
46
- PRIMITIVE_UNION_TYPES: tuple[type, ...] = (
47
- float,
48
- int,
49
- str,
50
- bool,
51
- )
52
-
53
-
54
- class DestructuredPrimitiveUnionType(ta.NamedTuple):
55
- prim: ta.Sequence[type]
56
- non_prim: ta.Sequence[rfl.Type]
57
-
58
-
59
- def _destructure_primitive_union_type(
60
- rty: rfl.Type,
61
- prim_tys: ta.Container[type] = PRIMITIVE_UNION_TYPES,
62
- ) -> DestructuredPrimitiveUnionType | None:
63
- if not isinstance(rty, rfl.Union):
64
- return None
65
-
66
- pr = col.partition(rty.args, lambda a: isinstance(a, type) and a in prim_tys)
67
- if not pr.t or len(pr.f) > 1:
68
- return None
69
-
70
- return DestructuredPrimitiveUnionType(*pr) # type: ignore[arg-type]
71
-
72
-
73
- #
74
-
75
-
76
- @dc.dataclass(frozen=True)
77
- class PrimitiveUnionMarshaler(Marshaler):
78
- tys: ta.Sequence[type]
79
- x: Marshaler | None = None
80
-
81
- def marshal(self, ctx: MarshalContext, o: ta.Any) -> Value:
82
- if type(o) not in self.tys:
83
- if self.x is not None:
84
- return self.x.marshal(ctx, o)
85
- raise TypeError(o)
86
- return o
87
-
88
-
89
- @dc.dataclass(frozen=True)
90
- class PrimitiveUnionMarshalerFactory(SimpleMarshalerFactory):
91
- tys: ta.Sequence[type] = PRIMITIVE_UNION_TYPES
92
-
93
- def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
94
- return _destructure_primitive_union_type(rty, self.tys) is not None
95
-
96
- def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
97
- prim, non_prim = check.not_none(_destructure_primitive_union_type(rty, self.tys))
98
- return PrimitiveUnionMarshaler(
99
- prim,
100
- ctx.make(check.single(non_prim)) if non_prim else None,
101
- )
102
-
103
-
104
- #
105
-
106
-
107
- @dc.dataclass(frozen=True)
108
- class PrimitiveUnionUnmarshaler(Unmarshaler):
109
- tys: ta.Sequence[type]
110
- x: Unmarshaler | None = None
111
-
112
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
113
- if type(v) not in self.tys:
114
- if self.x is not None:
115
- return self.x.unmarshal(ctx, v)
116
- raise TypeError(v)
117
- return v
118
-
119
-
120
- @dc.dataclass(frozen=True)
121
- class PrimitiveUnionUnmarshalerFactory(SimpleUnmarshalerFactory):
122
- tys: ta.Sequence[type] = PRIMITIVE_UNION_TYPES
123
-
124
- def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
125
- return _destructure_primitive_union_type(rty, self.tys) is not None
126
-
127
- def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
128
- prim, non_prim = check.not_none(_destructure_primitive_union_type(rty, self.tys))
129
- return PrimitiveUnionUnmarshaler(
130
- prim,
131
- ctx.make(check.single(non_prim)) if non_prim else None,
132
- )
133
-
134
-
135
- ##
136
-
137
-
138
- LITERAL_UNION_TYPES: tuple[type, ...] = (
139
- int,
140
- str,
141
- )
142
-
143
-
144
- class DestructuredLiteralUnionType(ta.NamedTuple):
145
- v_ty: type
146
- lit: rfl.Literal
147
- non_lit: rfl.Type
148
-
149
-
150
- def _destructure_literal_union_type(rty: rfl.Type) -> DestructuredLiteralUnionType | None:
151
- if not isinstance(rty, rfl.Union):
152
- return None
153
- lits, non_lits = col.partition(rty.args, lang.isinstance_of(rfl.Literal)) # noqa
154
- if len(lits) != 1 or len(non_lits) != 1:
155
- return None
156
- lit = check.isinstance(check.single(lits), rfl.Literal)
157
- v_tys = set(map(type, lit.args))
158
- if len(v_tys) != 1:
159
- return None
160
- [v_ty] = v_tys
161
- if v_ty in rty.args or v_ty not in LITERAL_UNION_TYPES:
162
- return None
163
- return DestructuredLiteralUnionType(v_ty, lit, check.single(non_lits))
164
-
165
-
166
- #
167
-
168
-
169
- @dc.dataclass(frozen=True)
170
- class LiteralUnionMarshaler(Marshaler):
171
- v_ty: type
172
- l: Marshaler
173
- x: Marshaler
174
-
175
- def marshal(self, ctx: MarshalContext, o: ta.Any | None) -> Value:
176
- if isinstance(o, self.v_ty):
177
- return self.l.marshal(ctx, o)
178
- else:
179
- return self.x.marshal(ctx, o)
180
-
181
-
182
- class LiteralUnionMarshalerFactory(SimpleMarshalerFactory):
183
- def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
184
- return _destructure_literal_union_type(rty) is not None
185
-
186
- def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
187
- ds = check.not_none(_destructure_literal_union_type(rty))
188
- return LiteralUnionMarshaler(ds.v_ty, ctx.make(ds.lit), ctx.make(ds.non_lit))
189
-
190
-
191
- #
192
-
193
-
194
- @dc.dataclass(frozen=True)
195
- class LiteralUnionUnmarshaler(Unmarshaler):
196
- v_ty: type
197
- l: Unmarshaler
198
- x: Unmarshaler
199
-
200
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any | None:
201
- if isinstance(v, self.v_ty):
202
- return self.l.unmarshal(ctx, v) # type: ignore[arg-type]
203
- else:
204
- return self.x.unmarshal(ctx, v)
205
-
206
-
207
- class LiteralUnionUnmarshalerFactory(SimpleUnmarshalerFactory):
208
- def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
209
- return _destructure_literal_union_type(rty) is not None
210
-
211
- def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
212
- ds = check.not_none(_destructure_literal_union_type(rty))
213
- return LiteralUnionUnmarshaler(ds.v_ty, ctx.make(ds.lit), ctx.make(ds.non_lit))
@@ -1,34 +0,0 @@
1
- from ... import lang
2
- from ... import reflect as rfl
3
- from ...funcs import match as mfs
4
- from ..base.contexts import MarshalContext
5
- from ..base.contexts import UnmarshalContext
6
- from ..base.types import Marshaler
7
- from ..base.types import MarshalerFactory
8
- from ..base.types import MarshalerMaker
9
- from ..base.types import Unmarshaler
10
- from ..base.types import UnmarshalerFactory
11
- from ..base.types import UnmarshalerMaker
12
-
13
-
14
- ##
15
-
16
-
17
- class MarshalerFactoryMatchClass(
18
- MarshalerFactory,
19
- mfs.MatchFnClass[[MarshalContext, rfl.Type], Marshaler],
20
- lang.Abstract,
21
- ):
22
- @property
23
- def make_marshaler(self) -> MarshalerMaker:
24
- return self # noqa
25
-
26
-
27
- class UnmarshalerFactoryMatchClass(
28
- UnmarshalerFactory,
29
- mfs.MatchFnClass[[UnmarshalContext, rfl.Type], Unmarshaler],
30
- lang.Abstract,
31
- ):
32
- @property
33
- def make_unmarshaler(self) -> UnmarshalerMaker:
34
- return self # noqa
@@ -1,28 +0,0 @@
1
- from ... import lang
2
- from ..base.types import MarshalerFactory
3
- from ..base.types import MarshalerMaker
4
- from ..base.types import UnmarshalerFactory
5
- from ..base.types import UnmarshalerMaker
6
-
7
-
8
- ##
9
-
10
-
11
- class SimpleMarshalerFactory(
12
- MarshalerFactory,
13
- MarshalerMaker,
14
- lang.Abstract,
15
- ):
16
- @property
17
- def make_marshaler(self) -> MarshalerMaker:
18
- return self # noqa
19
-
20
-
21
- class SimpleUnmarshalerFactory(
22
- UnmarshalerFactory,
23
- UnmarshalerMaker,
24
- lang.Abstract,
25
- ):
26
- @property
27
- def make_unmarshaler(self) -> UnmarshalerMaker:
28
- return self # noqa