omlish 0.0.0.dev173__py3-none-any.whl → 0.0.0.dev175__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev173'
2
- __revision__ = 'af782a809468cfc82da86cb06197a69f7e749145'
1
+ __version__ = '0.0.0.dev175'
2
+ __revision__ = '52c77498eb4f4bf0082f758cb859d51b69f93f9c'
3
3
 
4
4
 
5
5
  #
omlish/lite/marshal.py CHANGED
@@ -21,9 +21,11 @@ import weakref # noqa
21
21
 
22
22
  from .check import check
23
23
  from .reflect import deep_subclasses
24
+ from .reflect import get_literal_type_args
24
25
  from .reflect import get_new_type_supertype
25
26
  from .reflect import get_optional_alias_arg
26
27
  from .reflect import is_generic_alias
28
+ from .reflect import is_literal_type
27
29
  from .reflect import is_new_type
28
30
  from .reflect import is_union_alias
29
31
  from .strings import snake_case
@@ -141,6 +143,18 @@ class OptionalObjMarshaler(ObjMarshaler):
141
143
  return self.item.unmarshal(o, ctx)
142
144
 
143
145
 
146
+ @dc.dataclass(frozen=True)
147
+ class LiteralObjMarshaler(ObjMarshaler):
148
+ item: ObjMarshaler
149
+ vs: frozenset
150
+
151
+ def marshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
152
+ return self.item.marshal(check.in_(o, self.vs), ctx)
153
+
154
+ def unmarshal(self, o: ta.Any, ctx: 'ObjMarshalContext') -> ta.Any:
155
+ return check.in_(self.item.unmarshal(o, ctx), self.vs)
156
+
157
+
144
158
  @dc.dataclass(frozen=True)
145
159
  class MappingObjMarshaler(ObjMarshaler):
146
160
  ty: type
@@ -352,6 +366,11 @@ class ObjMarshalerManager:
352
366
  if is_new_type(ty):
353
367
  return rec(get_new_type_supertype(ty))
354
368
 
369
+ if is_literal_type(ty):
370
+ lvs = frozenset(get_literal_type_args(ty))
371
+ lty = check.single(set(map(type, lvs)))
372
+ return LiteralObjMarshaler(rec(lty), lvs)
373
+
355
374
  if is_generic_alias(ty):
356
375
  try:
357
376
  mt = self._generic_mapping_types[ta.get_origin(ty)]
omlish/lite/reflect.py CHANGED
@@ -7,6 +7,9 @@ import typing as ta
7
7
  T = ta.TypeVar('T')
8
8
 
9
9
 
