Skip to content

Commit 2ff0b88

Browse files
jorenhamcharris
authored andcommitted
TYP: fix stubtest errors in numpy.random (numpy#28538)
Ported from numpy/numtype#251
1 parent ecf97ae commit 2ff0b88

File tree

1 file changed

+69
-90
lines changed

1 file changed

+69
-90
lines changed

numpy/random/bit_generator.pyi

Lines changed: 69 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,107 @@
11
import abc
2-
from threading import Lock
32
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
3917

4018
@type_check_only
4119
class _SeedSeqState(TypedDict):
42-
entropy: None | int | Sequence[int]
20+
entropy: int | Sequence[int] | None
4321
spawn_key: tuple[int, ...]
4422
pool_size: int
4523
n_children_spawned: int
4624

4725
@type_check_only
4826
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+
###
5544

5645
class ISeedSequence(abc.ABC):
5746
@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]: ...
6148

62-
class ISpawnableSeedSequence(ISeedSequence):
49+
class ISpawnableSeedSequence(ISeedSequence, abc.ABC):
6350
@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]: ...
6555

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] = ...
7158

72-
class SeedSequence(ISpawnableSeedSequence):
73-
entropy: None | int | Sequence[int]
59+
entropy: int | Sequence[int] | None
7460
spawn_key: tuple[int, ...]
7561
pool_size: int
7662
n_children_spawned: int
77-
pool: NDArray[uint32]
63+
pool: NDArray[np.uint32]
64+
7865
def __init__(
7966
self,
80-
entropy: None | int | Sequence[int] | _ArrayLikeInt_co = ...,
67+
/,
68+
entropy: _ArrayLikeInt_co | None = None,
8169
*,
82-
spawn_key: Sequence[int] = ...,
83-
pool_size: int = ...,
70+
spawn_key: Sequence[int] = (),
71+
pool_size: int = 4,
8472
n_children_spawned: int = ...,
8573
) -> None: ...
86-
def __repr__(self) -> str: ...
74+
def spawn(self, /, n_children: int) -> list[Self]: ...
8775
@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: ...
9577

96-
class BitGenerator(abc.ABC):
78+
class BitGenerator(_CythonMixin, abc.ABC):
9779
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
11180
@property
11281
def state(self) -> Mapping[str, Any]: ...
11382
@state.setter
114-
def state(self, value: Mapping[str, Any]) -> None: ...
83+
def state(self, value: Mapping[str, Any], /) -> None: ...
11584
@property
11685
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: ...
12586
@property
12687
def ctypes(self) -> _Interface: ...
12788
@property
12889
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy