Skip to content

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

Open

Description

@seberg

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

Activity

  1. seberg commented on Jul 15, 2020

    @seberg
    MemberAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions