diff --git a/src/numpy-stubs/_core/arrayprint.pyi b/src/numpy-stubs/_core/arrayprint.pyi index b9449ad0..e2d1d3fe 100644 --- a/src/numpy-stubs/_core/arrayprint.pyi +++ b/src/numpy-stubs/_core/arrayprint.pyi @@ -1,14 +1,30 @@ from collections.abc import Callable from contextlib import _GeneratorContextManager -from typing import Any, Literal, SupportsIndex, TypeAlias, TypedDict, type_check_only +from typing import Any, Final, Literal, SupportsIndex, TypeAlias, TypedDict, overload, type_check_only +from typing_extensions import deprecated import numpy as np from numpy._typing import NDArray, _CharLike_co, _FloatLike_co +__all__ = [ + "array2string", + "array_repr", + "array_str", + "format_float_positional", + "format_float_scientific", + "get_printoptions", + "printoptions", + "set_printoptions", +] + +### + _FloatMode: TypeAlias = Literal["fixed", "unique", "maxprec", "maxprec_equal"] _Sign: TypeAlias = Literal["-", "+", " "] -_Legacy: TypeAlias = Literal[False, "1.13", "1.21"] _Trim: TypeAlias = Literal["k", ".", "0", "-"] +_Legacy: TypeAlias = Literal["1.13", "1.21", "1.25", "2.1", False] +_LegacyNoStyle: TypeAlias = Literal["1.21", "1.25", "2.1", False] +_ReprFunc: TypeAlias = Callable[[NDArray[Any]], str] @type_check_only class _FormatDict(TypedDict, total=False): @@ -43,85 +59,177 @@ class _FormatOptions(TypedDict): floatmode: _FloatMode legacy: _Legacy +### + +__docformat__: Final = "restructuredtext" # undocumented + +# public numpy export +def get_printoptions() -> _FormatOptions: ... + +# public numpy export def set_printoptions( - precision: SupportsIndex | None = ..., - threshold: int | None = ..., - edgeitems: int | None = ..., - linewidth: int | None = ..., - suppress: bool | None = ..., - nanstr: str | None = ..., - infstr: str | None = ..., - formatter: _FormatDict | None = ..., - sign: _Sign | None = ..., - floatmode: _FloatMode | None = ..., + precision: SupportsIndex | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + linewidth: int | None = None, + suppress: bool | None = None, + nanstr: str | None = None, + infstr: str | None = None, + formatter: _FormatDict | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, *, - legacy: _Legacy | None = ..., - override_repr: Callable[[NDArray[Any]], str] | None = ..., + legacy: _Legacy | None = None, + override_repr: _ReprFunc | None = None, ) -> None: ... -def get_printoptions() -> _FormatOptions: ... + +# public numpy export +def printoptions( + precision: SupportsIndex | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + linewidth: int | None = None, + suppress: bool | None = None, + nanstr: str | None = None, + infstr: str | None = None, + formatter: _FormatDict | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + *, + legacy: _Legacy | None = None, + override_repr: _ReprFunc | None = None, +) -> _GeneratorContextManager[_FormatOptions]: ... + +# public numpy export +@overload # no style def array2string( a: NDArray[Any], - max_line_width: int | None = ..., - precision: SupportsIndex | None = ..., - suppress_small: bool | None = ..., - separator: str = ..., - prefix: str = ..., - # NOTE: With the `style` argument being deprecated, - # all arguments between `formatter` and `suffix` are de facto - # keyworld-only arguments + max_line_width: int | None = None, + precision: SupportsIndex | None = None, + suppress_small: bool | None = None, + separator: str = " ", + prefix: str = "", *, - formatter: _FormatDict | None = ..., - threshold: int | None = ..., - edgeitems: int | None = ..., - sign: _Sign | None = ..., - floatmode: _FloatMode | None = ..., - suffix: str = ..., - legacy: _Legacy | None = ..., + formatter: _FormatDict | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + suffix: str = "", + legacy: _Legacy | None = None, ) -> str: ... +@overload # style= (positional), legacy="1.13" +def array2string( + a: NDArray[Any], + max_line_width: int | None, + precision: SupportsIndex | None, + suppress_small: bool | None, + separator: str, + prefix: str, + style: _ReprFunc, + formatter: _FormatDict | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + suffix: str = "", + *, + legacy: Literal["1.13"], +) -> str: ... +@overload # style= (keyword), legacy="1.13" +def array2string( + a: NDArray[Any], + max_line_width: int | None = None, + precision: SupportsIndex | None = None, + suppress_small: bool | None = None, + separator: str = " ", + prefix: str = "", + *, + style: _ReprFunc, + formatter: _FormatDict | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + suffix: str = "", + legacy: Literal["1.13"], +) -> str: ... +@overload # style= (positional), legacy!="1.13" +@deprecated("'style' argument is deprecated and no longer functional except in 1.13 'legacy' mode") +def array2string( + a: NDArray[Any], + max_line_width: int | None, + precision: SupportsIndex | None, + suppress_small: bool | None, + separator: str, + prefix: str, + style: _ReprFunc, + formatter: _FormatDict | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + suffix: str = "", + *, + legacy: _LegacyNoStyle | None = None, +) -> str: ... +@overload # style= (keyword), legacy="1.13" +@deprecated("'style' argument is deprecated and no longer functional except in 1.13 'legacy' mode") +def array2string( + a: NDArray[Any], + max_line_width: int | None = None, + precision: SupportsIndex | None = None, + suppress_small: bool | None = None, + separator: str = " ", + prefix: str = "", + *, + style: _ReprFunc, + formatter: _FormatDict | None = None, + threshold: int | None = None, + edgeitems: int | None = None, + sign: _Sign | None = None, + floatmode: _FloatMode | None = None, + suffix: str = "", + legacy: _LegacyNoStyle | None = None, +) -> str: ... + +# public numpy export def format_float_scientific( x: _FloatLike_co, - precision: int | None = ..., - unique: bool = ..., - trim: _Trim = ..., - sign: bool = ..., - pad_left: int | None = ..., - exp_digits: int | None = ..., - min_digits: int | None = ..., + precision: int | None = None, + unique: bool = True, + trim: _Trim = "k", + sign: bool = False, + pad_left: int | None = None, + exp_digits: int | None = None, + min_digits: int | None = None, ) -> str: ... + +# public numpy export def format_float_positional( x: _FloatLike_co, - precision: int | None = ..., - unique: bool = ..., - fractional: bool = ..., - trim: _Trim = ..., - sign: bool = ..., - pad_left: int | None = ..., - pad_right: int | None = ..., - min_digits: int | None = ..., + precision: int | None = None, + unique: bool = True, + fractional: bool = True, + trim: _Trim = "k", + sign: bool = False, + pad_left: int | None = None, + pad_right: int | None = None, + min_digits: int | None = None, ) -> str: ... + +# public numpy export def array_repr( arr: NDArray[Any], - max_line_width: int | None = ..., - precision: SupportsIndex | None = ..., - suppress_small: bool | None = ..., + max_line_width: int | None = None, + precision: SupportsIndex | None = None, + suppress_small: bool | None = None, ) -> str: ... + +# public numpy export def array_str( a: NDArray[Any], - max_line_width: int | None = ..., - precision: SupportsIndex | None = ..., - suppress_small: bool | None = ..., + max_line_width: int | None = None, + precision: SupportsIndex | None = None, + suppress_small: bool | None = None, ) -> str: ... -def printoptions( - precision: SupportsIndex | None = ..., - threshold: int | None = ..., - edgeitems: int | None = ..., - linewidth: int | None = ..., - suppress: bool | None = ..., - nanstr: str | None = ..., - infstr: str | None = ..., - formatter: _FormatDict | None = ..., - sign: _Sign | None = ..., - floatmode: _FloatMode | None = ..., - *, - legacy: _Legacy | None = ..., -) -> _GeneratorContextManager[_FormatOptions]: ... diff --git a/test/static/reject/arrayprint.pyi b/test/static/reject/arrayprint.pyi index ccb91f38..03dd0c0a 100644 --- a/test/static/reject/arrayprint.pyi +++ b/test/static/reject/arrayprint.pyi @@ -6,11 +6,11 @@ import numpy.typing as npt AR: npt.NDArray[np.float64] func1: Callable[[Any], str] -func2: Callable[[np.integer[Any]], str] +func2: Callable[[np.integer], str] -np.array2string(AR, style=None) # type: ignore[call-arg] # pyright: ignore[reportCallIssue] -np.array2string(AR, legacy="1.14") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] -np.array2string(AR, sign="*") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] -np.array2string(AR, floatmode="default") # type: ignore[arg-type] # pyright: ignore[reportArgumentType] -np.array2string(AR, formatter={"A": func1}) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] -np.array2string(AR, formatter={"float": func2}) # type: ignore[typeddict-item] # pyright: ignore[reportArgumentType] +np.array2string(AR, style=None) # type: ignore[call-overload] # pyright: ignore[reportArgumentType] +np.array2string(AR, legacy="1.14") # type: ignore[call-overload] # pyright: ignore[reportArgumentType] +np.array2string(AR, sign="*") # type: ignore[call-overload] # pyright: ignore[reportArgumentType] +np.array2string(AR, floatmode="default") # type: ignore[call-overload] # pyright: ignore[reportArgumentType] +np.array2string(AR, formatter={"A": func1}) # type: ignore[call-overload] # pyright: ignore[reportArgumentType] +np.array2string(AR, formatter={"float": func2}) # type: ignore[call-overload] # pyright: ignore[reportArgumentType]