1- from typing import (
2- Any ,
3- AsyncIterable ,
4- AsyncIterator ,
5- Awaitable ,
6- Callable ,
7- ClassVar ,
8- Generator ,
9- TypeAlias ,
10- )
11-
12- from . _sonyflake import (
13- SONYFLAKE_EPOCH ,
14- SONYFLAKE_MACHINE_ID_BITS ,
15- SONYFLAKE_MACHINE_ID_MAX ,
16- SONYFLAKE_MACHINE_ID_OFFSET ,
17- SONYFLAKE_SEQUENCE_BITS ,
18- SONYFLAKE_SEQUENCE_MAX ,
19- SONYFLAKE_TIME_OFFSET ,
20- MachineIDLCG ,
21- SonyFlake ,
22- )
23-
24- AsyncSleep : TypeAlias = Callable [[ float ], Awaitable [ None ]]
1+ try :
2+ from . _sonyflake import (
3+ SONYFLAKE_EPOCH ,
4+ SONYFLAKE_MACHINE_ID_BITS ,
5+ SONYFLAKE_MACHINE_ID_MAX ,
6+ SONYFLAKE_MACHINE_ID_OFFSET ,
7+ SONYFLAKE_SEQUENCE_BITS ,
8+ SONYFLAKE_SEQUENCE_MAX ,
9+ SONYFLAKE_TIME_OFFSET ,
10+ MachineIDLCG ,
11+ SonyFlake ,
12+ )
13+ except ImportError : # pragma: no cover
14+ from . pure import (
15+ SONYFLAKE_EPOCH ,
16+ SONYFLAKE_MACHINE_ID_BITS ,
17+ SONYFLAKE_MACHINE_ID_MAX ,
18+ SONYFLAKE_MACHINE_ID_OFFSET ,
19+ SONYFLAKE_SEQUENCE_BITS ,
20+ SONYFLAKE_SEQUENCE_MAX ,
21+ SONYFLAKE_TIME_OFFSET ,
22+ MachineIDLCG ,
23+ SonyFlake ,
24+ )
2525
2626__all__ = [
2727 "SONYFLAKE_EPOCH" ,
3737]
3838
3939
40- class AsyncSonyFlake ( Awaitable [ int ], AsyncIterable [ int ]) :
40+ class AsyncSonyFlake :
4141 """Async wrapper for :class:`SonyFlake`.
4242
4343 Usage:
@@ -57,10 +57,9 @@ class AsyncSonyFlake(Awaitable[int], AsyncIterable[int]):
5757 break
5858 """
5959
60- __slots__ : ClassVar [tuple [str , ...]] = ("sf" , "sleep" )
61- sleep : AsyncSleep
60+ __slots__ = ("sf" , "sleep" )
6261
63- def __init__ (self , sf : SonyFlake , sleep : AsyncSleep | None = None ) -> None :
62+ def __init__ (self , sf : SonyFlake , sleep = None ):
6463 """Initialize AsyncSonyFlake ID generator.
6564
6665 Args:
@@ -76,7 +75,7 @@ def __init__(self, sf: SonyFlake, sleep: AsyncSleep | None = None) -> None:
7675 self .sf = sf
7776 self .sleep = sleep
7877
79- async def __call__ (self , n : int ) -> list [ int ] :
78+ async def __call__ (self , n ) :
8079 """Generate multiple SonyFlake IDs at once.
8180
8281 Roughly equivalent to ``[await asf for _ in range(n)]``, but more
@@ -96,7 +95,7 @@ async def __call__(self, n: int) -> list[int]:
9695
9796 return ids
9897
99- def __await__ (self ) -> Generator [ Any , Any , int ] :
98+ def __await__ (self ):
10099 """Produce a SonyFlake ID."""
101100
102101 id_ , to_sleep = self .sf ._raw (None )
@@ -105,12 +104,12 @@ def __await__(self) -> Generator[Any, Any, int]:
105104
106105 return id_
107106
108- def __aiter__ (self ) -> AsyncIterator [ int ] :
107+ def __aiter__ (self ):
109108 """Return an infinite SonyFlake ID async iterator."""
110109
111110 return self ._gen ().__aiter__ ()
112111
113- async def _gen (self ) -> AsyncIterator [ int ] :
112+ async def _gen (self ):
114113 """Infinite SonyFlake ID async generator."""
115114
116115 while True :
0 commit comments