From 099d6643bef99f0b9d3ee05a708787b3dd1f5a84 Mon Sep 17 00:00:00 2001 From: jorenham Date: Wed, 12 Feb 2025 17:49:32 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20typing=20errors=20in=20`nu?= =?UTF-8?q?mpy.=5Fcore.function=5Fbase`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/numpy-stubs/_core/function_base.pyi | 286 +++++++++++++--------- test/static/accept/array_constructors.pyi | 8 +- 2 files changed, 168 insertions(+), 126 deletions(-) diff --git a/src/numpy-stubs/_core/function_base.pyi b/src/numpy-stubs/_core/function_base.pyi index 6ca47e15..1089bc42 100644 --- a/src/numpy-stubs/_core/function_base.pyi +++ b/src/numpy-stubs/_core/function_base.pyi @@ -1,108 +1,124 @@ -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]: ... # @@ -110,41 +126,52 @@ def linspace( 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]: ... # @@ -152,38 +179,53 @@ def logspace( 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 diff --git a/test/static/accept/array_constructors.pyi b/test/static/accept/array_constructors.pyi index 7db4f6b2..c69ee099 100644 --- a/test/static/accept/array_constructors.pyi +++ b/test/static/accept/array_constructors.pyi @@ -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])