Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 164 additions & 122 deletions src/numpy-stubs/_core/function_base.pyi
Original file line number Diff line number Diff line change
@@ -1,189 +1,231 @@
from typing import Any, Literal as L, SupportsIndex, overload
from typing_extensions import TypeVar
from typing import Any, Literal as L, SupportsIndex, TypedDict, overload, type_check_only
from typing_extensions import TypeVar, Unpack

import numpy as np
from numpy._typing import DTypeLike, NDArray, _ArrayLikeComplex_co, _ArrayLikeFloat_co, _DTypeLike
from numpy._typing import DTypeLike, NDArray, _ArrayLikeFloat_co, _ArrayLikeNumber_co, _DTypeLike

__all__ = ["geomspace", "linspace", "logspace"]

_SCT = TypeVar("_SCT", bound=np.generic)
###

_ScalarT = TypeVar("_ScalarT", bound=np.generic)

@type_check_only
class _DeviceKwargs(TypedDict, total=False):
device: L["cpu"] | None

###

@overload
def linspace(
start: _ArrayLikeFloat_co,
stop: _ArrayLikeFloat_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[False] = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
*,
device: L["cpu"] | None = ...,
num: SupportsIndex = 50,
endpoint: bool = True,
retstep: L[False] = False,
dtype: None = None,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> NDArray[np.floating]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[False] = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
*,
device: L["cpu"] | None = ...,
) -> NDArray[np.complexfloating]: ...
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
retstep: L[False] = False,
dtype: None = None,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> NDArray[np.inexact]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[False] = ...,
dtype: _DTypeLike[_SCT] = ...,
axis: SupportsIndex = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex,
endpoint: bool,
retstep: L[False],
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> NDArray[_ScalarT]: ...
@overload
def linspace(
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
retstep: L[False] = False,
*,
device: L["cpu"] | None = ...,
) -> NDArray[_SCT]: ...
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> NDArray[_ScalarT]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[False] = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
retstep: L[False] = False,
dtype: DTypeLike = ...,
axis: SupportsIndex = ...,
*,
device: L["cpu"] | None = ...,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> NDArray[Any]: ...
@overload
def linspace(
start: _ArrayLikeFloat_co,
stop: _ArrayLikeFloat_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[True] = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
num: SupportsIndex = 50,
endpoint: bool = True,
*,
device: L["cpu"] | None = ...,
retstep: L[True],
dtype: None = None,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> tuple[NDArray[np.floating], np.floating]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[True] = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
*,
device: L["cpu"] | None = ...,
) -> tuple[NDArray[np.complexfloating], np.complexfloating]: ...
retstep: L[True],
dtype: None = None,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> tuple[NDArray[np.inexact], np.inexact]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[True] = ...,
dtype: _DTypeLike[_SCT] = ...,
axis: SupportsIndex = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
*,
device: L["cpu"] | None = ...,
) -> tuple[NDArray[_SCT], _SCT]: ...
retstep: L[True],
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> tuple[NDArray[_ScalarT], _ScalarT]: ...
@overload
def linspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
retstep: L[True] = ...,
dtype: DTypeLike = ...,
axis: SupportsIndex = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
*,
device: L["cpu"] | None = ...,
retstep: L[True],
dtype: DTypeLike = ...,
axis: SupportsIndex = 0,
**kwargs: Unpack[_DeviceKwargs],
) -> tuple[NDArray[Any], Any]: ...

#
@overload
def logspace(
start: _ArrayLikeFloat_co,
stop: _ArrayLikeFloat_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
base: _ArrayLikeFloat_co = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
num: SupportsIndex = 50,
endpoint: bool = True,
base: _ArrayLikeFloat_co = 10.0,
dtype: None = None,
axis: SupportsIndex = 0,
) -> NDArray[np.floating]: ...
@overload
def logspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
base: _ArrayLikeComplex_co = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
) -> NDArray[np.complexfloating]: ...
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
base: _ArrayLikeNumber_co = 10.0,
dtype: None = None,
axis: SupportsIndex = 0,
) -> NDArray[np.inexact]: ...
@overload
def logspace(
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex,
endpoint: bool,
base: _ArrayLikeNumber_co,
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
) -> NDArray[_ScalarT]: ...
@overload
def logspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
base: _ArrayLikeComplex_co = ...,
dtype: _DTypeLike[_SCT] = ...,
axis: SupportsIndex = ...,
) -> NDArray[_SCT]: ...
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
base: _ArrayLikeNumber_co = 10.0,
*,
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
) -> NDArray[_ScalarT]: ...
@overload
def logspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
base: _ArrayLikeComplex_co = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
base: _ArrayLikeNumber_co = 10.0,
dtype: DTypeLike = ...,
axis: SupportsIndex = ...,
axis: SupportsIndex = 0,
) -> NDArray[Any]: ...

#
@overload
def geomspace(
start: _ArrayLikeFloat_co,
stop: _ArrayLikeFloat_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
num: SupportsIndex = 50,
endpoint: bool = True,
dtype: None = None,
axis: SupportsIndex = 0,
) -> NDArray[np.floating]: ...
@overload
def geomspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
dtype: None = ...,
axis: SupportsIndex = ...,
) -> NDArray[np.complexfloating]: ...
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
dtype: None = None,
axis: SupportsIndex = 0,
) -> NDArray[np.inexact]: ...
@overload
def geomspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
dtype: _DTypeLike[_SCT] = ...,
axis: SupportsIndex = ...,
) -> NDArray[_SCT]: ...
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex,
endpoint: bool,
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
) -> NDArray[_ScalarT]: ...
@overload
def geomspace(
start: _ArrayLikeComplex_co,
stop: _ArrayLikeComplex_co,
num: SupportsIndex = ...,
endpoint: bool = ...,
dtype: DTypeLike = ...,
axis: SupportsIndex = ...,
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
*,
dtype: _DTypeLike[_ScalarT],
axis: SupportsIndex = 0,
) -> NDArray[_ScalarT]: ...
@overload
def geomspace(
start: _ArrayLikeNumber_co,
stop: _ArrayLikeNumber_co,
num: SupportsIndex = 50,
endpoint: bool = True,
dtype: DTypeLike | None = None,
axis: SupportsIndex = 0,
) -> NDArray[Any]: ...

#
def add_newdoc(place: str, obj: str, doc: str | tuple[str, str] | list[tuple[str, str]], warn_on_python: bool = ...) -> None: ...
def add_newdoc(
place: str,
obj: str | None,
doc: str | tuple[str, str] | list[tuple[str, str]],
warn_on_python: bool = True,
) -> None: ... # undocumented
8 changes: 4 additions & 4 deletions test/static/accept/array_constructors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,21 @@ assert_type(np.require(B, requirements="A"), MyArray[np.float64])
assert_type(np.require(C), npt.NDArray[Any])

assert_type(np.linspace(0, 10), npt.NDArray[np.floating])
assert_type(np.linspace(0, 10j), npt.NDArray[np.complexfloating])
assert_type(np.linspace(0, 10j), npt.NDArray[np.inexact])
assert_type(np.linspace(0, 10, dtype=np.int64), npt.NDArray[np.int64])
assert_type(np.linspace(0, 10, dtype=int), npt.NDArray[Any])
assert_type(np.linspace(0, 10, retstep=True), tuple[npt.NDArray[np.floating], np.floating])
assert_type(np.linspace(0j, 10, retstep=True), tuple[npt.NDArray[np.complexfloating], np.complexfloating])
assert_type(np.linspace(0j, 10, retstep=True), tuple[npt.NDArray[np.inexact], np.inexact])
assert_type(np.linspace(0, 10, retstep=True, dtype=np.int64), tuple[npt.NDArray[np.int64], np.int64])
assert_type(np.linspace(0j, 10, retstep=True, dtype=int), tuple[npt.NDArray[Any], Any])

assert_type(np.logspace(0, 10), npt.NDArray[np.floating])
assert_type(np.logspace(0, 10j), npt.NDArray[np.complexfloating])
assert_type(np.logspace(0, 10j), npt.NDArray[np.inexact])
assert_type(np.logspace(0, 10, dtype=np.int64), npt.NDArray[np.int64])
assert_type(np.logspace(0, 10, dtype=int), npt.NDArray[Any])

assert_type(np.geomspace(0, 10), npt.NDArray[np.floating])
assert_type(np.geomspace(0, 10j), npt.NDArray[np.complexfloating])
assert_type(np.geomspace(0, 10j), npt.NDArray[np.inexact])
assert_type(np.geomspace(0, 10, dtype=np.int64), npt.NDArray[np.int64])
assert_type(np.geomspace(0, 10, dtype=int), npt.NDArray[Any])

Expand Down