Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

MAINT: pick up types from core/numerictypes.pyi #83

Merged
merged 1 commit into from
Jun 5, 2020
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
23 changes: 23 additions & 0 deletions numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,29 @@ class AxisError(ValueError, IndexError):
self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ...
) -> None: ...

# Functions from np.core.numerictypes
_DefaultType = TypeVar("_DefaultType")

def maximum_sctype(t: DtypeLike) -> dtype: ...
def issctype(rep: object) -> bool: ...
@overload
def obj2sctype(rep: object) -> Optional[generic]: ...
@overload
def obj2sctype(rep: object, default: None) -> Optional[generic]: ...
@overload
def obj2sctype(
rep: object, default: Type[_DefaultType]
) -> Union[generic, Type[_DefaultType]]: ...
def issubclass_(arg1: object, arg2: Union[object, Tuple[object, ...]]) -> bool: ...
def issubsctype(
arg1: Union[ndarray, DtypeLike], arg2: Union[ndarray, DtypeLike]
) -> bool: ...
def issubdtype(arg1: DtypeLike, arg2: DtypeLike) -> bool: ...
def sctype2char(sctype: object) -> str: ...
def find_common_type(
array_types: Sequence[DtypeLike], scalar_types: Sequence[DtypeLike]
) -> dtype: ...

# Functions from np.core.fromnumeric
_Mode = Literal["raise", "wrap", "clip"]
_Order = Literal["C", "F", "A"]
Expand Down
20 changes: 0 additions & 20 deletions numpy-stubs/core/numerictypes.pyi

This file was deleted.

13 changes: 13 additions & 0 deletions tests/fail/numerictypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np

# Techincally this works, but probably shouldn't. See
#
# https://github.com/numpy/numpy/issues/16366
#
np.maximum_sctype(1) # E: incompatible type "int"

np.issubsctype(1, np.int64) # E: incompatible type "int"

np.issubdtype(1, np.int64) # E: incompatible type "int"

np.find_common_type(np.int64, np.int64) # E: incompatible type "Type[int64]"
29 changes: 29 additions & 0 deletions tests/pass/numerictypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np

np.maximum_sctype("S8")
np.maximum_sctype(object)

np.issctype(object)
np.issctype("S8")

np.obj2sctype(list)
np.obj2sctype(list, default=None)
np.obj2sctype(list, default=np.string_)

np.issubclass_(np.int32, int)
np.issubclass_(np.float64, float)
np.issubclass_(np.float64, (int, float))

np.issubsctype("int64", int)
np.issubsctype(np.array([1]), np.array([1]))

np.issubdtype("S1", np.string_)
np.issubdtype(np.float64, np.float32)

np.sctype2char("S1")
np.sctype2char(list)

np.find_common_type([], [np.int64, np.float32, complex])
np.find_common_type((), (np.int64, np.float32, complex))
np.find_common_type([np.int64, np.float32], [])
np.find_common_type([np.float32], [np.int64, np.float64])
18 changes: 18 additions & 0 deletions tests/reveal/numerictypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np

reveal_type(np.issctype(np.generic)) # E: bool
reveal_type(np.issctype("foo")) # E: bool

reveal_type(np.obj2sctype("S8")) # E: Union[numpy.generic, None]
reveal_type(np.obj2sctype("S8", default=None)) # E: Union[numpy.generic, None]
reveal_type(
np.obj2sctype("foo", default=int) # E: Union[numpy.generic, Type[builtins.int*]]
)

reveal_type(np.issubclass_(np.float64, float)) # E: bool
reveal_type(np.issubclass_(np.float64, (int, float))) # E: bool

reveal_type(np.sctype2char("S8")) # E: str
reveal_type(np.sctype2char(list)) # E: str

reveal_type(np.find_common_type([np.int64], [np.int64])) # E: numpy.dtype