1
1
import multiprocessing .pool as mpp
2
2
import types
3
- from collections .abc import Callable , Iterable , Mapping , Sequence
4
- from typing import Concatenate , Final , Generic , NamedTuple , TypeAlias , overload
5
- from typing_extensions import TypeVar , override
3
+ from collections .abc import Callable , Iterable , Sequence
4
+ from typing import Any , Concatenate , Final , Generic , Literal , NamedTuple , TypeAlias , overload
5
+ from typing_extensions import Never , TypeVar , override
6
6
7
7
import numpy as np
8
8
import optype as op
9
9
import optype .numpy as onp
10
10
import optype .numpy .compat as npc
11
+ from numpy .random import Generator as Generator # implicit re-export
12
+ from optype .numpy .compat import DTypePromotionError as DTypePromotionError # implicit re-export
11
13
from scipy ._typing import RNG , EnterSelfMixin
12
14
13
- _AnyRNG = TypeVar ("_AnyRNG " , np .random .RandomState , np .random .Generator )
15
+ _AnyRNGT = TypeVar ("_AnyRNGT " , np .random .RandomState , np .random .Generator )
14
16
15
- _T = TypeVar ("_T" , default = object )
16
- _T_co = TypeVar ("_T_co" , covariant = True , default = object )
17
- _T_contra = TypeVar ("_T_contra" , contravariant = True , default = object )
18
17
_VT = TypeVar ("_VT" )
19
18
_RT = TypeVar ("_RT" )
19
+
20
+ _T = TypeVar ("_T" , default = Any )
21
+ _T_co = TypeVar ("_T_co" , default = Any , covariant = True )
22
+ _T_contra = TypeVar ("_T_contra" , default = Never , contravariant = True )
23
+
20
24
_AxisT = TypeVar ("_AxisT" , bound = npc .integer )
21
25
22
26
###
23
27
24
- np_long : Final [type [np .int32 | np .int64 ]] = ...
25
- np_ulong : Final [type [np .uint32 | np .uint64 ]] = ...
26
- copy_if_needed : Final [bool | None ] = ...
28
+ np_long : Final [type [np .int32 | np .int64 ]] = ... # `np.long` on `numpy>=2`, else `np.int_`
29
+ np_ulong : Final [type [np .uint32 | np .uint64 ]] = ... # `np.ulong` on `numpy>=2`, else `np.uint`
30
+ copy_if_needed : Final [Literal [ False ] | None ] = ... # `None` on `numpy>=2`, otherwise `False`
27
31
32
+ # NOTE: These aliases are implictly exported at runtime
28
33
IntNumber : TypeAlias = int | npc .integer
29
34
DecimalNumber : TypeAlias = float | npc .floating | npc .integer
30
-
31
35
_RNG : TypeAlias = np .random .Generator | np .random .RandomState
32
36
SeedType : TypeAlias = IntNumber | _RNG | None
33
- # NOTE: This is actually a exported at runtime :(
34
- GeneratorType = TypeVar ("GeneratorType" , bound = _RNG ) # noqa: PYI001
37
+ GeneratorType = TypeVar ("GeneratorType" , bound = _RNG ) # noqa: PYI001 # oof
38
+
39
+ ###
35
40
36
41
class ComplexWarning (RuntimeWarning ): ...
37
42
class VisibleDeprecationWarning (UserWarning ): ...
38
- class DTypePromotionError (TypeError ): ...
39
43
40
44
class AxisError (ValueError , IndexError ):
41
45
_msg : Final [str | None ]
42
46
axis : Final [int | None ]
43
- ndim : Final [int | None ]
44
-
47
+ ndim : Final [onp .NDim | None ]
45
48
@overload
46
49
def __init__ (self , / , axis : str , ndim : None = None , msg_prefix : None = None ) -> None : ...
47
50
@overload
48
- def __init__ (self , / , axis : int , ndim : int , msg_prefix : str | None = None ) -> None : ...
51
+ def __init__ (self , / , axis : int , ndim : onp . NDim , msg_prefix : str | None = None ) -> None : ...
49
52
50
53
class FullArgSpec (NamedTuple ):
51
- args : Sequence [str ]
54
+ args : list [str ]
52
55
varargs : str | None
53
56
varkw : str | None
54
- defaults : tuple [object , ...] | None
55
- kwonlyargs : Sequence [str ]
56
- kwonlydefaults : Mapping [str , object ] | None
57
- annotations : Mapping [str , type | object | str ]
57
+ defaults : tuple [Any , ...] | None
58
+ kwonlyargs : list [str ]
59
+ kwonlydefaults : dict [str , Any ] | None
60
+ annotations : dict [str , Any ]
58
61
59
62
class _FunctionWrapper (Generic [_T_contra , _T_co ]):
60
63
f : Callable [Concatenate [_T_contra , ...], _T_co ]
61
- args : tuple [object , ...]
62
-
64
+ args : tuple [Any , ...]
63
65
@overload
64
66
def __init__ (self , / , f : Callable [[_T_contra ], _T_co ], args : tuple [()]) -> None : ...
65
67
@overload
@@ -69,8 +71,8 @@ class _FunctionWrapper(Generic[_T_contra, _T_co]):
69
71
class MapWrapper (EnterSelfMixin ):
70
72
pool : int | mpp .Pool | None
71
73
72
- def __init__ (self , / , pool : Callable [[Callable [[_VT ], _RT ], Iterable [_VT ]], Sequence [_RT ]] | int = 1 ) -> None : ...
73
- def __call__ (self , / , func : Callable [[_VT ], _RT ], iterable : Iterable [_VT ]) -> Sequence [_RT ]: ...
74
+ def __init__ (self , / , pool : Callable [[Callable [[_VT ], _RT ], Iterable [_VT ]], Iterable [_RT ]] | int = 1 ) -> None : ...
75
+ def __call__ (self , / , func : Callable [[_VT ], _RT ], iterable : Iterable [_VT ]) -> Iterable [_RT ]: ...
74
76
def terminate (self , / ) -> None : ...
75
77
def join (self , / ) -> None : ...
76
78
def close (self , / ) -> None : ...
@@ -81,23 +83,23 @@ class _RichResult(dict[str, _T]):
81
83
def __setattr__ (self , name : str , value : _T , / ) -> None : ...
82
84
83
85
#
84
- def float_factorial (n : int ) -> float : ...
86
+ def float_factorial (n : op . CanIndex ) -> float : ... # will be `np.inf` if `n >= 171`
85
87
86
88
#
87
89
def getfullargspec_no_self (func : Callable [..., object ]) -> FullArgSpec : ...
88
90
89
91
#
90
92
@overload
91
- def check_random_state (seed : _AnyRNG ) -> _AnyRNG : ...
93
+ def check_random_state (seed : _AnyRNGT ) -> _AnyRNGT : ...
92
94
@overload
93
- def check_random_state (seed : int | npc . integer | types .ModuleType | None ) -> np .random .RandomState : ...
95
+ def check_random_state (seed : onp . ToJustInt | types .ModuleType | None ) -> np .random .RandomState : ...
94
96
95
97
#
96
98
@overload
97
99
def rng_integers (
98
100
gen : RNG | None ,
99
- low : onp .ToInt | onp . ToIntND ,
100
- high : onp .ToInt | onp . ToIntND | None = None ,
101
+ low : onp .ToInt ,
102
+ high : onp .ToInt | None = None ,
101
103
size : tuple [()] | None = None ,
102
104
dtype : onp .AnyIntegerDType = "int64" ,
103
105
endpoint : op .CanBool = False ,
@@ -114,8 +116,8 @@ def rng_integers(
114
116
115
117
#
116
118
@overload
117
- def normalize_axis_index (axis : int , ndim : int ) -> int : ...
119
+ def normalize_axis_index (axis : int , ndim : onp . NDim ) -> onp . NDim : ...
118
120
@overload
119
- def normalize_axis_index (axis : int , ndim : _AxisT ) -> _AxisT : ...
121
+ def normalize_axis_index (axis : int | _AxisT , ndim : _AxisT ) -> _AxisT : ...
120
122
@overload
121
- def normalize_axis_index (axis : _AxisT , ndim : int | _AxisT ) -> _AxisT : ...
123
+ def normalize_axis_index (axis : _AxisT , ndim : onp . NDim | _AxisT ) -> _AxisT : ...
0 commit comments