Skip to content

BUG: Masked singleton can be reshaped to be non-scalar #10292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6309,6 +6309,18 @@ def copy(self, *args, **kwargs):
# precedent for this with `np.bool_` scalars.
return self

def __setattr__(self, attr, value):
if not self.__has_singleton():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we combine the two to if self.__has_singleton() and self is self.__singleton? would imply longer comment but no duplicated code.

Copy link
Member Author

@eric-wieser eric-wieser Jan 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote it this way because the rationale for the two cases is different (even if it leads to the same result), and so the code duplication is perhaps justifiable - it may be desirable to only allow the __class__ attribute to be set in the last branch, for instance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, no big deal.

# allow the singleton to be initialized
return super(MaskedConstant, self).__setattr__(attr, value)
elif self is self.__singleton:
raise AttributeError(
"attributes of {!r} are not writeable".format(self))
else:
# duplicate instance - we can end up here from __array_finalize__,
# where we set the __class__ attribute
return super(MaskedConstant, self).__setattr__(attr, value)


masked = masked_singleton = MaskedConstant()
masked_array = MaskedArray
Expand Down
4 changes: 4 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4981,6 +4981,10 @@ class Sub(type(np.ma.masked)): pass
assert_(a is not np.ma.masked)
assert_not_equal(repr(a), 'masked')

def test_attributes_readonly(self):
assert_raises(AttributeError, setattr, np.ma.masked, 'shape', (1,))
assert_raises(AttributeError, setattr, np.ma.masked, 'dtype', np.int64)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous commit had MaskError, but this seems more useful for code that does:

try:
    x.shape = (1, 2)
except AttributeError:
    x = array(x)
    x.shape = (1, 2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, better.



class TestMaskedWhereAliases(object):

Expand Down