|
1 | 1 | import abc
|
2 |
| -from threading import Lock |
3 | 2 | from collections.abc import Callable, Mapping, Sequence
|
4 |
| -from typing import ( |
5 |
| - Any, |
6 |
| - NamedTuple, |
7 |
| - TypeAlias, |
8 |
| - TypedDict, |
9 |
| - TypeVar, |
10 |
| - overload, |
11 |
| - Literal, |
12 |
| - type_check_only, |
13 |
| -) |
14 |
| - |
15 |
| -from numpy import dtype, uint32, uint64 |
16 |
| -from numpy._typing import ( |
17 |
| - NDArray, |
18 |
| - _ArrayLikeInt_co, |
19 |
| - _ShapeLike, |
20 |
| - _SupportsDType, |
21 |
| - _UInt32Codes, |
22 |
| - _UInt64Codes, |
23 |
| -) |
24 |
| - |
25 |
| -_T = TypeVar("_T") |
26 |
| - |
27 |
| -_DTypeLikeUint32: TypeAlias = ( |
28 |
| - dtype[uint32] |
29 |
| - | _SupportsDType[dtype[uint32]] |
30 |
| - | type[uint32] |
31 |
| - | _UInt32Codes |
32 |
| -) |
33 |
| -_DTypeLikeUint64: TypeAlias = ( |
34 |
| - dtype[uint64] |
35 |
| - | _SupportsDType[dtype[uint64]] |
36 |
| - | type[uint64] |
37 |
| - | _UInt64Codes |
38 |
| -) |
| 3 | +from threading import Lock |
| 4 | +from typing import Any, ClassVar, Literal, NamedTuple, TypeAlias, TypedDict, overload, type_check_only |
| 5 | + |
| 6 | +from _typeshed import Incomplete |
| 7 | +from typing_extensions import CapsuleType, Self |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +from numpy._typing import NDArray, _ArrayLikeInt_co, _DTypeLike, _ShapeLike, _UInt32Codes, _UInt64Codes |
| 11 | + |
| 12 | +__all__ = ["BitGenerator", "SeedSequence"] |
| 13 | + |
| 14 | +### |
| 15 | + |
| 16 | +_DTypeLikeUint_: TypeAlias = _DTypeLike[np.uint32 | np.uint64] | _UInt32Codes | _UInt64Codes |
39 | 17 |
|
40 | 18 | @type_check_only
|
41 | 19 | class _SeedSeqState(TypedDict):
|
42 |
| - entropy: None | int | Sequence[int] |
| 20 | + entropy: int | Sequence[int] | None |
43 | 21 | spawn_key: tuple[int, ...]
|
44 | 22 | pool_size: int
|
45 | 23 | n_children_spawned: int
|
46 | 24 |
|
47 | 25 | @type_check_only
|
48 | 26 | class _Interface(NamedTuple):
|
49 |
| - state_address: Any |
50 |
| - state: Any |
51 |
| - next_uint64: Any |
52 |
| - next_uint32: Any |
53 |
| - next_double: Any |
54 |
| - bit_generator: Any |
| 27 | + state_address: Incomplete |
| 28 | + state: Incomplete |
| 29 | + next_uint64: Incomplete |
| 30 | + next_uint32: Incomplete |
| 31 | + next_double: Incomplete |
| 32 | + bit_generator: Incomplete |
| 33 | + |
| 34 | +@type_check_only |
| 35 | +class _CythonMixin: |
| 36 | + def __setstate_cython__(self, pyx_state: object, /) -> None: ... |
| 37 | + def __reduce_cython__(self) -> Any: ... # noqa: ANN401 |
| 38 | + |
| 39 | +@type_check_only |
| 40 | +class _GenerateStateMixin(_CythonMixin): |
| 41 | + def generate_state(self, /, n_words: int, dtype: _DTypeLikeUint_ = ...) -> NDArray[np.uint32 | np.uint64]: ... |
| 42 | + |
| 43 | +### |
55 | 44 |
|
56 | 45 | class ISeedSequence(abc.ABC):
|
57 | 46 | @abc.abstractmethod
|
58 |
| - def generate_state( |
59 |
| - self, n_words: int, dtype: _DTypeLikeUint32 | _DTypeLikeUint64 = ... |
60 |
| - ) -> NDArray[uint32 | uint64]: ... |
| 47 | + def generate_state(self, /, n_words: int, dtype: _DTypeLikeUint_ = ...) -> NDArray[np.uint32 | np.uint64]: ... |
61 | 48 |
|
62 |
| -class ISpawnableSeedSequence(ISeedSequence): |
| 49 | +class ISpawnableSeedSequence(ISeedSequence, abc.ABC): |
63 | 50 | @abc.abstractmethod
|
64 |
| - def spawn(self: _T, n_children: int) -> list[_T]: ... |
| 51 | + def spawn(self, /, n_children: int) -> list[Self]: ... |
| 52 | + |
| 53 | +class SeedlessSeedSequence(_GenerateStateMixin, ISpawnableSeedSequence): |
| 54 | + def spawn(self, /, n_children: int) -> list[Self]: ... |
65 | 55 |
|
66 |
| -class SeedlessSeedSequence(ISpawnableSeedSequence): |
67 |
| - def generate_state( |
68 |
| - self, n_words: int, dtype: _DTypeLikeUint32 | _DTypeLikeUint64 = ... |
69 |
| - ) -> NDArray[uint32 | uint64]: ... |
70 |
| - def spawn(self: _T, n_children: int) -> list[_T]: ... |
| 56 | +class SeedSequence(_GenerateStateMixin, ISpawnableSeedSequence): |
| 57 | + __pyx_vtable__: ClassVar[CapsuleType] = ... |
71 | 58 |
|
72 |
| -class SeedSequence(ISpawnableSeedSequence): |
73 |
| - entropy: None | int | Sequence[int] |
| 59 | + entropy: int | Sequence[int] | None |
74 | 60 | spawn_key: tuple[int, ...]
|
75 | 61 | pool_size: int
|
76 | 62 | n_children_spawned: int
|
77 |
| - pool: NDArray[uint32] |
| 63 | + pool: NDArray[np.uint32] |
| 64 | + |
78 | 65 | def __init__(
|
79 | 66 | self,
|
80 |
| - entropy: None | int | Sequence[int] | _ArrayLikeInt_co = ..., |
| 67 | + /, |
| 68 | + entropy: _ArrayLikeInt_co | None = None, |
81 | 69 | *,
|
82 |
| - spawn_key: Sequence[int] = ..., |
83 |
| - pool_size: int = ..., |
| 70 | + spawn_key: Sequence[int] = (), |
| 71 | + pool_size: int = 4, |
84 | 72 | n_children_spawned: int = ...,
|
85 | 73 | ) -> None: ...
|
86 |
| - def __repr__(self) -> str: ... |
| 74 | + def spawn(self, /, n_children: int) -> list[Self]: ... |
87 | 75 | @property
|
88 |
| - def state( |
89 |
| - self, |
90 |
| - ) -> _SeedSeqState: ... |
91 |
| - def generate_state( |
92 |
| - self, n_words: int, dtype: _DTypeLikeUint32 | _DTypeLikeUint64 = ... |
93 |
| - ) -> NDArray[uint32 | uint64]: ... |
94 |
| - def spawn(self, n_children: int) -> list[SeedSequence]: ... |
| 76 | + def state(self) -> _SeedSeqState: ... |
95 | 77 |
|
96 |
| -class BitGenerator(abc.ABC): |
| 78 | +class BitGenerator(_CythonMixin, abc.ABC): |
97 | 79 | lock: Lock
|
98 |
| - def __init__(self, seed: None | _ArrayLikeInt_co | SeedSequence = ...) -> None: ... |
99 |
| - def __getstate__(self) -> tuple[dict[str, Any], ISeedSequence]: ... |
100 |
| - def __setstate__( |
101 |
| - self, state_seed_seq: dict[str, Any] | tuple[dict[str, Any], ISeedSequence] |
102 |
| - ) -> None: ... |
103 |
| - def __reduce__( |
104 |
| - self, |
105 |
| - ) -> tuple[ |
106 |
| - Callable[[str], BitGenerator], |
107 |
| - tuple[str], |
108 |
| - tuple[dict[str, Any], ISeedSequence] |
109 |
| - ]: ... |
110 |
| - @abc.abstractmethod |
111 | 80 | @property
|
112 | 81 | def state(self) -> Mapping[str, Any]: ...
|
113 | 82 | @state.setter
|
114 |
| - def state(self, value: Mapping[str, Any]) -> None: ... |
| 83 | + def state(self, value: Mapping[str, Any], /) -> None: ... |
115 | 84 | @property
|
116 | 85 | def seed_seq(self) -> ISeedSequence: ...
|
117 |
| - def spawn(self, n_children: int) -> list[BitGenerator]: ... |
118 |
| - @overload |
119 |
| - def random_raw(self, size: None = ..., output: Literal[True] = ...) -> int: ... # type: ignore[misc] |
120 |
| - @overload |
121 |
| - def random_raw(self, size: _ShapeLike = ..., output: Literal[True] = ...) -> NDArray[uint64]: ... # type: ignore[misc] |
122 |
| - @overload |
123 |
| - def random_raw(self, size: None | _ShapeLike = ..., output: Literal[False] = ...) -> None: ... # type: ignore[misc] |
124 |
| - def _benchmark(self, cnt: int, method: str = ...) -> None: ... |
125 | 86 | @property
|
126 | 87 | def ctypes(self) -> _Interface: ...
|
127 | 88 | @property
|
128 | 89 | def cffi(self) -> _Interface: ...
|
| 90 | + @property |
| 91 | + def capsule(self) -> CapsuleType: ... |
| 92 | + |
| 93 | + # |
| 94 | + def __init__(self, /, seed: _ArrayLikeInt_co | SeedSequence | None = None) -> None: ... |
| 95 | + def __reduce__(self) -> tuple[Callable[[str], Self], tuple[str], tuple[Mapping[str, Any], ISeedSequence]]: ... |
| 96 | + def spawn(self, /, n_children: int) -> list[Self]: ... |
| 97 | + def _benchmark(self, /, cnt: int, method: str = "uint64") -> None: ... |
| 98 | + |
| 99 | + # |
| 100 | + @overload |
| 101 | + def random_raw(self, /, size: None = None, output: Literal[True] = True) -> int: ... |
| 102 | + @overload |
| 103 | + def random_raw(self, /, size: _ShapeLike, output: Literal[True] = True) -> NDArray[np.uint64]: ... |
| 104 | + @overload |
| 105 | + def random_raw(self, /, size: _ShapeLike | None, output: Literal[False]) -> None: ... |
| 106 | + @overload |
| 107 | + def random_raw(self, /, size: _ShapeLike | None = None, *, output: Literal[False]) -> None: ... |
0 commit comments