Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit 1eaccd4

Browse files
authored
MAINT: pick up types from core/numerictypes.pyi (#83)
Closes #65. The types currently aren't picked up. Fix that by moving them into `__init__.pyi` along with the rest of the types. While we are at it, polish the signatures a bit and add tests.
1 parent 66ed43a commit 1eaccd4

File tree

5 files changed

+83
-20
lines changed

5 files changed

+83
-20
lines changed

numpy-stubs/__init__.pyi

+23
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,29 @@ class AxisError(ValueError, IndexError):
796796
self, axis: int, ndim: Optional[int] = ..., msg_prefix: Optional[str] = ...
797797
) -> None: ...
798798

799+
# Functions from np.core.numerictypes
800+
_DefaultType = TypeVar("_DefaultType")
801+
802+
def maximum_sctype(t: DtypeLike) -> dtype: ...
803+
def issctype(rep: object) -> bool: ...
804+
@overload
805+
def obj2sctype(rep: object) -> Optional[generic]: ...
806+
@overload
807+
def obj2sctype(rep: object, default: None) -> Optional[generic]: ...
808+
@overload
809+
def obj2sctype(
810+
rep: object, default: Type[_DefaultType]
811+
) -> Union[generic, Type[_DefaultType]]: ...
812+
def issubclass_(arg1: object, arg2: Union[object, Tuple[object, ...]]) -> bool: ...
813+
def issubsctype(
814+
arg1: Union[ndarray, DtypeLike], arg2: Union[ndarray, DtypeLike]
815+
) -> bool: ...
816+
def issubdtype(arg1: DtypeLike, arg2: DtypeLike) -> bool: ...
817+
def sctype2char(sctype: object) -> str: ...
818+
def find_common_type(
819+
array_types: Sequence[DtypeLike], scalar_types: Sequence[DtypeLike]
820+
) -> dtype: ...
821+
799822
# Functions from np.core.fromnumeric
800823
_Mode = Literal["raise", "wrap", "clip"]
801824
_Order = Literal["C", "F", "A"]

numpy-stubs/core/numerictypes.pyi

-20
This file was deleted.

tests/fail/numerictypes.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
3+
# Techincally this works, but probably shouldn't. See
4+
#
5+
# https://github.com/numpy/numpy/issues/16366
6+
#
7+
np.maximum_sctype(1) # E: incompatible type "int"
8+
9+
np.issubsctype(1, np.int64) # E: incompatible type "int"
10+
11+
np.issubdtype(1, np.int64) # E: incompatible type "int"
12+
13+
np.find_common_type(np.int64, np.int64) # E: incompatible type "Type[int64]"

tests/pass/numerictypes.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import numpy as np
2+
3+
np.maximum_sctype("S8")
4+
np.maximum_sctype(object)
5+
6+
np.issctype(object)
7+
np.issctype("S8")
8+
9+
np.obj2sctype(list)
10+
np.obj2sctype(list, default=None)
11+
np.obj2sctype(list, default=np.string_)
12+
13+
np.issubclass_(np.int32, int)
14+
np.issubclass_(np.float64, float)
15+
np.issubclass_(np.float64, (int, float))
16+
17+
np.issubsctype("int64", int)
18+
np.issubsctype(np.array([1]), np.array([1]))
19+
20+
np.issubdtype("S1", np.string_)
21+
np.issubdtype(np.float64, np.float32)
22+
23+
np.sctype2char("S1")
24+
np.sctype2char(list)
25+
26+
np.find_common_type([], [np.int64, np.float32, complex])
27+
np.find_common_type((), (np.int64, np.float32, complex))
28+
np.find_common_type([np.int64, np.float32], [])
29+
np.find_common_type([np.float32], [np.int64, np.float64])

tests/reveal/numerictypes.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
3+
reveal_type(np.issctype(np.generic)) # E: bool
4+
reveal_type(np.issctype("foo")) # E: bool
5+
6+
reveal_type(np.obj2sctype("S8")) # E: Union[numpy.generic, None]
7+
reveal_type(np.obj2sctype("S8", default=None)) # E: Union[numpy.generic, None]
8+
reveal_type(
9+
np.obj2sctype("foo", default=int) # E: Union[numpy.generic, Type[builtins.int*]]
10+
)
11+
12+
reveal_type(np.issubclass_(np.float64, float)) # E: bool
13+
reveal_type(np.issubclass_(np.float64, (int, float))) # E: bool
14+
15+
reveal_type(np.sctype2char("S8")) # E: str
16+
reveal_type(np.sctype2char(list)) # E: str
17+
18+
reveal_type(np.find_common_type([np.int64], [np.int64])) # E: numpy.dtype

0 commit comments

Comments
 (0)