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,44 +2,39 @@ import threading
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 BaseContext
7
- from ..base.contexts import MarshalContext
8
- from ..base.contexts import UnmarshalContext
5
+ from ..base.contexts import MarshalFactoryContext
6
+ from ..base.contexts import UnmarshalFactoryContext
9
7
  from ..base.types import Marshaler
10
8
  from ..base.types import MarshalerFactory
11
- from ..base.types import MarshalerMaker
12
9
  from ..base.types import Unmarshaler
13
10
  from ..base.types import UnmarshalerFactory
14
- from ..base.types import UnmarshalerMaker
15
11
 
16
12
 
17
- R = ta.TypeVar('R')
18
- ContextT = ta.TypeVar('ContextT', bound=BaseContext)
13
+ FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
19
14
 
20
15
 
21
16
  ##
22
17
 
23
18
 
24
- class _InvalidatableFactory(mfs.MatchFn[[ContextT, rfl.Type], R]):
19
+ class _InvalidatableFactory(ta.Generic[FactoryT]):
25
20
  def __init__(
26
21
  self,
27
- fn: ta.Callable[[], mfs.MatchFn[[ContextT, rfl.Type], R]],
22
+ fac_fac: ta.Callable,
28
23
  check_fn: ta.Callable[[], bool] | None = None,
29
24
  ) -> None:
30
25
  super().__init__()
31
26
 
32
- self._fn = fn
27
+ self._fac_fac = fac_fac
33
28
  self._check_fn: ta.Callable[[], bool] | None = check_fn
34
29
 
35
30
  self._lock = threading.RLock()
36
31
 
37
32
  #
38
33
 
39
- _f_: mfs.MatchFn[[ContextT, rfl.Type], R] | None = None
34
+ __fac: FactoryT | None = None
40
35
 
41
36
  def _invalidate(self) -> None:
42
- self._f_ = None
37
+ self.__fac = None
43
38
 
44
39
  def invalidate(self) -> None:
45
40
  with self._lock:
@@ -52,67 +47,22 @@ class _InvalidatableFactory(mfs.MatchFn[[ContextT, rfl.Type], R]):
52
47
  if self._check_fn():
53
48
  self._invalidate()
54
49
 
55
- def _f(self) -> mfs.MatchFn[[ContextT, rfl.Type], R]:
50
+ def _fac(self) -> FactoryT:
56
51
  self._maybe_invalidate()
57
52
 
58
- if (f := self._f_) is None:
53
+ if (f := self.__fac) is None:
59
54
  with self._lock:
60
- if (f := self._f_) is None:
61
- f = self._f_ = self._fn()
55
+ if (f := self.__fac) is None:
56
+ f = self.__fac = self._fac_fac()
62
57
 
63
58
  return f
64
59
 
65
- #
66
-
67
- def guard(self, ctx: ContextT, rty: rfl.Type) -> bool:
68
- return self._f().guard(ctx, rty)
69
-
70
- def fn(self, ctx: ContextT, rty: rfl.Type) -> R:
71
- return self._f()(ctx, rty)
72
-
73
-
74
- ##
75
-
76
-
77
- class InvalidatableMarshalerFactory(MarshalerFactory):
78
- def __init__(
79
- self,
80
- fn: ta.Callable[[], MarshalerFactory],
81
- check_fn: ta.Callable[[], bool] | None = None,
82
- ) -> None:
83
- super().__init__()
84
60
 
85
- self._fn = fn
86
- self._u: _InvalidatableFactory[MarshalContext, Marshaler] = _InvalidatableFactory(
87
- lambda: fn().make_marshaler,
88
- check_fn,
89
- )
61
+ class InvalidatableMarshalerFactory(_InvalidatableFactory[MarshalerFactory], MarshalerFactory):
62
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
63
+ return self._fac().make_marshaler(ctx, rty)
90
64
 