10
+ ##
11
+
12
+
10
13
  _GENERIC_ALIAS_TYPES = (
11
14
  ta._GenericAlias, # type: ignore # noqa
12
15
  *([ta._SpecialGenericAlias] if hasattr(ta, '_SpecialGenericAlias') else []), # noqa
@@ -24,6 +27,9 @@ is_union_alias = functools.partial(is_generic_alias, origin=ta.Union)
24
27
  is_callable_alias = functools.partial(is_generic_alias, origin=ta.Callable)
25
28
 
26
29
 
30
+ ##
31
+
32
+
27
33
  def is_optional_alias(spec: ta.Any) -> bool:
28
34
  return (
29
35
  isinstance(spec, _GENERIC_ALIAS_TYPES) and # noqa
@@ -38,6 +44,9 @@ def get_optional_alias_arg(spec: ta.Any) -> ta.Any:
38
44
  return it
39
45
 
40
46
 
47
+ ##
48
+
49
+
41
50
  def is_new_type(spec: ta.Any) -> bool:
42
51
  if isinstance(ta.NewType, type):
43
52
  return isinstance(spec, ta.NewType)
@@ -50,6 +59,26 @@ def get_new_type_supertype(spec: ta.Any) -> ta.Any:
50
59
  return spec.__supertype__
51
60
 
52
61
 
62
+ ##
63
+
64
+
65
+ def is_literal_type(spec: ta.Any) -> bool:
66
+ if hasattr(ta, '_LiteralGenericAlias'):
67
+ return isinstance(spec, ta._LiteralGenericAlias) # noqa
68
+ else:
69
+ return (
70
+ isinstance(spec, ta._GenericAlias) and # type: ignore # noqa
71
+ spec.__origin__ is ta.Literal
72
+ )
73
+
74
+
75
+ def get_literal_type_args(spec: ta.Any) -> ta.Iterable[ta.Any]:
76
+ return spec.__args__
77
+
78
+
79
+ ##
80
+
81
+
53
82
  def deep_subclasses(cls: ta.Type[T]) -> ta.Iterator[ta.Type[T]]:
54
83
  seen = set()
55
84
  todo = list(reversed(cls.__subclasses__()))
@@ -0,0 +1,50 @@
1
+ import dataclasses as dc
2
+ import typing as ta
3
+
4
+ from .. import check
5
+ from .. import reflect as rfl
6
+ from .base import MarshalContext
7
+ from .base import Marshaler
8
+ from .base import MarshalerFactory
9
+ from .base import UnmarshalContext
10
+ from .base import Unmarshaler
11
+ from .base import UnmarshalerFactory
12
+ from .values import Value
13
+
14
+
15
+ @dc.dataclass(frozen=True)
16
+ class LiteralMarshaler(Marshaler):
17
+ e: Marshaler
18
+ vs: frozenset
19
+
20
+ def marshal(self, ctx: MarshalContext, o: ta.Any | None) -> Value:
21
+ return self.e.marshal(ctx, check.in_(o, self.vs))
22
+
23
+
24
+ class LiteralMarshalerFactory(MarshalerFactory):
25
+ def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
26
+ return isinstance(rty, rfl.Literal)
27
+
28
+ def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
29
+ lty = check.isinstance(rty, rfl.Literal)
30
+ ety = check.single(set(map(type, lty.args)))
31
+ return LiteralMarshaler(ctx.make(ety), frozenset(lty.args))
32
+
33
+
34
+ @dc.dataclass(frozen=True)
35
+ class LiteralUnmarshaler(Unmarshaler):
36
+ e: Unmarshaler
37
+ vs: frozenset
38
+
39
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any | None:
40
+ return check.in_(self.e.unmarshal(ctx, v), self.vs)
41
+
42
+
43
+ class LiteralUnmarshalerFactory(UnmarshalerFactory):
44
+ def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
45
+ return isinstance(rty, rfl.Literal)
46
+
47
+ def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
48
+ lty = check.isinstance(rty, rfl.Literal)
49
+ ety = check.single(set(map(type, lty.args)))
50
+ return LiteralUnmarshaler(ctx.make(ety), frozenset(lty.args))
@@ -17,6 +17,8 @@ from .enums import EnumMarshalerFactory
17
17
  from .enums import EnumUnmarshalerFactory
18
18
  from .iterables import IterableMarshalerFactory
19
19
  from .iterables import IterableUnmarshalerFactory
20
+ from .literals import LiteralMarshalerFactory
21
+ from .literals import LiteralUnmarshalerFactory
20
22
  from .mappings import MappingMarshalerFactory
21
23
  from .mappings import MappingUnmarshalerFactory
22
24
  from .maybes import MaybeMarshalerFactory
@@ -48,6 +50,7 @@ STANDARD_MARSHALER_FACTORIES: list[MarshalerFactory] = [
48
50
  DataclassMarshalerFactory(),
49
51
  NamedtupleMarshalerFactory(),
50
52
  EnumMarshalerFactory(),
53
+ LiteralMarshalerFactory(),
51
54
  NUMBERS_MARSHALER_FACTORY,
52
55
  UUID_MARSHALER_FACTORY,
53
56
  BASE64_MARSHALER_FACTORY,
@@ -80,6 +83,7 @@ STANDARD_UNMARSHALER_FACTORIES: list[UnmarshalerFactory] = [
80
83
  DataclassUnmarshalerFactory(),
81
84
  NamedtupleUnmarshalerFactory(),
82
85
  EnumUnmarshalerFactory(),
86
+ LiteralUnmarshalerFactory(),
83
87
  NUMBERS_UNMARSHALER_FACTORY,
84
88
  UUID_UNMARSHALER_FACTORY,
85
89
  BASE64_UNMARSHALER_FACTORY,
@@ -27,6 +27,7 @@ from .types import ( # noqa
27
27
  Any,
28
28
  DEFAULT_REFLECTOR,
29
29
  Generic,
30
+ Literal,
30
31
  NewType,
31
32
  ReflectTypeError,
32
33
  Reflector,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omlish
3
- Version: 0.0.0.dev173
3
+ Version: 0.0.0.dev175
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=lRkBDFxlAbf6lN5upo3WSf-owW8YG1T21dfpbQL-XHM,7598
2
- omlish/__about__.py,sha256=lwpg2kAkCyobGpYChlHlrlHiyjWU48sngnP5x3kz8J8,3409
2
+ omlish/__about__.py,sha256=25OnaWS3SJi0TLvUrE__wNhmSlkqlEh4wv-95tE1BY0,3409
3
3
  omlish/__init__.py,sha256=SsyiITTuK0v74XpKV8dqNaCmjOlan1JZKrHQv5rWKPA,253
4
4
  omlish/c3.py,sha256=ubu7lHwss5V4UznbejAI0qXhXahrU01MysuHOZI9C4U,8116
5
5
  omlish/cached.py,sha256=UI-XTFBwA6YXWJJJeBn-WkwBkfzDjLBBaZf4nIJA9y0,510
@@ -382,10 +382,10 @@ omlish/lite/dataclasses.py,sha256=M6UD4VwGo0Ky7RNzKWbO0IOy7iBZVCIbTiC6EYbFnX8,10
382
382
  omlish/lite/inject.py,sha256=EEaioN9ESAveVCMe2s5osjwI97FPRUVoU8P95vGUiYo,23376
383
383
  omlish/lite/json.py,sha256=7-02Ny4fq-6YAu5ynvqoijhuYXWpLmfCI19GUeZnb1c,740
384
384
  omlish/lite/logs.py,sha256=CWFG0NKGhqNeEgryF5atN2gkPYbUdTINEw_s1phbINM,51
385
- omlish/lite/marshal.py,sha256=ldoZs_yiQIUpOjBviV9f4mwm7hSZy0hRLXrvQA-6POU,14257
385
+ omlish/lite/marshal.py,sha256=8V2mWl6D8gfJK_a86YKwmSsBBvoKMucLK4uLBwVpF60,14910
386
386
  omlish/lite/maybes.py,sha256=7OlHJ8Q2r4wQ-aRbZSlJY7x0e8gDvufFdlohGEIJ3P4,833
387
387
  omlish/lite/pycharm.py,sha256=pUOJevrPClSqTCEOkQBO11LKX2003tfDcp18a03QFrc,1163
388
- omlish/lite/reflect.py,sha256=L5_9gNp_BmAZ3l9PVezDmiXFg_6BOHbfQNB98tUULW0,1765
388
+ omlish/lite/reflect.py,sha256=pzOY2PPuHH0omdtglkN6DheXDrGopdL3PtTJnejyLFU,2189
389
389
  omlish/lite/resources.py,sha256=YNSmX1Ohck1aoWRs55a-o5ChVbFJIQhtbqE-XwF55Oc,326
390
390
  omlish/lite/runtime.py,sha256=XQo408zxTdJdppUZqOWHyeUR50VlCpNIExNGHz4U6O4,459
391
391
  omlish/lite/secrets.py,sha256=3Mz3V2jf__XU9qNHcH56sBSw95L3U2UPL24bjvobG0c,816
@@ -422,6 +422,7 @@ omlish/marshal/forbidden.py,sha256=NDe828hqCQw-AgxcEm8MiDZNxWoBwf3o7sTyvQsSsQ0,8
422
422
  omlish/marshal/global_.py,sha256=K76wB1-pdg4VWgiqR7wyxRNYr-voJApexYW2nV-R4DM,1127
423
423
  omlish/marshal/helpers.py,sha256=-SOgYJmrURILHpPK6Wu3cCvhj8RJrqfJxuKhh9UMs7o,1102
424
424
  omlish/marshal/iterables.py,sha256=H9FoCB5RlJW0SVSi3SBD6sxOXN9XRTOUkHRwCYSqRb8,2632
425
+ omlish/marshal/literals.py,sha256=m92biF9-PwHGVBx3zG3s2YvklLeOQ7T5IOdgwBJr-NU,1599
425
426
  omlish/marshal/mappings.py,sha256=XcNOaV708ZHeuIrWiFHC6F1O6U9NyyTKUurvXwIryJo,2789
426
427
  omlish/marshal/maybes.py,sha256=i-gOQJ-7tdt6sOazAeyCh4N71SK3jWv-BKlkx-fZm-s,2200
427
428
  omlish/marshal/namedtuples.py,sha256=H0icxcE5FrqgfI9dRc8LlXJy430g_yuILTCVD0Zvfd0,2799
@@ -434,7 +435,7 @@ omlish/marshal/optionals.py,sha256=r0XB5rqfasvgZJNrKYd6Unq2U4nHt3JURi26j0dYHlw,1
434
435
  omlish/marshal/polymorphism.py,sha256=2SxrfneA9QdhNdxieEGFnHDHpUo3ftETA9dMbCbmbWY,6511
435
436
  omlish/marshal/primitives.py,sha256=f_6m24Cb-FDGsZpYSas11nLt3xCCEUXugw3Hv4-aNhg,1291
436
437
  omlish/marshal/registries.py,sha256=FvC6qXHCizNB2QmU_N3orxW7iqfGYkiUXYYdTRWS6HA,2353
437
- omlish/marshal/standard.py,sha256=UMET8wkxf__ApIDuVcoCKsjNWwZejbICLeXhhmn56Nk,3321
438
+ omlish/marshal/standard.py,sha256=-iA2o674AW1uNVabcA9Dtr6z0gyFS5yLP80nBQ8R5qY,3479
438
439
  omlish/marshal/unions.py,sha256=tT4W-mxuPi7s_kdl25_AUltsurYPO_0mQl2CiVmNonY,4357
439
440
  omlish/marshal/utils.py,sha256=puKJpwPpuDlMOIrKMcLTRLJyMiL6n_Xs-p59AuDEymA,543
440
441
  omlish/marshal/uuids.py,sha256=H4B7UX_EPNmP2tC8bubcKrPLTS4aQu98huvbXQ3Zv2g,910
@@ -452,7 +453,7 @@ omlish/os/linux.py,sha256=whJ6scwMKSFBdXiVhJW0BCpJV4jOGMr-a_a3Bhwz6Ls,18938
452
453
  omlish/os/paths.py,sha256=hqPiyg_eYaRoIVPdAeX4oeLEV4Kpln_XsH0tHvbOf8Q,844
453
454
  omlish/os/pidfile.py,sha256=S4Nbe00oSxckY0qCC9AeTEZe7NSw4eJudnQX7wCXzks,1738
454
455
  omlish/os/sizes.py,sha256=ohkALLvqSqBX4iR-7DMKJ4pfOCRdZXV8htH4QywUNM0,152
455
- omlish/reflect/__init__.py,sha256=5TAeB3SoPR8MgnkdyVMHpAoTtlQX0BWpQYV0yDaECuM,756
456
+ omlish/reflect/__init__.py,sha256=JBWwxKwP4IEaomkK0PTju02STU1BVXT14SCrShT1Sm0,769
456
457
  omlish/reflect/inspect.py,sha256=veJ424-9oZrqyvhVpvxOi7hcKW-PDBkdYL2yjrFlk4o,495
457
458
  omlish/reflect/ops.py,sha256=RJ6jzrM4ieFsXzWyNXWV43O_WgzEaUvlHSc5N2ezW2A,2044
458
459
  omlish/reflect/subst.py,sha256=g3q7NmNWsmc67mcchmCE3WFPCMDSBq-FXn4ah-DWL_U,3622
@@ -569,9 +570,9 @@ omlish/text/glyphsplit.py,sha256=Ug-dPRO7x-OrNNr8g1y6DotSZ2KH0S-VcOmUobwa4B0,329
569
570
  omlish/text/indent.py,sha256=6Jj6TFY9unaPa4xPzrnZemJ-fHsV53IamP93XGjSUHs,1274
570
571
  omlish/text/parts.py,sha256=7vPF1aTZdvLVYJ4EwBZVzRSy8XB3YqPd7JwEnNGGAOo,6495
571
572
  omlish/text/random.py,sha256=jNWpqiaKjKyTdMXC-pWAsSC10AAP-cmRRPVhm59ZWLk,194
572
- omlish-0.0.0.dev173.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
573
- omlish-0.0.0.dev173.dist-info/METADATA,sha256=aPEQU46sbHnNUIeFdMJpzGlI3UPn_pJED6zSN3odpiw,4264
574
- omlish-0.0.0.dev173.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
575
- omlish-0.0.0.dev173.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
576
- omlish-0.0.0.dev173.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
577
- omlish-0.0.0.dev173.dist-info/RECORD,,
573
+ omlish-0.0.0.dev175.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
574
+ omlish-0.0.0.dev175.dist-info/METADATA,sha256=IPfi7oJ83dAfqstWY5EBl2n0I-_yPL4tcbv61BIDhx4,4264
575
+ omlish-0.0.0.dev175.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
576
+ omlish-0.0.0.dev175.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
577
+ omlish-0.0.0.dev175.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
578
+ omlish-0.0.0.dev175.dist-info/RECORD,,