diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2020-10-16 09:43:13 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-16 09:43:13 -0600 |
commit | b9dd2be0108cb312b4c34239a1dc8d24ef3a05a9 (patch) | |
tree | c2acac172a6b6ee298a4ae9165f111a070438480 /numpy/typing | |
parent | b4718373f5412ea0d52ecbe2a3f9bbed824953a0 (diff) | |
parent | b81ab444c0e56011e96c8895a19e18906ab4e731 (diff) | |
download | numpy-b9dd2be0108cb312b4c34239a1dc8d24ef3a05a9.tar.gz |
Merge pull request #16759 from person142/dtype-generic
ENH: make dtype generic over scalar type
Diffstat (limited to 'numpy/typing')
-rw-r--r-- | numpy/typing/__init__.py | 2 | ||||
-rw-r--r-- | numpy/typing/_dtype_like.py | 30 | ||||
-rw-r--r-- | numpy/typing/tests/data/fail/dtype.py | 9 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/dtype.py | 33 |
4 files changed, 57 insertions, 17 deletions
diff --git a/numpy/typing/__init__.py b/numpy/typing/__init__.py index 987aa39aa..dafabd95a 100644 --- a/numpy/typing/__init__.py +++ b/numpy/typing/__init__.py @@ -102,7 +102,7 @@ from ._scalars import ( ) from ._array_like import _SupportsArray, ArrayLike from ._shape import _Shape, _ShapeLike -from ._dtype_like import DtypeLike +from ._dtype_like import _SupportsDtype, _VoidDtypeLike, DtypeLike from numpy._pytesttester import PytestTester test = PytestTester(__name__) diff --git a/numpy/typing/_dtype_like.py b/numpy/typing/_dtype_like.py index 7c1946a3e..5bfd8ffdc 100644 --- a/numpy/typing/_dtype_like.py +++ b/numpy/typing/_dtype_like.py @@ -38,18 +38,9 @@ else: _DtypeDict = Any _SupportsDtype = Any -# Anything that can be coerced into numpy.dtype. -# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html -DtypeLike = Union[ - dtype, - # default data type (float64) - None, - # array-scalar types and generic types - type, # TODO: enumerate these when we add type hints for numpy scalars - # anything with a dtype attribute - _SupportsDtype, - # character codes, type strings or comma-separated fields, e.g., 'float64' - str, + +# Would create a dtype[np.void] +_VoidDtypeLike = Union[ # (flexible_dtype, itemsize) Tuple[_DtypeLikeNested, int], # (fixed_dtype, shape) @@ -67,6 +58,21 @@ DtypeLike = Union[ Tuple[_DtypeLikeNested, _DtypeLikeNested], ] +# Anything that can be coerced into numpy.dtype. +# Reference: https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html +DtypeLike = Union[ + dtype, + # default data type (float64) + None, + # array-scalar types and generic types + type, # TODO: enumerate these when we add type hints for numpy scalars + # anything with a dtype attribute + _SupportsDtype, + # character codes, type strings or comma-separated fields, e.g., 'float64' + str, + _VoidDtypeLike, +] + # NOTE: while it is possible to provide the dtype as a dict of # dtype-like objects (e.g. `{'field1': ..., 'field2': ..., ...}`), # this syntax is officially discourged and diff --git a/numpy/typing/tests/data/fail/dtype.py b/numpy/typing/tests/data/fail/dtype.py index 3dc027daf..7d4783d8f 100644 --- a/numpy/typing/tests/data/fail/dtype.py +++ b/numpy/typing/tests/data/fail/dtype.py @@ -1,15 +1,16 @@ import numpy as np - class Test: not_dtype = float -np.dtype(Test()) # E: Argument 1 to "dtype" has incompatible type +np.dtype(Test()) # E: No overload variant of "dtype" matches -np.dtype( - { # E: Argument 1 to "dtype" has incompatible type +np.dtype( # E: No overload variant of "dtype" matches + { "field1": (float, 1), "field2": (int, 3), } ) + +np.dtype[np.float64](np.int64) # E: Argument 1 to "dtype" has incompatible type diff --git a/numpy/typing/tests/data/reveal/dtype.py b/numpy/typing/tests/data/reveal/dtype.py new file mode 100644 index 000000000..e0802299e --- /dev/null +++ b/numpy/typing/tests/data/reveal/dtype.py @@ -0,0 +1,33 @@ +import numpy as np + +reveal_type(np.dtype(np.float64)) # E: numpy.dtype[numpy.float64*] +reveal_type(np.dtype(np.int64)) # E: numpy.dtype[numpy.int64*] + +# String aliases +reveal_type(np.dtype("float64")) # E: numpy.dtype[numpy.float64] +reveal_type(np.dtype("float32")) # E: numpy.dtype[numpy.float32] +reveal_type(np.dtype("int64")) # E: numpy.dtype[numpy.int64] +reveal_type(np.dtype("int32")) # E: numpy.dtype[numpy.int32] +reveal_type(np.dtype("bool")) # E: numpy.dtype[numpy.bool_] +reveal_type(np.dtype("bytes")) # E: numpy.dtype[numpy.bytes_] +reveal_type(np.dtype("str")) # E: numpy.dtype[numpy.str_] + +# Python types +reveal_type(np.dtype(complex)) # E: numpy.dtype[numpy.complex128] +reveal_type(np.dtype(float)) # E: numpy.dtype[numpy.float64] +reveal_type(np.dtype(int)) # E: numpy.dtype +reveal_type(np.dtype(bool)) # E: numpy.dtype[numpy.bool_] +reveal_type(np.dtype(str)) # E: numpy.dtype[numpy.str_] +reveal_type(np.dtype(bytes)) # E: numpy.dtype[numpy.bytes_] + +# Special case for None +reveal_type(np.dtype(None)) # E: numpy.dtype[numpy.float64] + +# Dtypes of dtypes +reveal_type(np.dtype(np.dtype(np.float64))) # E: numpy.dtype[numpy.float64*] + +# Parameterized dtypes +reveal_type(np.dtype("S8")) # E: numpy.dtype + +# Void +reveal_type(np.dtype(("U", 10))) # E: numpy.dtype[numpy.void] |