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
@@ -2,13 +2,13 @@ import abc
2
2
  import dataclasses as dc
3
3
  import typing as ta
4
4
 
5
- from ... import check
6
5
  from ... import lang
7
6
  from ... import reflect as rfl
8
7
  from ..base.contexts import MarshalContext
8
+ from ..base.contexts import MarshalFactoryContext
9
9
  from ..base.types import Marshaler
10
+ from ..base.types import MarshalerFactory
10
11
  from ..base.values import Value
11
- from ..factories.simple import SimpleMarshalerFactory
12
12
  from .metadata import FieldTypeTagging
13
13
  from .metadata import Impls
14
14
  from .metadata import Polymorphism
@@ -53,10 +53,10 @@ class FieldPolymorphismMarshaler(PolymorphismMarshaler):
53
53
  def make_polymorphism_marshaler(
54
54
  impls: Impls,
55
55
  tt: TypeTagging,
56
- ctx: MarshalContext,
56
+ ctx: MarshalFactoryContext,
57
57
  ) -> Marshaler:
58
58
  m = {
59
- i.ty: (i.tag, ctx.make(i.ty))
59
+ i.ty: (i.tag, ctx.make_marshaler(i.ty))
60
60
  for i in impls
61
61
  }
62
62
  if isinstance(tt, WrapperTypeTagging):
