diff options
| author | Sebastian Berg <sebastianb@nvidia.com> | 2023-01-27 19:49:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-27 19:49:56 +0100 |
| commit | 2aa0bf6973b709724624db8e8a51d8efbdeb5d22 (patch) | |
| tree | 767cd9283d36154dbffff38e0a300ce6862b42f7 /numpy | |
| parent | 41499995a4c532556b2b6d6e3c0fabb0a7bdb61a (diff) | |
| parent | 51cfe4460d65242051c36894a26f47103d3e10f8 (diff) | |
| download | numpy-2aa0bf6973b709724624db8e8a51d8efbdeb5d22.tar.gz | |
Merge pull request #23113 from hmaarrfk/slots_for_mixins
ENH: Add slots to NDArrayOperatorsMixin allowing them in subclasses
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/lib/mixins.py | 1 | ||||
| -rw-r--r-- | numpy/ma/tests/test_subclassing.py | 10 |
2 files changed, 11 insertions, 0 deletions
diff --git a/numpy/lib/mixins.py b/numpy/lib/mixins.py index c81239f6b..117cc7851 100644 --- a/numpy/lib/mixins.py +++ b/numpy/lib/mixins.py @@ -133,6 +133,7 @@ class NDArrayOperatorsMixin: .. versionadded:: 1.13 """ + __slots__ = () # Like np.ndarray, this mixin class implements "Option 1" from the ufunc # overrides NEP. diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index 64c66eeb9..e3c885253 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -154,6 +154,7 @@ class WrappedArray(NDArrayOperatorsMixin): ufunc deferrals are commutative. See: https://github.com/numpy/numpy/issues/15200) """ + __slots__ = ('_array', 'attrs') __array_priority__ = 20 def __init__(self, array, **attrs): @@ -448,3 +449,12 @@ class TestClassWrapping: assert_(isinstance(np.divide(wm, m2), WrappedArray)) assert_(isinstance(np.divide(m2, wm), WrappedArray)) assert_equal(np.divide(m2, wm), np.divide(wm, m2)) + + def test_mixins_have_slots(self): + mixin = NDArrayOperatorsMixin() + # Should raise an error + assert_raises(AttributeError, mixin.__setattr__, "not_a_real_attr", 1) + + m = np.ma.masked_array([1, 3, 5], mask=[False, True, False]) + wm = WrappedArray(m) + assert_raises(AttributeError, wm.__setattr__, "not_an_attr", 2) |
