Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb38ec2

Browse files
Replace nptyping with numpy.typing (#154)
Fixes #152, #153 I replaced `nptyping` NDArray occurrences with `numpy.typing` ones. I am not sure if it's possible to specify shape restrictions without forcing the package to require `python>=3.9` so that [type subscription](https://peps.python.org/pep-0585/) is available and things like `np.ndarray[Literal[1], np.dtype[Any]]` can be declared. Note that `numpy.typing.NDArray` is a type alias for `np.ndarray[Any, np.dtype[+ScalarType]]` where `ScalarType` contains the following types: ``` (int, float, complex, bool, bytes, str, memoryview, numpy.bool_, numpy.complex64, numpy.complex128, numpy.complex256, numpy.float16, numpy.float32, numpy.float64, numpy.float128, numpy.int8, numpy.int16, numpy.int32, numpy.int64, numpy.longlong, numpy.timedelta64, numpy.datetime64, numpy.object_, numpy.bytes_, numpy.str_, numpy.uint8, numpy.uint16, numpy.uint32, numpy.uint64, numpy.ulonglong, numpy.void) ``` Although it is simpler to use it as it is, please let me know if a more permissible `NDArray[Any]` is preferred.
1 parent 40f49c3 commit bb38ec2

9 files changed

Lines changed: 22 additions & 28 deletions

File tree

README.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ Requirements
3232
------------
3333

3434
* `numpy <https://numpy.org/>`_
35-
* nptyping
3635
* typing_extensions
3736

3837
v1.0+ requires Python 3.7 or above. If you have an older Python version, please install a v0.x release instead.

docs/requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
sphinx==5.1.1
22
sphinx-rtd-theme==1.0.0
3-
numpy>=1.11.1
3+
numpy>=1.21
44
numpydoc==1.4.0
5-
nptyping
6-
typing_extensions
5+
typing_extensions

docs/source/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Requirements
2727
------------
2828

2929
* `numpy <https://numpy.org/>`_
30-
* nptyping
3130
* typing_extensions
3231

3332
v1.0+ requires Python 3.7 or above. If you have an older Python version, please install a v0.x release instead.
@@ -108,4 +107,4 @@ Contents
108107
Issue tracker <https://github.com/mhe/pynrrd/issues>
109108
Releases <https://github.com/mhe/pynrrd/releases>
110109
PyPi <https://pypi.org/project/pynrrd/>
111-
License <https://github.com/mhe/pynrrd/blob/master/LICENSE>
110+
License <https://github.com/mhe/pynrrd/blob/master/LICENSE>

nrrd/formatters.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import Any, Optional, Union
1+
from typing import Optional, Union
22

3-
import nptyping as npt
43
import numpy as np
5-
from typing_extensions import Literal
4+
import numpy.typing as npt
65

76

87
def format_number(x: Union[int, float]) -> str:
@@ -42,7 +41,7 @@ def format_number(x: Union[int, float]) -> str:
4241
return value
4342

4443

45-
def format_vector(x: npt.NDArray[Literal['*'], Any]) -> str:
44+
def format_vector(x: npt.NDArray) -> str:
4645
"""Format a (N,) :class:`numpy.ndarray` into a NRRD vector string
4746
4847
See :ref:`background/datatypes:int vector` and :ref:`background/datatypes:double vector` for more information on
@@ -62,7 +61,7 @@ def format_vector(x: npt.NDArray[Literal['*'], Any]) -> str:
6261
return '(' + ','.join([format_number(y) for y in x]) + ')'
6362

6463

65-
def format_optional_vector(x: Optional[npt.NDArray[Literal['*'], Any]]) -> str:
64+
def format_optional_vector(x: Optional[npt.NDArray]) -> str:
6665
"""Format a (N,) :class:`numpy.ndarray` into a NRRD optional vector string
6766
6867
Function converts a (N,) :class:`numpy.ndarray` or :obj:`None` into a string using NRRD vector format. If the input
@@ -90,7 +89,7 @@ def format_optional_vector(x: Optional[npt.NDArray[Literal['*'], Any]]) -> str:
9089
return format_vector(x)
9190

9291

93-
def format_matrix(x: npt.NDArray[Literal['*, *'], Any]) -> str:
92+
def format_matrix(x: npt.NDArray) -> str:
9493
"""Format a (M,N) :class:`numpy.ndarray` into a NRRD matrix string
9594
9695
See :ref:`background/datatypes:int matrix` and :ref:`background/datatypes:double matrix` for more information on
@@ -110,7 +109,7 @@ def format_matrix(x: npt.NDArray[Literal['*, *'], Any]) -> str:
110109
return ' '.join([format_vector(y) for y in x])
111110

112111

113-
def format_optional_matrix(x: Optional[npt.NDArray[Literal['*, *'], Any]]) -> str:
112+
def format_optional_matrix(x: Optional[npt.NDArray]) -> str:
114113
"""Format a (M,N) :class:`numpy.ndarray` of :class:`float` into a NRRD optional matrix string
115114
116115
Function converts a (M,N) :class:`numpy.ndarray` of :class:`float` into a string using the NRRD matrix format. For
@@ -136,7 +135,7 @@ def format_optional_matrix(x: Optional[npt.NDArray[Literal['*, *'], Any]]) -> st
136135
return ' '.join([format_optional_vector(y) for y in x])
137136

138137

139-
def format_number_list(x: npt.NDArray[Literal['*'], Any]) -> str:
138+
def format_number_list(x: npt.NDArray) -> str:
140139
"""Format a (N,) :class:`numpy.ndarray` into a NRRD number list.
141140
142141
See :ref:`background/datatypes:int list` and :ref:`background/datatypes:double list` for more information on the

nrrd/parsers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from typing import Any, Optional, Type, Union
1+
from typing import Optional, Type, Union
22

3-
import nptyping as npt
43
import numpy as np
5-
from typing_extensions import Literal
4+
import numpy.typing as npt
65

76
from nrrd.errors import NRRDError
87

98

10-
def parse_vector(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray[Literal['*'], Any]:
9+
def parse_vector(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray:
1110
"""Parse NRRD vector from string into (N,) :class:`numpy.ndarray`.
1211
1312
See :ref:`background/datatypes:int vector` and :ref:`background/datatypes:double vector` for more information on
@@ -52,7 +51,7 @@ def parse_vector(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt
5251

5352

5453
def parse_optional_vector(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> \
55-
Optional[npt.NDArray[Literal['*'], Any]]:
54+
Optional[npt.NDArray]:
5655
"""Parse optional NRRD vector from string into (N,) :class:`numpy.ndarray` or :obj:`None`.
5756
5857
Function parses optional NRRD vector from string into an (N,) :class:`numpy.ndarray`. This function works the same
@@ -83,7 +82,7 @@ def parse_optional_vector(x: str, dtype: Optional[Type[Union[int, float]]] = Non
8382
return parse_vector(x, dtype)
8483

8584

86-
def parse_matrix(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray[Literal['*, *'], Any]:
85+
def parse_matrix(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray:
8786
"""Parse NRRD matrix from string into (M,N) :class:`numpy.ndarray`.
8887
8988
See :ref:`background/datatypes:int matrix` and :ref:`background/datatypes:double matrix` for more information on
@@ -130,7 +129,7 @@ def parse_matrix(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt
130129
return matrix
131130

132131

133-
def parse_optional_matrix(x: str) -> Optional[npt.NDArray[Literal['*, *'], Any]]:
132+
def parse_optional_matrix(x: str) -> Optional[npt.NDArray]:
134133
"""Parse optional NRRD matrix from string into (M,N) :class:`numpy.ndarray` of :class:`float`.
135134
136135
Function parses optional NRRD matrix from string into an (M,N) :class:`numpy.ndarray` of :class:`float`. This
@@ -173,7 +172,7 @@ def parse_optional_matrix(x: str) -> Optional[npt.NDArray[Literal['*, *'], Any]]
173172
return matrix
174173

175174

176-
def parse_number_list(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray[Literal['*'], Any]:
175+
def parse_number_list(x: str, dtype: Optional[Type[Union[int, float]]] = None) -> npt.NDArray:
177176
"""Parse NRRD number list from string into (N,) :class:`numpy.ndarray`.
178177
179178
See :ref:`background/datatypes:int list` and :ref:`background/datatypes:double list` for more information on the

nrrd/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import warnings
77
import zlib
88
from collections import OrderedDict
9-
from typing import IO, AnyStr, Iterable, Tuple
9+
from typing import IO, Any, AnyStr, Iterable, Tuple
1010

1111
from nrrd.parsers import *
1212
from nrrd.types import IndexOrder, NRRDFieldMap, NRRDFieldType, NRRDHeader

nrrd/writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import zlib
55
from collections import OrderedDict
66
from datetime import datetime
7-
from typing import IO, Dict
7+
from typing import IO, Any, Dict
88

9-
import nptyping as npt
9+
import numpy.typing as npt
1010

1111
from nrrd.errors import NRRDError
1212
from nrrd.formatters import *

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
"Programming Language :: Python :: 3.11",
2323
]
2424

25-
dependencies = ["numpy >=1.11.1", "nptyping", "typing_extensions"]
25+
dependencies = ["numpy >= 1.21", "typing_extensions"]
2626

2727
[project.optional-dependencies]
2828
dev = ["build", "pre-commit", "pytest"]

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
numpy>=1.11.1
2-
nptyping
1+
numpy>=1.21
32
typing_extensions

0 commit comments

Comments
 (0)