Skip to content

basic units example's subclassing is broken #19841

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

Open
QuLogic opened this issue Apr 1, 2021 · 2 comments
Open

basic units example's subclassing is broken #19841

QuLogic opened this issue Apr 1, 2021 · 2 comments

Comments

@QuLogic
Copy link
Member

QuLogic commented Apr 1, 2021

Bug report

Bug summary

In basic_units.py, the TaggedValue has a __new__ method that does the following:

def __new__(cls, value, unit):
# generate a new subclass for value
value_class = type(value)
try:
subcls = type(f'TaggedValue_of_{value_class.__name__}',
(cls, value_class), {})
return object.__new__(subcls)
except TypeError:
return object.__new__(cls)

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
  • Matplotlib backend (print(matplotlib.get_backend())): n/a
  • Python version: 3.7.6
Copy link

github-actions bot commented Dec 8, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Dec 8, 2023
@QuLogic
Copy link
Member Author

QuLogic commented Dec 8, 2023

This might be something for @ksunden to check on.

@github-actions github-actions bot removed the status: inactive Marked by the “Stale” Github Action label Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant