10000 TYP: add missing annotations for ``numpy.object_.__new__`` by jorenham · Pull Request #26865 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: add missing annotations for numpy.object_.__new__ #26865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024
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
10000
Diff view
34 changes: 31 additions & 3 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2962,13 +2962,41 @@ class bool(generic):

bool_: TypeAlias = bool

_StringType = TypeVar("_StringType", bound=str | bytes)
_ShapeType = TypeVar("_ShapeType", bound=Any)
_ObjectType = TypeVar("_ObjectType", bound=object)

# A sequence-like interface like `collections.abc.Sequence`, but without the
# irrelevant methods.
class _SimpleSequence(Protocol):
def __len__(self, /) -> int: ...
def __getitem__(self, index: int, /) -> Any: ...

# The `object_` constructor returns the passed object, so instances with type
# `object_` cannot exists (at runtime).
@final
class object_(generic):
def __init__(self, value: object = ..., /) -> None: ...
@overload
def __new__(cls, nothing_to_see_here: None = ..., /) -> None: ...
@overload
def __new__(cls, stringy: _StringType, /) -> _StringType: ...
@overload
def __new__(
cls,
array: ndarray[_ShapeType, Any], /,
) -> ndarray[_ShapeType, dtype[object_]]: ...
@overload
def __new__(cls, sequence: _SimpleSequence, /) -> NDArray[object_]: ...
@overload
def __new__(cls, value: _ObjectType, /) -> _ObjectType: ...
# catch-all
@overload
def __new__(cls, value: Any = ..., /) -> object | NDArray[object_]: ...

@property
def real(self: _ArraySelf) -> _ArraySelf: ...
def real(self) -> object_: ...
@property
def imag(self: _ArraySelf) -> _ArraySelf: ...
def imag(self) -> object_: ...
# The 3 protocols below may or may not raise,
# depending on the underlying object
def __int__(self) -> int: ...
Expand Down
29 changes: 29 additions & 0 deletions numpy/typing/tests/data/reveal/scalars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ m: np.timedelta64
U: np.str_
S: np.bytes_
V: np.void
O: np.object_ # cannot exists at runtime

array_nd: np.ndarray[Any, Any]
array_0d: np.ndarray[tuple[()], Any]
array_2d_2x2: np.ndarray[tuple[Literal[2], Literal[2]], Any]

assert_type(c8.real, np.float32)
assert_type(c8.imag, np.float32)
Expand Down Expand Up @@ -156,3 +161,27 @@ assert_type(f8.__ceil__(), int)
assert_type(f8.__floor__(), int)

assert_type(i8.is_integer(), Literal[True])

assert_type(O.real, np.object_)
assert_type(O.imag, np.object_)
assert_type(int(O), int)
assert_type(float(O), float)
assert_type(complex(O), complex)

# These fail fail because of a mypy __new__ bug:
# https://github.com/python/mypy/issues/15182
# According to the typing spec, the following statements are valid, see
# https://typing.readthedocs.io/en/latest/spec/constructors.html#new-method

# assert_type(np.object_(), None)
# assert_type(np.object_(None), None)
# assert_type(np.object_(array_nd), np.ndarray[Any, np.dtype[np.object_]])
# assert_type(np.object_([]), npt.NDArray[np.object_])
# assert_type(np.object_(()), npt.NDArray[np.object_])
# assert_type(np.object_(range(4)), npt.NDArray[np.object_])
# assert_type(np.object_(+42), int)
# assert_type(np.object_(1 / 137), float)
# assert_type(np.object_('Developers! ' * (1 << 6)), str)
# assert_type(np.object_(object()), object)
# assert_type(np.object_({False, True, NotADirectoryError}), set[Any])
# assert_type(np.object_({'spam': 'food', 'ham': 'food'}), dict[str, str])
Loading
0