You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, when value_class is not object, then object.__new__(subcls) raises:
In [1]: class A:
...: def __new__(cls, value):
...: value_class = type(value)
...: subcls = type(f'A_{value_class.__name__}', (cls, value_class), {})
...: return object.__new__(subcls)
...:
In [2]: A(object())
Out[2]: <__main__.A_object at 0x7f9ce8a684c0>
In [3]: A(1.0)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-04f0da727658> in <module>
----> 1 A(1.0)
<ipython-input-5-3b6ef5f2aee2> in __new__(cls, value)
3 value_class = type(value)
4 subcls = type(f'A_{value_class.__name__}', (cls, value_class), {})
----> 5 return object.__new__(subcls)
6
TypeError: object.__new__(A_float) is not safe, use float.__new__()
This means that the intended subclassing there never happens, as it falls back to the generic object.__new__ in the exception handler. I came across this while trying to fix #19535 with a conditional method, but of course, that never worked because the code always ended up using the generic non-subclass.
I tried to fix this by doing super().__new__ or value_class.__new__ (and then some object.<method> needed to be super().<method> in other methods), but this runs into issues with other classes that also override __new__, namely MaskedArray.
I think it makes sense to create subclasses (and then fix it so it actually leaves out methods that shouldn't be there), but I'm not sure that multiple inheritance is the right way to do it.
Matplotlib version
Matplotlib version (import matplotlib; print(matplotlib.__version__)): master at the moment
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Bug report
Bug summary
In
basic_units.py
, theTaggedValue
has a__new__
method that does the following:matplotlib/examples/units/basic_units.py
Lines 120 to 128 in f710ad3
However, when
value_class
is notobject
, thenobject.__new__(subcls)
raises:This means that the intended subclassing there never happens, as it falls back to the generic
object.__new__
in the exception handler. I came across this while trying to fix #19535 with a conditional method, but of course, that never worked because the code always ended up using the generic non-subclass.I tried to fix this by doing
super().__new__
orvalue_class.__new__
(and then someobject.<method>
needed to besuper().<method>
in other methods), but this runs into issues with other classes that also override__new__
, namelyMaskedArray
.I think it makes sense to create subclasses (and then fix it so it actually leaves out methods that shouldn't be there), but I'm not sure that multiple inheritance is the right way to do it.
Matplotlib version
import matplotlib; print(matplotlib.__version__)
):master
at the momentprint(matplotlib.get_backend())
): n/aThe text was updated successfully, but these errors were encountered: