Skip to content

Commit

Permalink
Merge pull request #55 from ramonhagenaars/Release/1.4.4
Browse files Browse the repository at this point in the history
Fix for 0d arrays
  • Loading branch information
ramonhagenaars committed Sep 10, 2021
2 parents d6c1bc5 + c3c4a96 commit a825b7b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
History
-------

1.4.4 (2021-09-10)
++++++++++++++++++

- Fixed instance checks with 0d arrays.

1.4.3 (2021-08-05)
++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/ramonhagenaars/nptyping/branch/master/graph/badge.svg)](https://codecov.io/gh/ramonhagenaars/nptyping)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ramonhagenaars/nptyping/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ramonhagenaars/nptyping/?branch=master)

Type hints for `Numpy`!
Type hints with dynamic checks for `Numpy`!

<p align='center'>
<a href='https://https://pypi.org/project/nptyping/'>
Expand Down
2 changes: 1 addition & 1 deletion nptyping/_meta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__title__ = 'nptyping'
__version__ = '1.4.3'
__version__ = '1.4.4'
__author__ = 'Ramon Hagenaars'
__author_email__ = '[email protected]'
__description__ = 'Type hints for Numpy.'
Expand Down
3 changes: 2 additions & 1 deletion nptyping/functions/_get_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def _get_type_dtype(dtype: numpy.dtype) -> Type['NPType']:
def _get_type_arrary(arr: numpy.ndarray) -> Type['NPType']:
# Return the nptyping type of a numpy array.
type_ = get_type(arr.dtype)
return NDArray[arr.shape, type_]
shape = arr.shape or (Any, ...)
return NDArray[shape, type_]


def _get_type_of_number(
Expand Down
13 changes: 9 additions & 4 deletions nptyping/types/_ndarray_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ def __instancecheck__(cls, instance: Any) -> bool:
"""
from nptyping.functions._get_type import get_type
np_type = get_type(instance)
return (np_type.__name__ == cls.__name__
and _NDArrayMeta.__subclasscheck__(
cls, _NDArray[instance.shape, instance.dtype]))
return (
np_type.__name__ == cls.__name__ and
_NDArrayMeta.__subclasscheck__(
cls,
_NDArray[instance.shape or (Any, ...), instance.dtype]
)
)

def __subclasscheck__(cls, subclass: type) -> bool:
"""
Expand Down Expand Up @@ -171,7 +175,8 @@ def _sizes_and_type(


class _NDArray(NPType, metaclass=_NDArrayMeta):
_shape = (Any, ...) # type: Union[Tuple[int, ...], Tuple[Any, EllipsisType]] # noqa # pylint: disable=line-too-long
_shape = (
Any, ...) # type: Union[Tuple[int, ...], Tuple[Any, EllipsisType]] # noqa # pylint: disable=line-too-long
_type = Any
_special = True # Added to be able to compile types with sphinx.

Expand Down
5 changes: 5 additions & 0 deletions tests/test_types/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ def test_instance_check_with_object(self):
obj_arr = NDArray[(Any,), object]
ones = np.ones(shape=(5,))
self.assertIsInstance(ones, obj_arr)

def test_validate_0DArray(self):
self.assertIsInstance(np.array(1), NDArray)
self.assertIsInstance(np.array(1), NDArray[int])
self.assertNotIsInstance(np.array(1), NDArray[str])

0 comments on commit a825b7b

Please sign in to comment.