91
- def invalidate(self) -> None:
92
- self._u.invalidate()
93
-
94
- @property
95
- def make_marshaler(self) -> MarshalerMaker:
96
- return self._u
97
-
98
-
99
- class InvalidatableUnmarshalerFactory(UnmarshalerFactory):
100
- def __init__(
101
- self,
102
- fn: ta.Callable[[], UnmarshalerFactory],
103
- check_fn: ta.Callable[[], bool] | None = None,
104
- ) -> None:
105
- super().__init__()
106
-
107
- self._fn = fn
108
- self._u: _InvalidatableFactory[UnmarshalContext, Unmarshaler] = _InvalidatableFactory(
109
- lambda: fn().make_unmarshaler,
110
- check_fn,
111
- )
112
-
113
- def invalidate(self) -> None:
114
- self._u.invalidate()
115
65
 
116
- @property
117
- def make_unmarshaler(self) -> UnmarshalerMaker:
118
- return self._u
66
+ class InvalidatableUnmarshalerFactory(_InvalidatableFactory[UnmarshalerFactory], UnmarshalerFactory):
67
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
68
+ return self._fac().make_unmarshaler(ctx, rty)
@@ -0,0 +1,26 @@
1
+ import typing as ta
2
+
3
+ from ... import lang
4
+ from ... import reflect as rfl
5
+ from ...funcs import guard as gfs
6
+ from ..base.contexts import MarshalFactoryContext
7
+ from ..base.contexts import UnmarshalFactoryContext
8
+ from ..base.types import Marshaler
9
+ from ..base.types import MarshalerFactory
10
+ from ..base.types import Unmarshaler
11
+ from ..base.types import UnmarshalerFactory
12
+
13
+
14
+ ##
15
+
16
+
17
+ class MarshalerFactoryMethodClass(MarshalerFactory, lang.Abstract):
18
+ @gfs.method(instance_cache=True)
19
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
20
+ raise NotImplementedError
21
+
22
+
23
+ class UnmarshalerFactoryMethodClass(UnmarshalerFactory, lang.Abstract):
24
+ @gfs.method(instance_cache=True)
25
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
26
+ raise NotImplementedError
@@ -11,41 +11,37 @@ import threading
11
11
  import typing as ta
12
12
 
13
13
  from .... import reflect as rfl
14
- from ....funcs import match as mfs
15
14
  from ...base.contexts import BaseContext
16
- from ...base.contexts import MarshalContext
17
- from ...base.contexts import UnmarshalContext
15
+ from ...base.contexts import MarshalFactoryContext
16
+ from ...base.contexts import UnmarshalFactoryContext
18
17
  from ...base.types import Marshaler
19
18
  from ...base.types import MarshalerFactory
20
- from ...base.types import MarshalerMaker
21
19
  from ...base.types import Unmarshaler
22
20
  from ...base.types import UnmarshalerFactory
23
- from ...base.types import UnmarshalerMaker
24
21
  from .configs import ModuleImport
25
22
 
26
23
 
27
- R = ta.TypeVar('R')
28
- ContextT = ta.TypeVar('ContextT', bound=BaseContext)
24
+ FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
29
25
 
30
26
 
31
27
  ##
32
28
 
33
29
 
34
- class _ModuleImportingFactory(mfs.MatchFn[[ContextT, rfl.Type], R]):
30
+ class _ModuleImportingFactory(ta.Generic[FactoryT]):
35
31
  def __init__(
36
32
  self,
37
- f: mfs.MatchFn[[ContextT, rfl.Type], R],
33
+ fac: FactoryT,
38
34
  callback: ta.Callable[[], None] | None = None,
39
35
  ) -> None:
40
36
  super().__init__()
41
37
 
42
- self._f = f
38
+ self._fac = fac
43
39
  self._callback = callback
44
40
 
45
41
  self._lock = threading.RLock()
46
42
  self._last_mis: ta.Any = None
47
43
 
48
- def _do_import(self, ctx: ContextT, mis: ta.Sequence[ModuleImport]) -> None:
44
+ def _do_import(self, ctx: BaseContext, mis: ta.Sequence[ModuleImport]) -> None:
49
45
  c = 0
50
46
  for mi in mis:
51
47
  if mi.import_if_necessary():
