summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/__init__.pyi134
-rw-r--r--numpy/core/_add_newdocs.py62
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src2
-rw-r--r--numpy/ctypeslib.py13
-rw-r--r--numpy/lib/histograms.pyi52
-rw-r--r--numpy/lib/polynomial.pyi308
-rw-r--r--numpy/random/src/pcg64/pcg64.c3
-rw-r--r--numpy/random/tests/test_direct.py22
-rw-r--r--numpy/tests/test_ctypeslib.py17
-rw-r--r--numpy/typing/tests/data/fail/histograms.pyi13
-rw-r--r--numpy/typing/tests/data/fail/lib_polynomial.pyi29
-rw-r--r--numpy/typing/tests/data/reveal/histograms.pyi19
-rw-r--r--numpy/typing/tests/data/reveal/lib_polynomial.pyi111
13 files changed, 700 insertions, 85 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi
index 69f18fac4..560139d48 100644
--- a/numpy/__init__.pyi
+++ b/numpy/__init__.pyi
@@ -771,64 +771,6 @@ class matrix(ndarray[_ShapeType, _DType_co]):
def getH(self): ...
def getI(self): ...
-class poly1d:
- def __init__(
- self,
- c_or_r: Any,
- r: Any = ...,
- variable: Any = ...,
- ) -> None: ...
- def __call__(self, val: Any) -> Any: ...
- __hash__: Any
- @property
- def coeffs(self): ...
- @coeffs.setter
- def coeffs(self, value): ...
- @property
- def c(self): ...
- @c.setter
- def c(self, value): ...
- @property
- def coef(self): ...
- @coef.setter
- def coef(self, value): ...
- @property
- def coefficients(self): ...
- @coefficients.setter
- def coefficients(self, value): ...
- @property
- def variable(self): ...
- @property
- def order(self): ...
- @property
- def o(self): ...
- @property
- def roots(self): ...
- @property
- def r(self): ...
- def __array__(self, t=...): ...
- def __len__(self): ...
- def __neg__(self): ...
- def __pos__(self): ...
- def __mul__(self, other): ...
- def __rmul__(self, other): ...
- def __add__(self, other): ...
- def __radd__(self, other): ...
- def __pow__(self, val): ...
- def __sub__(self, other): ...
- def __rsub__(self, other): ...
- def __div__(self, other): ...
- def __truediv__(self, other): ...
- def __rdiv__(self, other): ...
- def __rtruediv__(self, other): ...
- def __eq__(self, other): ...
- def __ne__(self, other): ...
- def __getitem__(self, val): ...
- def __setitem__(self, key, val): ...
- def __iter__(self): ...
- def integ(self, m=..., k=...): ...
- def deriv(self, m=...): ...
-
# Some of these are aliases; others are wrappers with an identical signature
round = around
round_ = around
@@ -3898,3 +3840,79 @@ class vectorize:
signature: None | str = ...,
) -> None: ...
def __call__(self, *args: Any, **kwargs: Any) -> NDArray[Any]: ...
+
+class poly1d:
+ @property
+ def variable(self) -> str: ...
+ @property
+ def order(self) -> int: ...
+ @property
+ def o(self) -> int: ...
+ @property
+ def roots(self) -> NDArray[Any]: ...
+ @property
+ def r(self) -> NDArray[Any]: ...
+
+ @property
+ def coeffs(self) -> NDArray[Any]: ...
+ @coeffs.setter
+ def coeffs(self, value: NDArray[Any]) -> None: ...
+
+ @property
+ def c(self) -> NDArray[Any]: ...
+ @c.setter
+ def c(self, value: NDArray[Any]) -> None: ...
+
+ @property
+ def coef(self) -> NDArray[Any]: ...
+ @coef.setter
+ def coef(self, value: NDArray[Any]) -> None: ...
+
+ @property
+ def coefficients(self) -> NDArray[Any]: ...
+ @coefficients.setter
+ def coefficients(self, value: NDArray[Any]) -> None: ...
+
+ __hash__: None # type: ignore
+
+ @overload
+ def __array__(self, t: None = ...) -> NDArray[Any]: ...
+ @overload
+ def __array__(self, t: _DType) -> ndarray[Any, _DType]: ...
+
+ @overload
+ def __call__(self, val: _ScalarLike_co) -> Any: ...
+ @overload
+ def __call__(self, val: poly1d) -> poly1d: ...
+ @overload
+ def __call__(self, val: ArrayLike) -> NDArray[Any]: ...
+
+ def __init__(
+ self,
+ c_or_r: ArrayLike,
+ r: bool = ...,
+ variable: None | str = ...,
+ ) -> None: ...
+ def __len__(self) -> int: ...
+ def __neg__(self) -> poly1d: ...
+ def __pos__(self) -> poly1d: ...
+ def __mul__(self, other: ArrayLike) -> poly1d: ...
+ def __rmul__(self, other: ArrayLike) -> poly1d: ...
+ def __add__(self, other: ArrayLike) -> poly1d: ...
+ def __radd__(self, other: ArrayLike) -> poly1d: ...
+ def __pow__(self, val: _FloatLike_co) -> poly1d: ... # Integral floats are accepted
+ def __sub__(self, other: ArrayLike) -> poly1d: ...
+ def __rsub__(self, other: ArrayLike) -> poly1d: ...
+ def __div__(self, other: ArrayLike) -> poly1d: ...
+ def __truediv__(self, other: ArrayLike) -> poly1d: ...
+ def __rdiv__(self, other: ArrayLike) -> poly1d: ...
+ def __rtruediv__(self, other: ArrayLike) -> poly1d: ...
+ def __getitem__(self, val: int) -> Any: ...
+ def __setitem__(self, key: int, val: Any) -> None: ...
+ def __iter__(self) -> Iterator[Any]: ...
+ def deriv(self, m: SupportsInt | SupportsIndex = ...) -> poly1d: ...
+ def integ(
+ self,
+ m: SupportsInt | SupportsIndex = ...,
+ k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ...,
+ ) -> poly1d: ...
diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py
index 37f21211f..7467be80f 100644
--- a/numpy/core/_add_newdocs.py
+++ b/numpy/core/_add_newdocs.py
@@ -6113,6 +6113,68 @@ add_newdoc('numpy.core.multiarray', 'dtype', ('__class_getitem__',
"""))
+add_newdoc('numpy.core.multiarray', 'dtype', ('__ge__',
+ """
+ __ge__(value, /)
+
+ Return ``self >= value``.
+
+ Equivalent to ``np.can_cast(value, self, casting="safe")``.
+
+ See Also
+ --------
+ can_cast : Returns True if cast between data types can occur according to
+ the casting rule.
+
+ """))
+
+add_newdoc('numpy.core.multiarray', 'dtype', ('__le__',
+ """
+ __le__(value, /)
+
+ Return ``self <= value``.
+
+ Equivalent to ``np.can_cast(self, value, casting="safe")``.
+
+ See Also
+ --------
+ can_cast : Returns True if cast between data types can occur according to
+ the casting rule.
+
+ """))
+
+add_newdoc('numpy.core.multiarray', 'dtype', ('__gt__',
+ """
+ __ge__(value, /)
+
+ Return ``self > value``.
+
+ Equivalent to
+ ``self != value and np.can_cast(value, self, casting="safe")``.
+
+ See Also
+ --------
+ can_cast : Returns True if cast between data types can occur according to
+ the casting rule.
+
+ """))
+
+add_newdoc('numpy.core.multiarray', 'dtype', ('__lt__',
+ """
+ __lt__(value, /)
+
+ Return ``self < value``.
+
+ Equivalent to
+ ``self != value and np.can_cast(self, value, casting="safe")``.
+
+ See Also
+ --------
+ can_cast : Returns True if cast between data types can occur according to
+ the casting rule.
+
+ """))
+
##############################################################################
#
# Datetime-related Methods
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index c0c087056..15782a91b 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -62,7 +62,7 @@ get_dummy_stack_array(PyArrayObject *orig)
PyArrayObject_fields new_fields;
new_fields.flags = PyArray_FLAGS(orig);
/* Set to NULL so the dummy object can be distinguished from the real one */
- Py_TYPE(&new_fields) = NULL;
+ Py_SET_TYPE(&new_fields, NULL);
new_fields.base = (PyObject *)orig;
return new_fields;
}
diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py
index 8ba6f15e5..8d105a248 100644
--- a/numpy/ctypeslib.py
+++ b/numpy/ctypeslib.py
@@ -90,18 +90,23 @@ else:
def load_library(libname, loader_path):
"""
It is possible to load a library using
+
>>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP
But there are cross-platform considerations, such as library file extensions,
plus the fact Windows will just load the first library it finds with that name.
NumPy supplies the load_library function as a convenience.
+ .. versionchanged:: 1.20.0
+ Allow libname and loader_path to take any
+ :term:`python:path-like object`.
+
Parameters
----------
- libname : str
+ libname : path-like
Name of the library, which can have 'lib' as a prefix,
but without an extension.
- loader_path : str
+ loader_path : path-like
Where the library can be found.
Returns
@@ -120,6 +125,10 @@ else:
warnings.warn("All features of ctypes interface may not work "
"with ctypes < 1.0.1", stacklevel=2)
+ # Convert path-like objects into strings
+ libname = os.fsdecode(libname)
+ loader_path = os.fsdecode(loader_path)
+
ext = os.path.splitext(libname)[1]
if not ext:
# Try to load library with platform-specific name, otherwise
diff --git a/numpy/lib/histograms.pyi b/numpy/lib/histograms.pyi
index 25a33e3ae..2ceb60793 100644
--- a/numpy/lib/histograms.pyi
+++ b/numpy/lib/histograms.pyi
@@ -1,7 +1,51 @@
-from typing import List
+from typing import (
+ Literal as L,
+ List,
+ Tuple,
+ Any,
+ SupportsIndex,
+ Sequence,
+)
+
+from numpy.typing import (
+ NDArray,
+ ArrayLike,
+)
+
+_BinKind = L[
+ "stone",
+ "auto",
+ "doane",
+ "fd",
+ "rice",
+ "scott",
+ "sqrt",
+ "sturges",
+]
__all__: List[str]
-def histogram_bin_edges(a, bins=..., range=..., weights=...): ...
-def histogram(a, bins=..., range=..., normed=..., weights=..., density=...): ...
-def histogramdd(sample, bins=..., range=..., normed=..., weights=..., density=...): ...
+def histogram_bin_edges(
+ a: ArrayLike,
+ bins: _BinKind | SupportsIndex | ArrayLike = ...,
+ range: None | Tuple[float, float] = ...,
+ weights: None | ArrayLike = ...,
+) -> NDArray[Any]: ...
+
+def histogram(
+ a: ArrayLike,
+ bins: _BinKind | SupportsIndex | ArrayLike = ...,
+ range: None | Tuple[float, float] = ...,
+ normed: None = ...,
+ weights: None | ArrayLike = ...,
+ density: bool = ...,
+) -> Tuple[NDArray[Any], NDArray[Any]]: ...
+
+def histogramdd(
+ sample: ArrayLike,
+ bins: SupportsIndex | ArrayLike = ...,
+ range: Sequence[Tuple[float, float]] = ...,
+ normed: None | bool = ...,
+ weights: None | ArrayLike = ...,
+ density: None | bool = ...,
+) -> Tuple[NDArray[Any], List[NDArray[Any]]]: ...
diff --git a/numpy/lib/polynomial.pyi b/numpy/lib/polynomial.pyi
index 7d38658d0..00065f53b 100644
--- a/numpy/lib/polynomial.pyi
+++ b/numpy/lib/polynomial.pyi
@@ -1,19 +1,305 @@
-from typing import List
+from typing import (
+ Literal as L,
+ List,
+ overload,
+ Any,
+ SupportsInt,
+ SupportsIndex,
+ TypeVar,
+ Tuple,
+ NoReturn,
+)
from numpy import (
RankWarning as RankWarning,
poly1d as poly1d,
+ unsignedinteger,
+ signedinteger,
+ floating,
+ complexfloating,
+ bool_,
+ int32,
+ int64,
+ float64,
+ complex128,
+ object_,
+)
+
+from numpy.typing import (
+ NDArray,
+ ArrayLike,
+ _ArrayLikeBool_co,
+ _ArrayLikeUInt_co,
+ _ArrayLikeInt_co,
+ _ArrayLikeFloat_co,
+ _ArrayLikeComplex_co,
+ _ArrayLikeObject_co,
)
+_T = TypeVar("_T")
+
+_2Tup = Tuple[_T, _T]
+_5Tup = Tuple[
+ _T,
+ NDArray[float64],
+ NDArray[int32],
+ NDArray[float64],
+ NDArray[float64],
+]
+
__all__: List[str]
-def poly(seq_of_zeros): ...
-def roots(p): ...
-def polyint(p, m=..., k=...): ...
-def polyder(p, m=...): ...
-def polyfit(x, y, deg, rcond=..., full=..., w=..., cov=...): ...
-def polyval(p, x): ...
-def polyadd(a1, a2): ...
-def polysub(a1, a2): ...
-def polymul(a1, a2): ...
-def polydiv(u, v): ...
+def poly(seq_of_zeros: ArrayLike) -> NDArray[floating[Any]]: ...
+
+# Returns either a float or complex array depending on the input values.
+# See `np.linalg.eigvals`.
+def roots(p: ArrayLike) -> NDArray[complexfloating[Any, Any]] | NDArray[floating[Any]]: ...
+
+@overload
+def polyint(
+ p: poly1d,
+ m: SupportsInt | SupportsIndex = ...,
+ k: None | _ArrayLikeComplex_co | _ArrayLikeObject_co = ...,
+) -> poly1d: ...
+@overload
+def polyint(
+ p: _ArrayLikeFloat_co,
+ m: SupportsInt | SupportsIndex = ...,
+ k: None | _ArrayLikeFloat_co = ...,
+) -> NDArray[floating[Any]]: ...
+@overload
+def polyint(
+ p: _ArrayLikeComplex_co,
+ m: SupportsInt | SupportsIndex = ...,
+ k: None | _ArrayLikeComplex_co = ...,
+) -> NDArray[complexfloating[Any, Any]]: ...
+@overload
+def polyint(
+ p: _ArrayLikeObject_co,
+ m: SupportsInt | SupportsIndex = ...,
+ k: None | _ArrayLikeObject_co = ...,
+) -> NDArray[object_]: ...
+
+@overload
+def polyder(
+ p: poly1d,
+ m: SupportsInt | SupportsIndex = ...,
+) -> poly1d: ...
+@overload
+def polyder(
+ p: _ArrayLikeFloat_co,
+ m: SupportsInt | SupportsIndex = ...,
+) -> NDArray[floating[Any]]: ...
+@overload
+def polyder(
+ p: _ArrayLikeComplex_co,
+ m: SupportsInt | SupportsIndex = ...,
+) -> NDArray[complexfloating[Any, Any]]: ...
+@overload
+def polyder(
+ p: _ArrayLikeObject_co,
+ m: SupportsInt | SupportsIndex = ...,
+) -> NDArray[object_]: ...
+
+@overload
+def polyfit(
+ x: _ArrayLikeFloat_co,
+ y: _ArrayLikeFloat_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[False] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: L[False] = ...,
+) -> NDArray[float64]: ...
+@overload
+def polyfit(
+ x: _ArrayLikeComplex_co,
+ y: _ArrayLikeComplex_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[False] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: L[False] = ...,
+) -> NDArray[complex128]: ...
+@overload
+def polyfit(
+ x: _ArrayLikeFloat_co,
+ y: _ArrayLikeFloat_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[False] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: L[True, "unscaled"] = ...,
+) -> _2Tup[NDArray[float64]]: ...
+@overload
+def polyfit(
+ x: _ArrayLikeComplex_co,
+ y: _ArrayLikeComplex_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[False] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: L[True, "unscaled"] = ...,
+) -> _2Tup[NDArray[complex128]]: ...
+@overload
+def polyfit(
+ x: _ArrayLikeFloat_co,
+ y: _ArrayLikeFloat_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[True] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: bool | L["unscaled"] = ...,
+) -> _5Tup[NDArray[float64]]: ...
+@overload
+def polyfit(
+ x: _ArrayLikeComplex_co,
+ y: _ArrayLikeComplex_co,
+ deg: SupportsIndex | SupportsInt,
+ rcond: None | float = ...,
+ full: L[True] = ...,
+ w: None | _ArrayLikeFloat_co = ...,
+ cov: bool | L["unscaled"] = ...,
+) -> _5Tup[NDArray[complex128]]: ...
+
+@overload
+def polyval(
+ p: _ArrayLikeBool_co,
+ x: _ArrayLikeBool_co,
+) -> NDArray[int64]: ...
+@overload
+def polyval(
+ p: _ArrayLikeUInt_co,
+ x: _ArrayLikeUInt_co,
+) -> NDArray[unsignedinteger[Any]]: ...
+@overload
+def polyval(
+ p: _ArrayLikeInt_co,
+ x: _ArrayLikeInt_co,
+) -> NDArray[signedinteger[Any]]: ...
+@overload
+def polyval(
+ p: _ArrayLikeFloat_co,
+ x: _ArrayLikeFloat_co,
+) -> NDArray[floating[Any]]: ...
+@overload
+def polyval(
+ p: _ArrayLikeComplex_co,
+ x: _ArrayLikeComplex_co,
+) -> NDArray[complexfloating[Any, Any]]: ...
+@overload
+def polyval(
+ p: _ArrayLikeObject_co,
+ x: _ArrayLikeObject_co,
+) -> NDArray[object_]: ...
+
+@overload
+def polyadd(
+ a1: poly1d,
+ a2: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+) -> poly1d: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+ a2: poly1d,
+) -> poly1d: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeBool_co,
+ a2: _ArrayLikeBool_co,
+) -> NDArray[bool_]: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeUInt_co,
+ a2: _ArrayLikeUInt_co,
+) -> NDArray[unsignedinteger[Any]]: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeInt_co,
+ a2: _ArrayLikeInt_co,
+) -> NDArray[signedinteger[Any]]: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeFloat_co,
+ a2: _ArrayLikeFloat_co,
+) -> NDArray[floating[Any]]: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeComplex_co,
+ a2: _ArrayLikeComplex_co,
+) -> NDArray[complexfloating[Any, Any]]: ...
+@overload
+def polyadd(
+ a1: _ArrayLikeObject_co,
+ a2: _ArrayLikeObject_co,
+) -> NDArray[object_]: ...
+
+@overload
+def polysub(
+ a1: poly1d,
+ a2: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+) -> poly1d: ...
+@overload
+def polysub(
+ a1: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+ a2: poly1d,
+) -> poly1d: ...
+@overload
+def polysub(
+ a1: _ArrayLikeBool_co,
+ a2: _ArrayLikeBool_co,
+) -> NoReturn: ...
+@overload
+def polysub(
+ a1: _ArrayLikeUInt_co,
+ a2: _ArrayLikeUInt_co,
+) -> NDArray[unsignedinteger[Any]]: ...
+@overload
+def polysub(
+ a1: _ArrayLikeInt_co,
+ a2: _ArrayLikeInt_co,
+) -> NDArray[signedinteger[Any]]: ...
+@overload
+def polysub(
+ a1: _ArrayLikeFloat_co,
+ a2: _ArrayLikeFloat_co,
+) -> NDArray[floating[Any]]: ...
+@overload
+def polysub(
+ a1: _ArrayLikeComplex_co,
+ a2: _ArrayLikeComplex_co,
+) -> NDArray[complexfloating[Any, Any]]: ...
+@overload
+def polysub(
+ a1: _ArrayLikeObject_co,
+ a2: _ArrayLikeObject_co,
+) -> NDArray[object_]: ...
+
+# NOTE: Not an alias, but they do have the same signature (that we can reuse)
+polymul = polyadd
+
+@overload
+def polydiv(
+ u: poly1d,
+ v: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+) -> _2Tup[poly1d]: ...
+@overload
+def polydiv(
+ u: _ArrayLikeComplex_co | _ArrayLikeObject_co,
+ v: poly1d,
+) -> _2Tup[poly1d]: ...
+@overload
+def polydiv(
+ u: _ArrayLikeFloat_co,
+ v: _ArrayLikeFloat_co,
+) -> _2Tup[NDArray[floating[Any]]]: ...
+@overload
+def polydiv(
+ u: _ArrayLikeComplex_co,
+ v: _ArrayLikeComplex_co,
+) -> _2Tup[NDArray[complexfloating[Any, Any]]]: ...
+@overload
+def polydiv(
+ u: _ArrayLikeObject_co,
+ v: _ArrayLikeObject_co,
+) -> _2Tup[NDArray[Any]]: ...
diff --git a/numpy/random/src/pcg64/pcg64.c b/numpy/random/src/pcg64/pcg64.c
index c623c809b..b9be1e39d 100644
--- a/numpy/random/src/pcg64/pcg64.c
+++ b/numpy/random/src/pcg64/pcg64.c
@@ -109,8 +109,7 @@ pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, pcg128_t cur_mult,
cur_plus = pcg128_mult(pcg128_add(cur_mult, PCG_128BIT_CONSTANT(0u, 1u)),
cur_plus);
cur_mult = pcg128_mult(cur_mult, cur_mult);
- delta.low >>= 1;
- delta.low += delta.high & 1;
+ delta.low = (delta.low >> 1) | (delta.high << 63);
delta.high >>= 1;
}
return pcg128_add(pcg128_mult(acc_mult, state), acc_plus);
diff --git a/numpy/random/tests/test_direct.py b/numpy/random/tests/test_direct.py
index 29054b70b..ea1ebacb6 100644
--- a/numpy/random/tests/test_direct.py
+++ b/numpy/random/tests/test_direct.py
@@ -358,6 +358,17 @@ class TestPCG64(Base):
assert val_neg == val_pos
assert val_big == val_pos
+ def test_advange_large(self):
+ rs = Generator(self.bit_generator(38219308213743))
+ pcg = rs.bit_generator
+ state = pcg.state["state"]
+ initial_state = 287608843259529770491897792873167516365
+ assert state["state"] == initial_state
+ pcg.advance(sum(2**i for i in (96, 64, 32, 16, 8, 4, 2, 1)))
+ state = pcg.state["state"]
+ advanced_state = 135275564607035429730177404003164635391
+ assert state["state"] == advanced_state
+
class TestPCG64DXSM(Base):
@classmethod
@@ -386,6 +397,17 @@ class TestPCG64DXSM(Base):
assert val_neg == val_pos
assert val_big == val_pos
+ def test_advange_large(self):
+ rs = Generator(self.bit_generator(38219308213743))
+ pcg = rs.bit_generator
+ state = pcg.state
+ initial_state = 287608843259529770491897792873167516365
+ assert state["state"]["state"] == initial_state
+ pcg.advance(sum(2**i for i in (96, 64, 32, 16, 8, 4, 2, 1)))
+ state = pcg.state["state"]
+ advanced_state = 277778083536782149546677086420637664879
+ assert state["state"] == advanced_state
+
class TestMT19937(Base):
@classmethod
diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py
index af3730df1..1ea083700 100644
--- a/numpy/tests/test_ctypeslib.py
+++ b/numpy/tests/test_ctypeslib.py
@@ -1,6 +1,7 @@
import sys
import pytest
import weakref
+from pathlib import Path
import numpy as np
from numpy.ctypeslib import ndpointer, load_library, as_array
@@ -37,13 +38,15 @@ else:
reason="Known to fail on cygwin")
class TestLoadLibrary:
def test_basic(self):
- try:
- # Should succeed
- load_library('_multiarray_umath', np.core._multiarray_umath.__file__)
- except ImportError as e:
- msg = ("ctypes is not available on this python: skipping the test"
- " (import error was: %s)" % str(e))
- print(msg)
+ loader_path = np.core._multiarray_umath.__file__
+
+ out1 = load_library('_multiarray_umath', loader_path)
+ out2 = load_library(Path('_multiarray_umath'), loader_path)
+ out3 = load_library('_multiarray_umath', Path(loader_path))
+ out4 = load_library(b'_multiarray_umath', loader_path)
+
+ assert isinstance(out1, ctypes.CDLL)
+ assert out1 is out2 is out3 is out4
def test_basic2(self):
# Regression for #801: load_library with a full library name
diff --git a/numpy/typing/tests/data/fail/histograms.pyi b/numpy/typing/tests/data/fail/histograms.pyi
new file mode 100644
index 000000000..ad151488d
--- /dev/null
+++ b/numpy/typing/tests/data/fail/histograms.pyi
@@ -0,0 +1,13 @@
+import numpy as np
+import numpy.typing as npt
+
+AR_i8: npt.NDArray[np.int64]
+AR_f8: npt.NDArray[np.float64]
+
+np.histogram_bin_edges(AR_i8, range=(0, 1, 2)) # E: incompatible type
+
+np.histogram(AR_i8, range=(0, 1, 2)) # E: incompatible type
+np.histogram(AR_i8, normed=True) # E: incompatible type
+
+np.histogramdd(AR_i8, range=(0, 1)) # E: incompatible type
+np.histogramdd(AR_i8, range=[(0, 1, 2)]) # E: incompatible type
diff --git a/numpy/typing/tests/data/fail/lib_polynomial.pyi b/numpy/typing/tests/data/fail/lib_polynomial.pyi
new file mode 100644
index 000000000..ca02d7bde
--- /dev/null
+++ b/numpy/typing/tests/data/fail/lib_polynomial.pyi
@@ -0,0 +1,29 @@
+import numpy as np
+import numpy.typing as npt
+
+AR_f8: npt.NDArray[np.float64]
+AR_c16: npt.NDArray[np.complex128]
+AR_O: npt.NDArray[np.object_]
+AR_U: npt.NDArray[np.str_]
+
+poly_obj: np.poly1d
+
+np.polyint(AR_U) # E: incompatible type
+np.polyint(AR_f8, m=1j) # E: No overload variant
+
+np.polyder(AR_U) # E: incompatible type
+np.polyder(AR_f8, m=1j) # E: No overload variant
+
+np.polyfit(AR_O, AR_f8, 1) # E: incompatible type
+np.polyfit(AR_f8, AR_f8, 1, rcond=1j) # E: No overload variant
+np.polyfit(AR_f8, AR_f8, 1, w=AR_c16) # E: incompatible type
+np.polyfit(AR_f8, AR_f8, 1, cov="bob") # E: No overload variant
+
+np.polyval(AR_f8, AR_U) # E: incompatible type
+np.polyadd(AR_f8, AR_U) # E: incompatible type
+np.polysub(AR_f8, AR_U) # E: incompatible type
+np.polymul(AR_f8, AR_U) # E: incompatible type
+np.polydiv(AR_f8, AR_U) # E: incompatible type
+
+5**poly_obj # E: No overload variant
+hash(poly_obj)
diff --git a/numpy/typing/tests/data/reveal/histograms.pyi b/numpy/typing/tests/data/reveal/histograms.pyi
new file mode 100644
index 000000000..55fa9518f
--- /dev/null
+++ b/numpy/typing/tests/data/reveal/histograms.pyi
@@ -0,0 +1,19 @@
+import numpy as np
+import numpy.typing as npt
+
+AR_i8: npt.NDArray[np.int64]
+AR_f8: npt.NDArray[np.float64]
+
+reveal_type(np.histogram_bin_edges(AR_i8, bins="auto")) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(np.histogram_bin_edges(AR_i8, bins="rice", range=(0, 3))) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(np.histogram_bin_edges(AR_i8, bins="scott", weights=AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+
+reveal_type(np.histogram(AR_i8, bins="auto")) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]]
+reveal_type(np.histogram(AR_i8, bins="rice", range=(0, 3))) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]]
+reveal_type(np.histogram(AR_i8, bins="scott", weights=AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]]
+reveal_type(np.histogram(AR_f8, bins=1, density=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]]
+
+reveal_type(np.histogramdd(AR_i8, bins=[1])) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]]
+reveal_type(np.histogramdd(AR_i8, range=[(0, 3)])) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]]
+reveal_type(np.histogramdd(AR_i8, weights=AR_f8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]]
+reveal_type(np.histogramdd(AR_f8, density=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], builtins.list[numpy.ndarray[Any, numpy.dtype[Any]]]]
diff --git a/numpy/typing/tests/data/reveal/lib_polynomial.pyi b/numpy/typing/tests/data/reveal/lib_polynomial.pyi
new file mode 100644
index 000000000..5a4a3c424
--- /dev/null
+++ b/numpy/typing/tests/data/reveal/lib_polynomial.pyi
@@ -0,0 +1,111 @@
+import numpy as np
+import numpy.typing as npt
+
+AR_b: npt.NDArray[np.bool_]
+AR_u4: npt.NDArray[np.uint32]
+AR_i8: npt.NDArray[np.int64]
+AR_f8: npt.NDArray[np.float64]
+AR_c16: npt.NDArray[np.complex128]
+AR_O: npt.NDArray[np.object_]
+
+poly_obj: np.poly1d
+
+reveal_type(poly_obj.variable) # E: str
+reveal_type(poly_obj.order) # E: int
+reveal_type(poly_obj.o) # E: int
+reveal_type(poly_obj.roots) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.r) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.coeffs) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.c) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.coef) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.coefficients) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj.__hash__) # E: None
+
+reveal_type(poly_obj(1)) # E: Any
+reveal_type(poly_obj([1])) # E: numpy.ndarray[Any, numpy.dtype[Any]]
+reveal_type(poly_obj(poly_obj)) # E: numpy.poly1d
+
+reveal_type(len(poly_obj)) # E: int
+reveal_type(-poly_obj) # E: numpy.poly1d
+reveal_type(+poly_obj) # E: numpy.poly1d
+
+reveal_type(poly_obj * 5) # E: numpy.poly1d
+reveal_type(5 * poly_obj) # E: numpy.poly1d
+reveal_type(poly_obj + 5) # E: numpy.poly1d
+reveal_type(5 + poly_obj) # E: numpy.poly1d
+reveal_type(poly_obj - 5) # E: numpy.poly1d
+reveal_type(5 - poly_obj) # E: numpy.poly1d
+reveal_type(poly_obj**1) # E: numpy.poly1d
+reveal_type(poly_obj**1.0) # E: numpy.poly1d
+reveal_type(poly_obj / 5) # E: numpy.poly1d
+reveal_type(5 / poly_obj) # E: numpy.poly1d
+
+reveal_type(poly_obj[0]) # E: Any
+poly_obj[0] = 5
+reveal_type(iter(poly_obj)) # E: Iterator[Any]
+reveal_type(poly_obj.deriv()) # E: numpy.poly1d
+reveal_type(poly_obj.integ()) # E: numpy.poly1d
+
+reveal_type(np.poly(poly_obj)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.poly(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.poly(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+
+reveal_type(np.polyint(poly_obj)) # E: numpy.poly1d
+reveal_type(np.polyint(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polyint(AR_f8, k=AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polyint(AR_O, m=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polyder(poly_obj)) # E: numpy.poly1d
+reveal_type(np.polyder(AR_f8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polyder(AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polyder(AR_O, m=2)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polyfit(AR_f8, AR_f8, 2)) # E: numpy.ndarray[Any, numpy.dtype[{float64}]]
+reveal_type(np.polyfit(AR_f8, AR_i8, 1, full=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]]
+reveal_type(np.polyfit(AR_u4, AR_f8, 1.0, cov="unscaled")) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]]
+reveal_type(np.polyfit(AR_c16, AR_f8, 2)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]]
+reveal_type(np.polyfit(AR_f8, AR_c16, 1, full=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{complex128}]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[numpy.typing._32Bit]]], numpy.ndarray[Any, numpy.dtype[{float64}]], numpy.ndarray[Any, numpy.dtype[{float64}]]]
+reveal_type(np.polyfit(AR_u4, AR_c16, 1.0, cov=True)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[{complex128}]], numpy.ndarray[Any, numpy.dtype[{complex128}]]]
+
+reveal_type(np.polyval(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[{int64}]]
+reveal_type(np.polyval(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]]
+reveal_type(np.polyval(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]
+reveal_type(np.polyval(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polyval(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polyval(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polyadd(poly_obj, AR_i8)) # E: numpy.poly1d
+reveal_type(np.polyadd(AR_f8, poly_obj)) # E: numpy.poly1d
+reveal_type(np.polyadd(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]]
+reveal_type(np.polyadd(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]]
+reveal_type(np.polyadd(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]
+reveal_type(np.polyadd(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polyadd(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polyadd(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polysub(poly_obj, AR_i8)) # E: numpy.poly1d
+reveal_type(np.polysub(AR_f8, poly_obj)) # E: numpy.poly1d
+reveal_type(np.polysub(AR_b, AR_b)) # E: <nothing>
+reveal_type(np.polysub(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]]
+reveal_type(np.polysub(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]
+reveal_type(np.polysub(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polysub(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polysub(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polymul(poly_obj, AR_i8)) # E: numpy.poly1d
+reveal_type(np.polymul(AR_f8, poly_obj)) # E: numpy.poly1d
+reveal_type(np.polymul(AR_b, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bool_]]
+reveal_type(np.polymul(AR_u4, AR_b)) # E: numpy.ndarray[Any, numpy.dtype[numpy.unsignedinteger[Any]]]
+reveal_type(np.polymul(AR_i8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.signedinteger[Any]]]
+reveal_type(np.polymul(AR_f8, AR_i8)) # E: numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]
+reveal_type(np.polymul(AR_i8, AR_c16)) # E: numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]
+reveal_type(np.polymul(AR_O, AR_O)) # E: numpy.ndarray[Any, numpy.dtype[numpy.object_]]
+
+reveal_type(np.polydiv(poly_obj, AR_i8)) # E: numpy.poly1d
+reveal_type(np.polydiv(AR_f8, poly_obj)) # E: numpy.poly1d
+reveal_type(np.polydiv(AR_b, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]]
+reveal_type(np.polydiv(AR_u4, AR_b)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]]
+reveal_type(np.polydiv(AR_i8, AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]]
+reveal_type(np.polydiv(AR_f8, AR_i8)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]], numpy.ndarray[Any, numpy.dtype[numpy.floating[Any]]]]
+reveal_type(np.polydiv(AR_i8, AR_c16)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]], numpy.ndarray[Any, numpy.dtype[numpy.complexfloating[Any, Any]]]]
+reveal_type(np.polydiv(AR_O, AR_O)) # E: Tuple[numpy.ndarray[Any, numpy.dtype[Any]], numpy.ndarray[Any, numpy.dtype[Any]]]