8000 Object casts convert to python type (which makes some operations work magically) · Issue #16876 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Object casts convert to python type (which makes some operations work magically) #16876

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
seberg opened this issue Jul 15, 2020 · 1 comment

Comments

@seberg
Copy link
Member
seberg commented Jul 15, 2020

Consider the NumPy float32:

f32 = np.float32(2)
type(f32.astype(object))  # gives `float`

which is fair enough in a sense. Our scalars are weird...

This also makes some stranger things work though:

class myf:
   def __radd__(self, other):
       return f"called backward with {type(other)}"

f32 + myf()
# "called backward with <class 'float'>"

This works by first dispatching to the ufunc machinery, which converts both to arrays, which then casts f32 to object and then calls the full operator (both ways in principle).

Now, that would enter a recursive call of course if it would not convert to the float32 to a python float, so that:

myf() + np.float128()

segfaults due to infinite recursion I guess. If you swap the order, things are more OK, although it probably crashes just as much if np.float128 was instead a subclass of float128...

@seberg
Copy link
Member Author
seberg commented Jul 15, 2020

So there are actually two things here:

  1. Our scalar binop logic is shady
  2. Discuss whether converting float32 to float is actually correct for object casts? Although I guess that we are stuck with that...

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

No branches or pull requests

1 participant
0