@@ -55,58 +51,21 @@ class _ModuleImportingFactory(mfs.MatchFn[[ContextT, rfl.Type], R]):
55
51
  if self._callback is not None:
56
52
  self._callback()
57
53
 
58
- def _import_if_necessary(self, ctx: ContextT) -> None:
54
+ def _import_if_necessary(self, ctx: BaseContext) -> None:
59
55
  if (mis := ctx.config_registry.get_of(None, ModuleImport)) and mis is not self._last_mis:
60
56
  with self._lock:
61
57
  if (mis := ctx.config_registry.get_of(None, ModuleImport)) and mis is not self._last_mis:
62
58
  self._do_import(ctx, mis)
63
59
  self._last_mis = mis
64
60
 
65
- def guard(self, ctx: ContextT, rty: rfl.Type) -> bool:
66
- self._import_if_necessary(ctx)
67
- return self._f.guard(ctx, rty)
68
61
 
69
- def fn(self, ctx: ContextT, rty: rfl.Type) -> R:
62
+ class ModuleImportingMarshalerFactory(_ModuleImportingFactory[MarshalerFactory], MarshalerFactory):
63
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
70
64
  self._import_if_necessary(ctx)
71
- return self._f(ctx, rty)
72
-
73
-
74
- ##
75
-
76
-
77
- class ModuleImportingMarshalerFactory(MarshalerFactory):
78
- def __init__(
79
- self,
80
- f: MarshalerFactory,
81
- callback: ta.Callable[[], None] | None = None,
82
- ) -> None:
83
- super().__init__()
65
+ return self._fac.make_marshaler(ctx, rty)
84
66
 
85
- self._f = f
86
- self._tcf: _ModuleImportingFactory[MarshalContext, Marshaler] = _ModuleImportingFactory(
87
- f.make_marshaler,
88
- callback,
89
- )
90
67
 
91
- @property
92
- def make_marshaler(self) -> MarshalerMaker:
93
- return self._tcf
94
-
95
-
96
- class ModuleImportingUnmarshalerFactory(UnmarshalerFactory):
97
- def __init__(
98
- self,
99
- f: UnmarshalerFactory,
100
- callback: ta.Callable[[], None] | None = None,
101
- ) -> None:
102
- super().__init__()
103
-
104
- self._f = f
105
- self._tcf: _ModuleImportingFactory[UnmarshalContext, Unmarshaler] = _ModuleImportingFactory(
106
- f.make_unmarshaler,
107
- callback,
108
- )
109
-
110
- @property
111
- def make_unmarshaler(self) -> UnmarshalerMaker:
112
- return self._tcf
68
+ class ModuleImportingUnmarshalerFactory(_ModuleImportingFactory[UnmarshalerFactory], UnmarshalerFactory):
69
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
70
+ self._import_if_necessary(ctx)
71
+ return self._fac.make_unmarshaler(ctx, rty)
@@ -1,15 +1,13 @@
1
1
  import typing as ta
2
2
 
3
3
  from ... import reflect as rfl
4
- from ...funcs import match as mfs
5
- from ..base.contexts import MarshalContext
6
- from ..base.contexts import UnmarshalContext
4
+ from ...funcs import guard as gfs
5
+ from ..base.contexts import MarshalFactoryContext
6
+ from ..base.contexts import UnmarshalFactoryContext
7
7
  from ..base.types import Marshaler
8
8
  from ..base.types import MarshalerFactory
9
- from ..base.types import MarshalerMaker
10
9
  from ..base.types import Unmarshaler
11
10
  from ..base.types import UnmarshalerFactory
12
- from ..base.types import UnmarshalerMaker
13
11
 
14
12
 
15
13
  ##
@@ -18,38 +16,28 @@ from ..base.types import UnmarshalerMaker
18
16
  class MultiMarshalerFactory(MarshalerFactory):
19
17
  def __init__(
20
18
  self,
21
- fs: ta.Iterable[MarshalerFactory],
22
- *,
19
+ *facs: MarshalerFactory,
23
20
  strict: bool = False,
24
21
  ) -> None:
25
22
  super().__init__()
26
23
 
27
- self._fs = list(fs)
28
- self._mmf: mfs.MultiMatchFn[[MarshalContext, rfl.Type], Marshaler] = mfs.MultiMatchFn(
29
- [f.make_marshaler for f in self._fs],
30
- strict=strict,
31
- )
24
+ self._facs = facs
25
+ self._mgf = gfs.multi(*[f.make_marshaler for f in self._facs], strict=strict)
32
26
 
33
- @property
34
- def make_marshaler(self) -> MarshalerMaker:
35
- return self._mmf
27
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
28
+ return self._mgf(ctx, rty)
36
29
 
37
30
 
38
31
  class MultiUnmarshalerFactory(UnmarshalerFactory):
39
32
  def __init__(
40
33
  self,
41
- fs: ta.Iterable[UnmarshalerFactory],
42
- *,
34
+ *facs: UnmarshalerFactory,
43
35
  strict: bool = False,
44
36
  ) -> None:
45
37
  super().__init__()
46
38
 
47
- self._fs = list(fs)
48
- self._mmf: mfs.MultiMatchFn[[UnmarshalContext, rfl.Type], Unmarshaler] = mfs.MultiMatchFn(
49
- [f.make_unmarshaler for f in self._fs],
50
- strict=strict,
51
- )
39
+ self._facs = facs
40
+ self._mgf = gfs.multi(*[f.make_unmarshaler for f in self._facs], strict=strict)
52
41
 
53
- @property
54
- def make_unmarshaler(self) -> UnmarshalerMaker:
55
- return self._mmf
42
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
43
+ return self._mgf(ctx, rty)
@@ -1,61 +1,54 @@
1
1
  import typing as ta
2
2
 
3
- from ... import check
4
- from ... import lang
5
3
  from ... import reflect as rfl
6
- from ...funcs import match as mfs
7
4
  from ..base.contexts import MarshalContext
5
+ from ..base.contexts import MarshalFactoryContext
8
6
  from ..base.contexts import UnmarshalContext
7
+ from ..base.contexts import UnmarshalFactoryContext
9
8
  from ..base.types import Marshaler
10
9
  from ..base.types import MarshalerFactory
11
- from ..base.types import MarshalerMaker
12
10
  from ..base.types import Unmarshaler
13
11
  from ..base.types import UnmarshalerFactory
14
- from ..base.types import UnmarshalerMaker
15
12
  from ..base.values import Value
16
13
 
17
14
 
15
+ FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
18
16
  T = ta.TypeVar('T')
19
- R = ta.TypeVar('R')
20
- C = ta.TypeVar('C')
21
17
 
22
18
 
23
19
  ##
24
20
 
25
21
 
26
- class _RecursiveTypeFactory(mfs.MatchFn[[C, rfl.Type], R]):
22
+ class _RecursiveFactory(ta.Generic[FactoryT]):
27
23
  def __init__(
28
24
  self,
29
- f: mfs.MatchFn[[C, rfl.Type], R],
30
- prx: ta.Callable[[], tuple[R, ta.Callable[[R], None]]],
25
+ fac: FactoryT,
26
+ prx: ta.Callable[[], tuple[ta.Any, ta.Callable[[ta.Any], None]]],
31
27
  ) -> None:
32
28
  super().__init__()
33
29
 
34
- self._f = f
30
+ self._fac = fac
35
31
  self._prx = prx
36
- self._dct: dict[rfl.Type, R] = {}
37
-
38
- def guard(self, ctx: C, rty: rfl.Type) -> bool:
39
- check.isinstance(rty, rfl.TYPES)
40
- return self._f.guard(ctx, rty)
41
-
42
- def fn(self, ctx: C, rty: rfl.Type) -> R:
43
- check.isinstance(rty, rfl.TYPES)
44
- try:
45
- return self._dct[rty]
46
- except KeyError:
47
- pass
48
- p, sp = self._prx()
49
- self._dct[rty] = p
50
- try:
51
- r = self._f(ctx, rty)
52
- sp(r)
53
- return r
54
- finally:
55
- del self._dct[rty]
56
32
 
33
+ self._dct: dict[rfl.Type, ta.Any] = {}
57
34
 
58
- ##
35
+ def _wrap(self, m, rty):
36
+ def inner():
37
+ try:
38
+ return self._dct[rty]
39
+ except KeyError:
40
+ pass
41
+
42
+ p, sp = self._prx()
43
+ self._dct[rty] = p
44
+ try:
45
+ r = m()
46
+ sp(r)
47
+ return r
48
+ finally:
49
+ del self._dct[rty]
50
+
51
+ return inner
59
52
 
60
53
 
61
54
  class _Proxy(ta.Generic[T]):
@@ -77,7 +70,7 @@ class _Proxy(ta.Generic[T]):
77
70
  return (p := cls()), p._set_obj # noqa
78
71
 
79
72
 
80
- ##
73
+ #
81
74
 
82
75
 
83
76
  class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
@@ -85,19 +78,17 @@ class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
85
78
  return self._obj.marshal(ctx, o)
86
79
 
87
80
 
88
- class RecursiveMarshalerFactory(MarshalerFactory, lang.Final):
89
- def __init__(self, f: MarshalerFactory) -> None:
90
- super().__init__()
81
+ class RecursiveMarshalerFactory(_RecursiveFactory[MarshalerFactory], MarshalerFactory):
82
+ def __init__(self, fac: MarshalerFactory) -> None:
83
+ super().__init__(fac, _ProxyMarshaler._new) # noqa
91
84
 
92
- self._f = f
93
- self._rtf: _RecursiveTypeFactory[MarshalContext, Marshaler] = _RecursiveTypeFactory(
94
- self._f.make_marshaler, # noqa
95
- _ProxyMarshaler._new, # noqa
96
- )
85
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
86
+ if (m := self._fac.make_marshaler(ctx, rty)) is None:
87
+ return None
88
+ return self._wrap(m, rty)
97
89
 
98
- @property
99
- def make_marshaler(self) -> MarshalerMaker:
100
- return self._rtf
90
+
91
+ #
101
92
 
102
93
 
103
94
  class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
@@ -105,16 +96,11 @@ class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
105
96
  return self._obj.unmarshal(ctx, v)
106
97
 
107
98
 
108
- class RecursiveUnmarshalerFactory(UnmarshalerFactory, lang.Final):
109
- def __init__(self, f: UnmarshalerFactory) -> None:
110
- super().__init__()
99
+ class RecursiveUnmarshalerFactory(_RecursiveFactory[UnmarshalerFactory], UnmarshalerFactory):
100
+ def __init__(self, fac: UnmarshalerFactory) -> None:
101
+ super().__init__(fac, _ProxyUnmarshaler._new) # noqa
111
102
 
112
- self._f = f
113
- self._rtf: _RecursiveTypeFactory[UnmarshalContext, Unmarshaler] = _RecursiveTypeFactory(
114
- self._f.make_unmarshaler, # noqa
115
- _ProxyUnmarshaler._new, # noqa
116
- )
117
-
118
- @property
119
- def make_unmarshaler(self) -> UnmarshalerMaker:
120
- return self._rtf
103
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
104
+ if (m := self._fac.make_unmarshaler(ctx, rty)) is None:
105
+ return None
106
+ return self._wrap(m, rty)
@@ -3,109 +3,52 @@ import typing as ta
3
3
 
4
4
  from ... import check
5
5
  from ... import reflect as rfl
6
- from ...funcs import match as mfs
7
- from ..base.contexts import MarshalContext
8
- from ..base.contexts import UnmarshalContext
6
+ from ..base.contexts import MarshalFactoryContext
7
+ from ..base.contexts import UnmarshalFactoryContext
9
8
  from ..base.types import Marshaler
10
9
  from ..base.types import MarshalerFactory
11
- from ..base.types import MarshalerMaker
12
10
  from ..base.types import Unmarshaler
13
11
  from ..base.types import UnmarshalerFactory
14
- from ..base.types import UnmarshalerMaker
15
12
 
16
13
 
17
- R = ta.TypeVar('R')
18
- C = ta.TypeVar('C')
14
+ FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
19
15
 