@@ -68,13 +68,11 @@ def make_polymorphism_marshaler(
68
68
 
69
69
 
70
70
  @dc.dataclass(frozen=True)
71
- class PolymorphismMarshalerFactory(SimpleMarshalerFactory):
71
+ class PolymorphismMarshalerFactory(MarshalerFactory):
72
72
  p: Polymorphism
73
73
  tt: TypeTagging = WrapperTypeTagging()
74
74
 
75
- def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
76
- return rty is self.p.ty
77
-
78
- def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
79
- check.is_(rty, self.p.ty)
80
- return make_polymorphism_marshaler(self.p.impls, self.tt, ctx)
75
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
76
+ if rty is not self.p.ty:
77
+ return None
78
+ return lambda: make_polymorphism_marshaler(self.p.impls, self.tt, ctx)
@@ -1,14 +1,16 @@
1
+ import typing as ta
2
+
1
3
  from ... import cached
2
4
  from ... import check
3
5
  from ... import dataclasses as dc
4
6
  from ... import lang
5
7
  from ... import reflect as rfl
6
- from ..base.contexts import MarshalContext
7
- from ..base.contexts import UnmarshalContext
8
+ from ..base.contexts import MarshalFactoryContext
9
+ from ..base.contexts import UnmarshalFactoryContext
8
10
  from ..base.types import Marshaler
11
+ from ..base.types import MarshalerFactory
9
12
  from ..base.types import Unmarshaler
10
- from ..factories.simple import SimpleMarshalerFactory
11
- from ..factories.simple import SimpleUnmarshalerFactory
13
+ from ..base.types import UnmarshalerFactory
12
14
  from .marshal import make_polymorphism_marshaler
13
15
  from .metadata import Impls
14
16
  from .metadata import TypeTagging
@@ -30,7 +32,7 @@ class _BasePolymorphismUnionFactory(lang.Abstract):
30
32
  def rtys(self) -> frozenset[rfl.Type]:
31
33
  return frozenset(i.ty for i in self.impls)
32
34
 
33
- def guard(self, ctx: MarshalContext | UnmarshalContext, rty: rfl.Type) -> bool:
35
+ def _guard(self, rty: rfl.Type) -> bool:
34
36
  if not isinstance(rty, rfl.Union):
35
37
  return False
36
38
  if self.allow_partial:
@@ -44,12 +46,16 @@ class _BasePolymorphismUnionFactory(lang.Abstract):
44
46
 
45
47
 
46
48
  @dc.dataclass(frozen=True)
47
- class PolymorphismUnionMarshalerFactory(_BasePolymorphismUnionFactory, SimpleMarshalerFactory):
48
- def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
49
- return make_polymorphism_marshaler(self.get_impls(rty), self.tt, ctx)
49
+ class PolymorphismUnionMarshalerFactory(_BasePolymorphismUnionFactory, MarshalerFactory):
50
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
51
+ if not self._guard(rty):
52
+ return None
53
+ return lambda: make_polymorphism_marshaler(self.get_impls(rty), self.tt, ctx)
50
54
 
51
55
 
52
56
  @dc.dataclass(frozen=True)
53
- class PolymorphismUnionUnmarshalerFactory(_BasePolymorphismUnionFactory, SimpleUnmarshalerFactory):
54
- def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
55
- return make_polymorphism_unmarshaler(self.get_impls(rty), self.tt, ctx)
57
+ class PolymorphismUnionUnmarshalerFactory(_BasePolymorphismUnionFactory, UnmarshalerFactory):
58
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
59
+ if not self._guard(rty):
60
+ return None
61
+ return lambda: make_polymorphism_unmarshaler(self.get_impls(rty), self.tt, ctx)
@@ -7,9 +7,10 @@ from ... import check
7
7
  from ... import lang
8
8
  from ... import reflect as rfl
9
9
  from ..base.contexts import UnmarshalContext
10
+ from ..base.contexts import UnmarshalFactoryContext
10
11
  from ..base.types import Unmarshaler
12
+ from ..base.types import UnmarshalerFactory
11
13
  from ..base.values import Value
12
- from ..factories.simple import SimpleUnmarshalerFactory
13
14
  from .metadata import FieldTypeTagging
14
15
  from .metadata import Impls
15
16
  from .metadata import Polymorphism
@@ -58,12 +59,12 @@ class FieldPolymorphismUnmarshaler(PolymorphismUnmarshaler):
58
59
  def make_polymorphism_unmarshaler(
59
60
  impls: Impls,
60
61
  tt: TypeTagging,
61
- ctx: UnmarshalContext,
62
+ ctx: UnmarshalFactoryContext,
62
63
  ) -> Unmarshaler:
63
64
  m = {
64
65
  t: u
65
66
  for i in impls
66
- for u in [ctx.make(i.ty)]
67
+ for u in [ctx.make_unmarshaler(i.ty)]
67
68
  for t in [i.tag, *i.alts]
68
69
  }
69
70
  if isinstance(tt, WrapperTypeTagging):
@@ -75,13 +76,11 @@ def make_polymorphism_unmarshaler(
75
76
 
76
77
 
77
78
  @dc.dataclass(frozen=True)
78
- class PolymorphismUnmarshalerFactory(SimpleUnmarshalerFactory):
79
+ class PolymorphismUnmarshalerFactory(UnmarshalerFactory):
79
80
  p: Polymorphism
80
81
  tt: TypeTagging = WrapperTypeTagging()
81
82
 
82
- def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
83
- return rty is self.p.ty
84
-
85
- def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
86
- check.is_(rty, self.p.ty)
87
- return make_polymorphism_unmarshaler(self.p.impls, self.tt, ctx)
83
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
84
+ if rty is not self.p.ty:
85
+ return None
86
+ return lambda: make_polymorphism_unmarshaler(self.p.impls, self.tt, ctx)
@@ -5,12 +5,14 @@ import typing as ta
5
5
  from ... import check
6
6
  from ... import reflect as rfl
7
7
  from ..base.contexts import MarshalContext
8
+ from ..base.contexts import MarshalFactoryContext
8
9
  from ..base.contexts import UnmarshalContext
10
+ from ..base.contexts import UnmarshalFactoryContext
9
11
  from ..base.types import Marshaler
12
+ from ..base.types import MarshalerFactory
10
13
  from ..base.types import Unmarshaler
14
+ from ..base.types import UnmarshalerFactory
11
15
  from ..base.values import Value
12
- from ..factories.simple import SimpleMarshalerFactory
13
- from ..factories.simple import SimpleUnmarshalerFactory
14
16
 
15
17
 
16
18
  ##
@@ -24,14 +26,11 @@ class EnumMarshaler(Marshaler):
24
26
  return o.name
25
27
 
26
28
 
27
- class EnumMarshalerFactory(SimpleMarshalerFactory):
28
- def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
29
- return isinstance(rty, type) and issubclass(rty, enum.Enum)
30
-
31
- def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
32
- ty = check.isinstance(rty, type)
33
- check.state(issubclass(ty, enum.Enum))
34
- return EnumMarshaler(ty) # noqa
29
+ class EnumMarshalerFactory(MarshalerFactory):
30
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
31
+ if not (isinstance(rty, type) and issubclass(rty, enum.Enum)):
32
+ return None
33
+ return lambda: EnumMarshaler(rty) # noqa
35
34
 
36
35
 
37
36
  @dc.dataclass(frozen=True)
@@ -42,11 +41,8 @@ class EnumUnmarshaler(Unmarshaler):
42
41
  return self.ty[check.isinstance(v, str)]
43
42
 
44
43
 
45
- class EnumUnmarshalerFactory(SimpleUnmarshalerFactory):
46
- def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
47
- return isinstance(rty, type) and issubclass(rty, enum.Enum)
48
-
49
- def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
50
- ty = check.isinstance(rty, type)
51
- check.state(issubclass(ty, enum.Enum))
52
- return EnumUnmarshaler(ty)
44
+ class EnumUnmarshalerFactory(UnmarshalerFactory):
45
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
46
+ if not (isinstance(rty, type) and issubclass(rty, enum.Enum)):
47
+ return None
48
+ return lambda: EnumUnmarshaler(rty) # noqa
@@ -22,10 +22,10 @@ from .composite.optionals import OptionalMarshalerFactory
22
22
  from .composite.optionals import OptionalUnmarshalerFactory
23
23
  from .composite.special import SequenceNotStrMarshalerFactory
24
24
  from .composite.special import SequenceNotStrUnmarshalerFactory
25
- from .composite.unions import LiteralUnionMarshalerFactory
26
- from .composite.unions import LiteralUnionUnmarshalerFactory
27
- from .composite.unions import PrimitiveUnionMarshalerFactory
28
- from .composite.unions import PrimitiveUnionUnmarshalerFactory
25
+ from .composite.unions.literals import LiteralUnionMarshalerFactory
26
+ from .composite.unions.literals import LiteralUnionUnmarshalerFactory
27
+ from .composite.unions.primitives import PrimitiveUnionMarshalerFactory
28
+ from .composite.unions.primitives import PrimitiveUnionUnmarshalerFactory
29
29
  from .factories.invalidate import InvalidatableMarshalerFactory
30
30
  from .factories.invalidate import InvalidatableUnmarshalerFactory
31
31
  from .factories.moduleimport.factories import ModuleImportingMarshalerFactory
@@ -119,11 +119,11 @@ def new_standard_marshaler_factory(
119
119
  nonlocal gl
120
120
  gl = DEFAULT_STANDARD_FACTORIES
121
121
 
122
- fi: MarshalerFactory = MultiMarshalerFactory([
122
+ fi: MarshalerFactory = MultiMarshalerFactory(
123
123
  *(first if first is not None else []),
124
124
  *gl.marshaler_factories,
125
125
  *(last if last is not None else []),
126
- ])
126
+ )
127
127
 
128
128
  fi = RecursiveMarshalerFactory(fi)
129
129
 
@@ -152,11 +152,11 @@ def new_standard_unmarshaler_factory(
152
152
  nonlocal gl
153
153
  gl = DEFAULT_STANDARD_FACTORIES
154
154
 
155
- fi: UnmarshalerFactory = MultiUnmarshalerFactory([
155
+ fi: UnmarshalerFactory = MultiUnmarshalerFactory(
156
156
  *(first if first is not None else []),
157
157
  *gl.unmarshaler_factories,
158
158
  *(last if last is not None else []),
159
- ])
159
+ )
160
160
 
161
161
  fi = RecursiveUnmarshalerFactory(fi)
162
162
 
@@ -15,7 +15,7 @@ from ..factories.typemap import TypeMapUnmarshalerFactory
15
15
 
16
16
  class AnyMarshalerUnmarshaler(Marshaler, Unmarshaler):
17
17
  def marshal(self, ctx: MarshalContext, o: ta.Any) -> Value:
18
- return ctx.make(type(o)).marshal(ctx, o)
18
+ return ctx.marshal_factory_context.make_marshaler(type(o)).marshal(ctx, o)
19
19
 
20
20
  def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
21
21
  return v
@@ -2,45 +2,40 @@ import dataclasses as dc
2
2
  import typing as ta
3
3
 
4
4
  from ... import reflect as rfl
5
- from ...funcs import match as mfs
6
- from ..base.contexts import MarshalContext
7
- from ..base.contexts import UnmarshalContext
5
+ from ..base.contexts import MarshalFactoryContext
6
+ from ..base.contexts import UnmarshalFactoryContext
8
7
  from ..base.errors import ForbiddenTypeError
9
8
  from ..base.types import Marshaler
9
+ from ..base.types import MarshalerFactory
10
10
  from ..base.types import Unmarshaler
11
- from ..factories.simple import SimpleMarshalerFactory
12
- from ..factories.simple import SimpleUnmarshalerFactory
13
-
14
-
15
- C = ta.TypeVar('C')
16
- R = ta.TypeVar('R')
11
+ from ..base.types import UnmarshalerFactory
17
12
 
18
13
 
19
14
  ##
20
15
 
21
16
 
22
17
  @dc.dataclass(frozen=True)
23
- class ForbiddenTypeFactory(mfs.MatchFn[[C, rfl.Type], R]):
18
+ class ForbiddenTypeMarshalerFactoryUnmarshalerFactory(MarshalerFactory, UnmarshalerFactory):
24
19
  rtys: ta.AbstractSet[rfl.Type]
25
20
 
26
- def guard(self, ctx: C, rty: rfl.Type) -> bool:
27
- return rty in self.rtys
21
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
22
+ if rty not in self.rtys:
23
+ return None
28
24
 
29
- def fn(self, ctx: C, rty: rfl.Type) -> R:
30
- raise ForbiddenTypeError(rty)
25
+ def inner():
26
+ raise ForbiddenTypeError(rty)
31
27
 
28
+ return inner
32
29
 
33
- @dc.dataclass(frozen=True)
34
- class ForbiddenTypeMarshalerFactory(
35
- ForbiddenTypeFactory[MarshalContext, Marshaler],
36
- SimpleMarshalerFactory,
37
- ):
38
- pass
30
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
31
+ if rty not in self.rtys:
32
+ return None
39
33
 
34
+ def inner():
35
+ raise ForbiddenTypeError(rty)
40
36
 
41
- @dc.dataclass(frozen=True)
42
- class ForbiddenTypeUnmarshalerFactory(
43
- ForbiddenTypeFactory[UnmarshalContext, Unmarshaler],
44
- SimpleUnmarshalerFactory,
45
- ):
46
- pass
37
+ return inner
38
+
39
+
40
+ ForbiddenTypeMarshalerFactory = ForbiddenTypeMarshalerFactoryUnmarshalerFactory
41
+ ForbiddenTypeUnmarshalerFactory = ForbiddenTypeMarshalerFactoryUnmarshalerFactory
omlish/os/forkhooks.py CHANGED
@@ -1,5 +1,9 @@
1
1
  # ruff: noqa: UP006 UP007 UP045
2
2
  # @omlish-lite
3
+ """
4
+ TODO:
5
+ - https://github.com/python/cpython/issues/50970
6
+ """
3
7
  import os
4
8
  import threading
5
9
  import typing as ta
@@ -5,7 +5,6 @@ from ... import dataclasses as dc
5
5
  from ... import lang
6
6
  from ... import marshal as msh
7
7
  from ... import reflect as rfl
8
- from ...funcs import match as mfs
9
8
  from .types import NotSpecified
10
9
 
11
10
 
@@ -25,29 +24,39 @@ class NotSpecifiedUnionMarshaler(msh.Marshaler):
25
24
  return self.m.marshal(ctx, o)
26
25
 
27
26
 
28
- class NotSpecifiedUnionMarshalerFactory(msh.MarshalerFactoryMatchClass):
29
- @mfs.simple(lambda _, ctx, rty: (
30
- isinstance(rty, rfl.Union) and
31
- not rty.is_optional and
32
- _NOT_SPECIFIED_RTY in rty.args
33
- ))
34
- def _build(self, ctx: msh.MarshalContext, rty: rfl.Type) -> msh.Marshaler:
35
- args = set(check.isinstance(rty, rfl.Union).args) - {_NOT_SPECIFIED_RTY}
36
- nty = rfl.type_(ta.Union[*args])
37
- m = ctx.make(nty)
38
- return NotSpecifiedUnionMarshaler(m)
39
-
40
-
41
- class NotSpecifiedUnionUnmarshalerFactory(msh.UnmarshalerFactoryMatchClass):
42
- @mfs.simple(lambda _, ctx, rty: (
43
- isinstance(rty, rfl.Union) and
44
- not rty.is_optional and
45
- _NOT_SPECIFIED_RTY in rty.args
46
- ))
47
- def _build(self, ctx: msh.UnmarshalContext, rty: rfl.Type) -> msh.Unmarshaler:
48
- args = set(check.isinstance(rty, rfl.Union).args) - {_NOT_SPECIFIED_RTY}
49
- nty = rfl.type_(ta.Union[*args])
50
- return ctx.make(nty)
27
+ class NotSpecifiedUnionMarshalerFactory(msh.MarshalerFactory):
28
+ def make_marshaler(self, ctx: msh.MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Marshaler] | None:
29
+ if not (
30
+ isinstance(rty, rfl.Union) and
31
+ not rty.is_optional and
32
+ _NOT_SPECIFIED_RTY in rty.args
33
+ ):
34
+ return None
35
+
36
+ def inner() -> msh.Marshaler:
37
+ args = set(check.isinstance(rty, rfl.Union).args) - {_NOT_SPECIFIED_RTY}
38
+ nty = rfl.type_(ta.Union[*args])
39
+ m = ctx.make_marshaler(nty)
40
+ return NotSpecifiedUnionMarshaler(m)
41
+
42
+ return inner
43
+
44
+
45
+ class NotSpecifiedUnionUnmarshalerFactory(msh.UnmarshalerFactory):
46
+ def make_unmarshaler(self, ctx: msh.UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Unmarshaler] | None: # noqa
47
+ if not (
48
+ isinstance(rty, rfl.Union) and
49
+ not rty.is_optional and
50
+ _NOT_SPECIFIED_RTY in rty.args
51
+ ):
52
+ return None
53
+
54
+ def inner() -> msh.Unmarshaler:
55
+ args = set(check.isinstance(rty, rfl.Union).args) - {_NOT_SPECIFIED_RTY}
56
+ nty = rfl.type_(ta.Union[*args])
57
+ return ctx.make_unmarshaler(nty)
58
+
59
+ return inner
51
60
 
52
61
 
53
62
  @lang.static_init
@@ -5,7 +5,6 @@ from ... import dataclasses as dc
5
5
  from ... import lang
6
6
  from ... import marshal as msh
7
7
  from ... import reflect as rfl
8
- from ...funcs import match as mfs
9
8
  from .. import jsonschema as jsch
10
9
  from .openapi import Reference
11
10
  from .openapi import Schema
@@ -36,10 +35,14 @@ class _ReferenceUnionMarshaler(msh.Marshaler):
36
35
  return self.m.marshal(ctx, o)
37
36
 
38
37
 
39
- class _ReferenceUnionMarshalerFactory(msh.MarshalerFactoryMatchClass):
40
- @mfs.simple(lambda _, ctx, rty: _reference_union_arg(rty) is not None)
41
- def _build(self, ctx: msh.MarshalContext, rty: rfl.Type) -> msh.Marshaler:
42
- return _ReferenceUnionMarshaler(ctx.make(check.not_none(_reference_union_arg(rty))), ctx.make(Reference))
38
+ class _ReferenceUnionMarshalerFactory(msh.MarshalerFactory):
39
+ def make_marshaler(self, ctx: msh.MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Marshaler] | None:
40
+ if (rua := _reference_union_arg(rty)) is None:
41
+ return None
42
+ return lambda: _ReferenceUnionMarshaler(
43
+ ctx.make_marshaler(check.not_none(rua)),
44
+ ctx.make_marshaler(Reference),
45
+ )
43
46
 
44
47
 
45
48
  #
@@ -59,10 +62,14 @@ class _ReferenceUnionUnmarshaler(msh.Unmarshaler):
59
62
  return self.u.unmarshal(ctx, v) # noqa
60
63
 
61
64
 
62
- class _ReferenceUnionUnmarshalerFactory(msh.UnmarshalerFactoryMatchClass):
63
- @mfs.simple(lambda _, ctx, rty: _reference_union_arg(rty) is not None)
64
- def _build(self, ctx: msh.UnmarshalContext, rty: rfl.Type) -> msh.Unmarshaler:
65
- return _ReferenceUnionUnmarshaler(ctx.make(check.not_none(_reference_union_arg(rty))), ctx.make(Reference))
65
+ class _ReferenceUnionUnmarshalerFactory(msh.UnmarshalerFactory):
66
+ def make_unmarshaler(self, ctx: msh.UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Unmarshaler] | None: # noqa
67
+ if (rua := _reference_union_arg(rty)) is None:
68
+ return None
69
+ return lambda: _ReferenceUnionUnmarshaler(
70
+ ctx.make_unmarshaler(check.not_none(rua)),
71
+ ctx.make_unmarshaler(Reference),
72
+ )
66
73
 
67
74
 
68
75
  ##
@@ -86,16 +93,17 @@ class _SchemaMarshaler(msh.Marshaler):
86
93
  return dct
87
94
 
88
95
 
89
- class _SchemaMarshalerFactory(msh.MarshalerFactoryMatchClass):
90
- @mfs.simple(lambda _, ctx, rty: rty is Schema)
91
- def _build(self, ctx: msh.MarshalContext, rty: rfl.Type) -> msh.Marshaler:
92
- return _SchemaMarshaler(
96
+ class _SchemaMarshalerFactory(msh.MarshalerFactory):
97
+ def make_marshaler(self, ctx: msh.MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Marshaler] | None:
98
+ if rty is not Schema:
99
+ return None
100
+ return lambda: _SchemaMarshaler(
93
101
  {
94
- f: (msh.translate_name(f, msh.Naming.LOW_CAMEL), ctx.make(rfl.type_(a)))
102
+ f: (msh.translate_name(f, msh.Naming.LOW_CAMEL), ctx.make_marshaler(rfl.type_(a)))
95
103
  for f, a in dc.reflect(Schema).field_annotations.items()
96
104
  if f != 'keywords'
97
105
  },
98
- ctx.make(jsch.Keywords),
106
+ ctx.make_marshaler(jsch.Keywords),
99
107
  )
100
108
 
101
109
 
@@ -121,16 +129,17 @@ class _SchemaUnmarshaler(msh.Unmarshaler):
121
129
  return Schema(**kw)
122
130
 
123
131
 
124
- class _SchemaUnmarshalerFactory(msh.UnmarshalerFactoryMatchClass):
125
- @mfs.simple(lambda _, ctx, rty: rty is Schema)
126
- def _build(self, ctx: msh.UnmarshalContext, rty: rfl.Type) -> msh.Unmarshaler:
127
- return _SchemaUnmarshaler(
132
+ class _SchemaUnmarshalerFactory(msh.UnmarshalerFactory):
133
+ def make_unmarshaler(self, ctx: msh.UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], msh.Unmarshaler] | None: # noqa
134
+ if rty is not Schema:
135
+ return None
136
+ return lambda: _SchemaUnmarshaler(
128
137
  {
129
- msh.translate_name(f, msh.Naming.LOW_CAMEL): (f, ctx.make(rfl.type_(a)))
138
+ msh.translate_name(f, msh.Naming.LOW_CAMEL): (f, ctx.make_unmarshaler(rfl.type_(a)))
130
139
  for f, a in dc.reflect(Schema).field_annotations.items()
131
140
  if f != 'keywords'
132
141
  },
133
- ctx.make(jsch.Keywords),
142
+ ctx.make_unmarshaler(jsch.Keywords),
134
143
  )
135
144
 
136
145