omlish 0.0.0.dev408__py3-none-any.whl → 0.0.0.dev410__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.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev408'
2
- __revision__ = '5d4bd94d19abed3d89f070bc4748ab16aed6c9dc'
1
+ __version__ = '0.0.0.dev410'
2
+ __revision__ = 'fa58128e15cab568117709c80765dc8d633cb4e0'
3
3
 
4
4
 
5
5
  #
omlish/asyncs/all.py CHANGED
@@ -1,11 +1,3 @@
1
- from .sync import ( # noqa
2
- SyncableIterable,
3
- async_list,
4
- sync_await,
5
- sync_list,
6
- syncable_iterable,
7
- )
8
-
9
1
  from .bridge import ( # noqa
10
2
  a_to_s,
11
3
  is_in_bridge,
omlish/asyncs/bridge.py CHANGED
@@ -43,7 +43,6 @@ from .. import check
43
43
  from .. import lang
44
44
  from .. import sync
45
45
  from ..concurrent import threadlets
46
- from .sync import sync_await
47
46
 
48
47
 
49
48
  if ta.TYPE_CHECKING:
@@ -71,7 +70,7 @@ def trivial_s_to_a(fn):
71
70
 
72
71
  def trivial_a_to_s(fn):
73
72
  def inner(*args, **kwargs):
74
- return sync_await(fn, *args, **kwargs)
73
+ return lang.sync_await(fn, *args, **kwargs)
75
74
  return inner
76
75
 
77
76
 
omlish/lang/__init__.py CHANGED
@@ -1,3 +1,10 @@
1
+ from .asyncs import ( # noqa
2
+ async_list,
3
+
4
+ sync_await,
5
+ sync_async_list,
6
+ )
7
+
1
8
  from .attrs import ( # noqa
2
9
  AttrOps,
3
10
  AttributePresentError,
@@ -237,7 +244,7 @@ from .imports.proxyinit import ( # noqa
237
244
  auto_proxy_init,
238
245
  )
239
246
 
240
- from .imports.resolution import ( # noqa
247
+ from .imports.resolving import ( # noqa
241
248
  can_import,
242
249
  get_real_module_name,
243
250
  resolve_import_name,
@@ -273,11 +280,11 @@ from .lazyglobals import ( # noqa
273
280
  )
274
281
 
275
282
  from .maysyncs import ( # noqa
276
- MaysyncP,
283
+ MaysyncFn,
284
+ MaysyncGeneratorFn,
277
285
 
278
286
  make_maysync,
279
287
  make_maysync_from_sync,
280
-
281
288
  maysync,
282
289
  )
283
290
 
@@ -420,8 +427,11 @@ just = Maybe.just
420
427
 
421
428
  from ..lite.maysyncs import ( # noqa
422
429
  Maywaitable,
423
- Maysync,
430
+ MaysyncGenerator,
431
+
424
432
  Maysync_,
433
+ MaysyncFn_,
434
+ MaysyncGeneratorFn_,
425
435
  )
426
436
 
427
437
  from ..lite.reprs import ( # noqa
omlish/lang/asyncs.py ADDED
@@ -0,0 +1,80 @@
1
+ import typing as ta
2
+
3
+
4
+ T = ta.TypeVar('T')
5
+ P = ta.ParamSpec('P')
6
+
7
+
8
+ ##
9
+
10
+
11
+ async def async_list(
12
+ fn: ta.Callable[P, ta.AsyncIterator[T]],
13
+ *args: P.args,
14
+ **kwargs: P.kwargs,
15
+ ) -> list[T]:
16
+ """Simply eagerly reads the full contents of a function call returning an async iterator."""
17
+
18
+ return [v async for v in fn(*args, **kwargs)]
19
+
20
+
21
+ ##
22
+
23
+
24
+ def sync_await(
25
+ fn: ta.Callable[P, ta.Awaitable[T]],
26
+ *args: P.args,
27
+ **kwargs: P.kwargs,
28
+ ) -> T:
29
+ """
30
+ Allows for the synchronous execution of async functions which will never actually *externally* await anything. These
31
+ functions are allowed to await any number of other functions - including contextmanagers and generators - so long as
32
+ nothing ever actually 'leaks' out of the function, presumably to an event loop.
33
+ """
34
+
35
+ ret = missing = object()
36
+
37
+ async def gate():
38
+ nonlocal ret
39
+
40
+ ret = await fn(*args, **kwargs)
41
+
42
+ cr = gate()
43
+ try:
44
+ try:
45
+ cr.send(None)
46
+ except StopIteration:
47
+ pass
48
+
49
+ if ret is missing or cr.cr_await is not None or cr.cr_running:
50
+ raise TypeError('Not terminated')
51
+
52
+ finally:
53
+ cr.close()
54
+
55
+ return ta.cast(T, ret)
56
+
57
+
58
+ def sync_async_list(
59
+ fn: ta.Callable[P, ta.AsyncIterator[T]],
60
+ *args: P.args,
61
+ **kwargs: P.kwargs,
62
+ ) -> list[T]:
63
+ """
64
+ Uses `sync_await` to synchronously read the full contents of a function call returning an async iterator, given that
65
+ the function never externally awaits anything.
66
+ """
67
+
68
+ lst: list[T] | None = None
69
+
70
+ async def inner():
71
+ nonlocal lst
72
+
73
+ lst = [v async for v in fn(*args, **kwargs)]
74
+
75
+ sync_await(inner)
76
+
77
+ if not isinstance(lst, list):
78
+ raise TypeError(lst)
79
+
80
+ return lst
@@ -1,6 +1,6 @@
1
1
  import sys
2
2
 
3
- from .resolution import resolve_import_name
3
+ from .resolving import resolve_import_name
4
4
 
5
5
 
6
6
  ##
omlish/lang/maysyncs.py CHANGED
@@ -1,15 +1,22 @@
1
1
  import typing as ta
2
2
 
3
+ from ..lite.maysyncs import MaysyncGenerator
3
4
  from ..lite.maysyncs import Maywaitable
4
5
  from ..lite.maysyncs import make_maysync as _make_maysync
5
6
  from ..lite.maysyncs import maysync as _maysync
7
+ from ..lite.maysyncs import maysync_fn as _maysync_fn
8
+ from ..lite.maysyncs import maysync_generator_fn as _maysync_generator_fn
6
9
  from .functions import as_async
7
10
 
8
11
 
9
12
  T = ta.TypeVar('T')
13
+ O = ta.TypeVar('O')
14
+ I = ta.TypeVar('I')
15
+
10
16
  P = ta.ParamSpec('P')
11
17
 
12
- MaysyncP: ta.TypeAlias = ta.Callable[P, Maywaitable[T]]
18
+ MaysyncFn: ta.TypeAlias = ta.Callable[P, Maywaitable[T]]
19
+ MaysyncGeneratorFn: ta.TypeAlias = ta.Callable[P, MaysyncGenerator[O, I]]
13
20
 
14
21
 
15
22
  ##
@@ -18,8 +25,8 @@ MaysyncP: ta.TypeAlias = ta.Callable[P, Maywaitable[T]]
18
25
  def make_maysync(
19
26
  s: ta.Callable[P, T],
20
27
  a: ta.Callable[P, ta.Awaitable[T]],
21
- ) -> MaysyncP[P, T]:
22
- return ta.cast('MaysyncP[P, T]', _make_maysync(
28
+ ) -> MaysyncFn[P, T]:
29
+ return ta.cast('MaysyncFn[P, T]', _make_maysync(
23
30
  s,
24
31
  a,
25
32
  ))
@@ -28,8 +35,8 @@ def make_maysync(
28
35
  def make_maysync_from_sync(
29
36
  s: ta.Callable[P, T],
30
37
  a: ta.Callable[P, ta.Awaitable[T]] | None = None,
31
- ) -> MaysyncP[P, T]:
32
- return ta.cast('MaysyncP[P, T]', _make_maysync(
38
+ ) -> MaysyncFn[P, T]:
39
+ return ta.cast('MaysyncFn[P, T]', _make_maysync(
33
40
  s,
34
41
  a if a is not None else as_async(s),
35
42
  ))
@@ -38,7 +45,31 @@ def make_maysync_from_sync(
38
45
  ##
39
46
 
40
47
 
41
- def maysync(m: ta.Callable[P, ta.Awaitable[T]]) -> MaysyncP[P, T]:
42
- return ta.cast('MaysyncP[P, T]', _maysync(
43
- m,
44
- ))
48
+ def maysync_fn(
49
+ m: ta.Callable[P, ta.Awaitable[T]],
50
+ ) -> MaysyncFn[P, T]:
51
+ return ta.cast('MaysyncFn[P, T]', _maysync_fn(m))
52
+
53
+
54
+ def maysync_generator_fn(
55
+ m: ta.Callable[P, ta.AsyncGenerator[O, I]],
56
+ ) -> MaysyncGeneratorFn[P, O, I]:
57
+ return ta.cast('MaysyncGeneratorFn[P, O, I]', _maysync_generator_fn(m))
58
+
59
+
60
+ @ta.overload
61
+ def maysync(
62
+ m: ta.Callable[P, ta.Awaitable[T]],
63
+ ) -> MaysyncFn[P, T]:
64
+ ...
65
+
66
+
67
+ @ta.overload
68
+ def maysync(
69
+ m: ta.Callable[P, ta.AsyncGenerator[O, I]],
70
+ ) -> MaysyncGeneratorFn[P, O, I]:
71
+ ...
72
+
73
+
74
+ def maysync(m):
75
+ return _maysync(m)