20
16
 
21
17
  ##
22
18
 
23
19
 
24
- class _TypeCacheFactory(mfs.MatchFn[[C, rfl.Type], R]):
25
- def __init__(self, f: mfs.MatchFn[[C, rfl.Type], R]) -> None:
20
+ class _TypeCacheFactory(ta.Generic[FactoryT]):
21
+ def __init__(self, fac: FactoryT) -> None:
26
22
  super().__init__()
27
23
 
28
- self._f = f
29
- self._dct: dict[rfl.Type, R | None] = {}
30
- self._lock = threading.RLock()
31
-
32
- def guard(self, ctx: C, rty: rfl.Type) -> bool:
33
- check.isinstance(rty, rfl.TYPES)
34
-
35
- try:
36
- return self._dct[rty] is not None
37
- except KeyError:
38
- pass
39
-
40
- with self._lock:
41
- try:
42
- e = self._dct[rty]
43
-
44
- except KeyError:
45
- if self._f.guard(ctx, rty):
46
- return True
47
- else:
48
- self._dct[rty] = None
49
- return False
24
+ self._fac = fac
50
25
 
51
- else:
52
- return e is not None
26
+ self._dct: dict[rfl.Type, ta.Any | None] = {}
27
+ self._lock = threading.RLock()
53
28
 
54
- def fn(self, ctx: C, rty: rfl.Type) -> R:
29
+ def _make(self, rty, dfl):
55
30
  check.isinstance(rty, rfl.TYPES)
56
31
 
57
32
  try:
58
- e = self._dct[rty]
33
+ return self._dct[rty]
59
34
  except KeyError:
60
35
  pass
61
- else:
62
- if e is None:
63
- raise mfs.MatchGuardError(ctx, rty)
64
- else:
65
- return e
66
36
 
67
37
  with self._lock:
68
38
  try:
69
- e = self._dct[rty]
70
-
39
+ return self._dct[rty]
71
40
  except KeyError:
72
- try:
73
- ret = self._f(ctx, rty)
74
- except mfs.MatchGuardError:
75
- self._dct[rty] = None
76
- raise
77
- else:
78
- self._dct[rty] = ret
79
- return ret
80
-
81
- if e is None:
82
- raise mfs.MatchGuardError(ctx, rty)
83
- else:
84
- return e
41
+ pass
85
42
 
86
-
87
- ##
43
+ m = self._dct[rty] = dfl()
44
+ return m
88
45
 
89
46
 
90
- class TypeCacheMarshalerFactory(MarshalerFactory):
91
- def __init__(self, f: MarshalerFactory) -> None:
92
- super().__init__()
93
-
94
- self._f = f
95
- self._tcf: _TypeCacheFactory[MarshalContext, Marshaler] = _TypeCacheFactory(f.make_marshaler)
96
-
97
- @property
98
- def make_marshaler(self) -> MarshalerMaker:
99
- return self._tcf
100
-
101
-
102
- class TypeCacheUnmarshalerFactory(UnmarshalerFactory):
103
- def __init__(self, f: UnmarshalerFactory) -> None:
104
- super().__init__()
47
+ class TypeCacheMarshalerFactory(_TypeCacheFactory[MarshalerFactory], MarshalerFactory):
48
+ def make_marshaler(self, ctx: MarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
49
+ return self._make(rty, lambda: self._fac.make_marshaler(ctx, rty))
105
50
 
106
- self._f = f
107
- self._tcf: _TypeCacheFactory[UnmarshalContext, Unmarshaler] = _TypeCacheFactory(f.make_unmarshaler)
108
51
 
109
- @property
110
- def make_unmarshaler(self) -> UnmarshalerMaker:
111
- return self._tcf
52
+ class TypeCacheUnmarshalerFactory(_TypeCacheFactory[UnmarshalerFactory], UnmarshalerFactory):
53
+ def make_unmarshaler(self, ctx: UnmarshalFactoryContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
54
+ return self._make(rty, lambda: self._fac.make_unmarshaler(ctx, rty))