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

Conversation

eric-wieser
Copy link
Member

It's possible this is the cause of gh-10270, where it seems something is wrong with the shape

@eric-wieser
Copy link
Member Author

This is less good a fix than intended, and still doesn't stop assigning the shape of np.ma.masked.mask - that will come in a later PR, as it's more dangerous.

numpy/ma/core.py Outdated
def __setattr__(self, attr, value):
if self is self.__singleton:
raise MaskError("Cannot change attributes of the masked singleton")
super(MaskedConstant, self).__setattr__(attr, value)
Copy link
Member

Choose a reason for hiding this comment

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

Is this functionality needed? Maybe a note.

Copy link
Member Author

Choose a reason for hiding this comment

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

It stops it from breaking subclasses, if anyone else is brave enough to do that (@mhvk?)

Copy link
Contributor

Choose a reason for hiding this comment

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

No, while we do subclass MaskedArray, even we don't subclass this (as far as I know).

Copy link
Member Author

Choose a reason for hiding this comment

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

Update: yes, this is needed in order to reassign __class__ of duplicate instances in __array_finalize__

It's possible this is the cause of numpygh-10270, where it seems something is wrong with the shape
@eric-wieser eric-wieser force-pushed the no-change-masked-shape branch from 51ca981 to 8b1b7f1 Compare January 1, 2018 17:27
@@ -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.

@@ -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.

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

Seems all OK

@charris charris merged commit dd866e3 into numpy:master Jan 2, 2018
@charris
Copy link
Member

charris commented Jan 2, 2018

Thanks Eric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants