summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2020-12-11 08:13:57 -0700
committerGitHub <noreply@github.com>2020-12-11 08:13:57 -0700
commitba596df4cc23445a117c3785239a75a3cab6b2f2 (patch)
tree6dd52b6b5bf9d701c752d935a94ec3f5958c94e5
parent78a58c3d5b9b5fb5d7def57bc5b026e539744910 (diff)
parentab4252d1ea4bca1d881a44376a693ca8c5a75c1b (diff)
downloadnumpy-ba596df4cc23445a117c3785239a75a3cab6b2f2.tar.gz
Merge pull request #17968 from BvB93/dtype-typevar
ENH: Use more typevars in `np.dtype`
-rw-r--r--numpy/__init__.pyi10
-rw-r--r--numpy/typing/tests/data/pass/dtype.py8
-rw-r--r--numpy/typing/tests/data/reveal/dtype.py8
3 files changed, 21 insertions, 5 deletions
diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi
index 83afd2e49..f2c414c0b 100644
--- a/numpy/__init__.pyi
+++ b/numpy/__init__.pyi
@@ -870,7 +870,7 @@ class dtype(Generic[_DTypeScalar]):
@property
def alignment(self) -> int: ...
@property
- def base(self) -> dtype: ...
+ def base(self: _DType) -> _DType: ...
@property
def byteorder(self) -> str: ...
@property
@@ -880,7 +880,7 @@ class dtype(Generic[_DTypeScalar]):
@property
def fields(
self,
- ) -> Optional[Mapping[str, Union[Tuple[dtype, int], Tuple[dtype, int, Any]]]]: ...
+ ) -> Optional[Mapping[str, Union[Tuple[dtype[Any], int], Tuple[dtype[Any], int, Any]]]]: ...
@property
def flags(self) -> int: ...
@property
@@ -906,14 +906,14 @@ class dtype(Generic[_DTypeScalar]):
@property
def ndim(self) -> int: ...
@property
- def subdtype(self) -> Optional[Tuple[dtype, _Shape]]: ...
- def newbyteorder(self, __new_order: _ByteOrder = ...) -> dtype: ...
+ def subdtype(self: _DType) -> Optional[Tuple[_DType, _Shape]]: ...
+ def newbyteorder(self: _DType, __new_order: _ByteOrder = ...) -> _DType: ...
# Leave str and type for end to avoid having to use `builtins.str`
# everywhere. See https://github.com/python/mypy/issues/3775
@property
def str(self) -> builtins.str: ...
@property
- def type(self) -> Type[generic]: ...
+ def type(self) -> Type[_DTypeScalar]: ...
class _flagsobj:
aligned: bool
diff --git a/numpy/typing/tests/data/pass/dtype.py b/numpy/typing/tests/data/pass/dtype.py
index cbae8c078..a97edc302 100644
--- a/numpy/typing/tests/data/pass/dtype.py
+++ b/numpy/typing/tests/data/pass/dtype.py
@@ -1,5 +1,7 @@
import numpy as np
+dtype_obj = np.dtype(np.str_)
+
np.dtype(dtype=np.int64)
np.dtype(int)
np.dtype("int")
@@ -33,3 +35,9 @@ class Test:
np.dtype(Test())
+
+# Methods and attributes
+dtype_obj.base
+dtype_obj.subdtype
+dtype_obj.newbyteorder()
+dtype_obj.type
diff --git a/numpy/typing/tests/data/reveal/dtype.py b/numpy/typing/tests/data/reveal/dtype.py
index d414f2c49..626a15270 100644
--- a/numpy/typing/tests/data/reveal/dtype.py
+++ b/numpy/typing/tests/data/reveal/dtype.py
@@ -1,5 +1,7 @@
import numpy as np
+dtype_obj: np.dtype[np.str_]
+
reveal_type(np.dtype(np.float64)) # E: numpy.dtype[numpy.floating[numpy.typing._64Bit]]
reveal_type(np.dtype(np.int64)) # E: numpy.dtype[numpy.signedinteger[numpy.typing._64Bit]]
@@ -31,3 +33,9 @@ reveal_type(np.dtype("S8")) # E: numpy.dtype
# Void
reveal_type(np.dtype(("U", 10))) # E: numpy.dtype[numpy.void]
+
+# Methods and attributes
+reveal_type(dtype_obj.base) # E: numpy.dtype[numpy.str_]
+reveal_type(dtype_obj.subdtype) # E: Union[Tuple[numpy.dtype[numpy.str_], builtins.tuple[builtins.int]], None]
+reveal_type(dtype_obj.newbyteorder()) # E: numpy.dtype[numpy.str_]
+reveal_type(dtype_obj.type) # E: Type[numpy.str_]