Thanks to visit codestin.com
Credit goes to github.com

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
238 changes: 173 additions & 65 deletions src/numpy-stubs/_core/arrayprint.pyi
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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=<given> (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=<given> (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=<given> (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=<given> (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]: ...
14 changes: 7 additions & 7 deletions test/static/reject/arrayprint.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]