8000 Merge pull request #10292 from eric-wieser/no-change-masked-shape · eric-wieser/numpy@dd866e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd866e3

Browse files
authored
Merge pull request numpy#10292 from eric-wieser/no-change-masked-shape
BUG: Masked singleton can be reshaped to be non-scalar
2 parents 5acb9b3 + 8b1b7f1 commit dd866e3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

numpy/ma/core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6313,6 +6313,18 @@ def copy(self, *args, **kwargs):
63136313
# precedent for this with `np.bool_` scalars.
63146314
return self
63156315

6316+
def __setattr__(self, attr, value):
6317+
if not self.__has_singleton():
6318+
# allow the singleton to be initialized
6319+
return super(MaskedConstant, self).__setattr__(attr, value)
6320+
elif self is self.__singleton:
6321+
raise AttributeError(
6322+
"attributes of {!r} are not writeable".format(self))
6323+
else:
6324+
# duplicate instance - we can end up here from __array_finalize__,
6325+
# where we set the __class__ attribute
6326+
return super(MaskedConstant, self).__setattr__(attr, value)
6327+
63166328

63176329
masked = masked_singleton = MaskedConstant()
63186330
masked_array = MaskedArray

numpy/ma/tests/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4981,6 +4981,10 @@ class Sub(type(np.ma.masked)): pass
49814981
assert_(a is not np.ma.masked)
49824982
assert_not_equal(repr(a), 'masked')
49834983

4984+
def test_attributes_readonly(self):
4985+
assert_raises(AttributeError, setattr, np.ma.masked, 'shape', (1,))
4986+
assert_raises(AttributeError, setattr, np.ma.masked, 'dtype', np.int64)
4987+
49844988

49854989
class TestMaskedWhereAliases(object):
49864990

0 commit comments

Comments
 (0)
0