Skip to content

Commit

Permalink
Fixed bug that ndarrays were considered to be instances of RecArray. (#…
Browse files Browse the repository at this point in the history
…76)

* Fixed bug that ndarrays were considered to be instances of RecArray.

* Version bump.

Co-authored-by: Ramon <p8u7wAPC5Pg9HYkkCkzA>
  • Loading branch information
ramonhagenaars committed Jun 1, 2022
1 parent 630f32b commit a95dd01
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## 2.1.1 (2022-06-01)

- Fixed bug that numpy ndarrays were incorrectly instance checked against `RecArray`.

## 2.1.0 (2022-06-01)

- Added `Structure` and "structure expressions" to support structured arrays.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ to express them.
>>> from nptyping import RecArray

>>> arr = np.array([("Peter", 34)], dtype=[("name", "U10"), ("age", "i4")])
>>> isinstance(arr, RecArray[Any, Structure["name: Str, age: Int"]])
>>> rec_arr = arr.view(np.recarray)
>>> isinstance(rec_arr, RecArray[Any, Structure["name: Str, age: Int"]])
True

```
Expand Down
2 changes: 1 addition & 1 deletion nptyping/package_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
"""
__title__ = "nptyping"
__version__ = "2.1.0"
__version__ = "2.1.1"
__author__ = "Ramon Hagenaars"
__author_email__ = "[email protected]"
__description__ = "Type hints for NumPy."
Expand Down
9 changes: 9 additions & 0 deletions nptyping/recarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"""
from typing import Any, Tuple

import numpy as np

from nptyping.error import InvalidArgumentsError
from nptyping.ndarray import NDArray, NDArrayMeta
from nptyping.structure import Structure
Expand All @@ -47,6 +49,13 @@ def _get_dtype(cls, dtype_candidate: Any) -> DType:
)
return dtype_candidate

def __instancecheck__( # pylint: disable=bad-mcs-method-argument
self, instance: Any
) -> bool:
return isinstance(instance, np.recarray) and NDArrayMeta.__instancecheck__(
self, instance
)


class RecArray(NDArray, metaclass=RecArrayMeta):
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/test_recarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
class RecArrayTest(TestCase):
def test_isinstance_succeeds_if_shape_and_structure_match(self):
arr = np.array([("William", 23)], dtype=[("name", "U8"), ("age", "i4")])

self.assertNotIsInstance(arr, RecArray)

rec_arr = arr.view(np.recarray)

self.assertIsInstance(rec_arr, RecArray)
Expand Down

0 comments on commit a95dd01

Please sign in to comment.