-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
Conversation
This is less good a fix than intended, and still doesn't stop assigning the shape of |
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
51ca981
to
8b1b7f1
Compare
@@ -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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, no big deal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems all OK
Thanks Eric. |
It's possible this is the cause of gh-10270, where it seems something is wrong with the shape