param icon indicating copy to clipboard operation
param copied to clipboard

Interaction between `param.rx` and `isinstance`

Open LecrisUT opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

I couldn't find an relevant information on how to do an isinstance check of the inner object of the param.rx. Right now I have:

            if isinstance(field, param.rx):
                if field.rx.pipe(isinstance, xarray.DataArray):
                    curr_data = field

This could work, but feels a bit clunky. It would be much nicer if isinstance could work without having to check if it's a param.rx and then use pipe, e.g. by creating a dynamic class that inherits the type of the first definition, or any other way to override isinstance checks.

Describe alternatives you've considered

I have of course tried if it already works, but it doesn't seem to

import param
a = param.rx("string")
isinstance(a, str)

but this doesn't work

LecrisUT avatar Jul 11 '24 18:07 LecrisUT

Good question! Would you be happy with a.rx.isinstance(str)? I think that would be the natural approach here, though we'd need to add that method.

jbednar avatar Jul 14 '24 22:07 jbednar

As a clutch, it does help. The preferred interface would be to not have to check for isinstance(_, param.rx). Unfortunately the only method that I know would work is like attrs, but it is quite a redactor for that.

I'm still getting a hang of the .rx interface. I guess neither approach will work with if.

LecrisUT avatar Jul 15 '24 06:07 LecrisUT

I'd prefer if isinstance resolved to the underlying value.

maximlt avatar Jul 15 '24 14:07 maximlt