diff --git a/numpy-stubs/__init__.pyi b/numpy-stubs/__init__.pyi index 75323b5..d139bb6 100644 --- a/numpy-stubs/__init__.pyi +++ b/numpy-stubs/__init__.pyi @@ -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"] diff --git a/numpy-stubs/core/numerictypes.pyi b/numpy-stubs/core/numerictypes.pyi deleted file mode 100644 index be01533..0000000 --- a/numpy-stubs/core/numerictypes.pyi +++ /dev/null @@ -1,20 +0,0 @@ -from typing import Any, Optional, Union, Tuple, List - -from numpy import dtype - -def maximum_sctype(t: dtype) -> dtype: ... -def issctype(rep: Any) -> bool: ... -def obj2sctype(rep: Any, default: Optional[Any] = ...) -> type: ... -def issubclass_( - arg1: type, arg2: Union[type, Tuple[Union[type, Tuple], ...]] -) -> bool: ... -def issubsctype( - arg1: type, arg2: Union[type, Tuple[Union[type, Tuple], ...]] -) -> bool: ... -def issubdtype( - arg1: Union[object, type], arg2: Union[type, Tuple[Union[type, Tuple], ...]] -) -> bool: ... -def sctype2char(sctype: Any) -> str: ... -def find_common_type( - array_types: Union[dtype, List[dtype]], scalar_types: Union[dtype, List[dtype]] -) -> dtype: ... diff --git a/tests/fail/numerictypes.py b/tests/fail/numerictypes.py new file mode 100644 index 0000000..dd03eac --- /dev/null +++ b/tests/fail/numerictypes.py @@ -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]" diff --git a/tests/pass/numerictypes.py b/tests/pass/numerictypes.py new file mode 100644 index 0000000..4f205ca --- /dev/null +++ b/tests/pass/numerictypes.py @@ -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]) diff --git a/tests/reveal/numerictypes.py b/tests/reveal/numerictypes.py new file mode 100644 index 0000000..e026158 --- /dev/null +++ b/tests/reveal/numerictypes.py @@ -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