Only allow some certain class with param.ClassSelector() but not imported

import as ccrs
crs = param.ClassSelector(class_=[str, ccrs])

Is it possible to only allow ccrs objects without importing?

Here I assume you mean without importing Cartopy in this file, as it presumably had to be imported in some other context for you to have an object of that type. I.e., it sounds like you’re wanting ClassSelector to work the same as param.DataFrame, which only imports DataFrame when that class is instantiated, not when it is defined? Or maybe, further, you even want to be able to instantiate the Parameter and have it validate the type, without ever importing? Either way, yes, I think it should be possible to extend ClassSelector to support that, or inherit from it to override that behavior. It would be tricky to write, and not currently supported but seems doable and useful!

Probably simpler is just to write your own CRS parameter class that works like DataFrame does; easier than supporting all the logic of ClassSelector (but less general, of course!).

1 Like

Ah didn’t even realize DataFrame does that, but thanks for the advice

1 Like

I think something like:

class Cartopy(param.ClassSelector):
    def __init__(self, default=None, **params):
        import as ccrs
        objects = tuple(
            obj for name, obj in vars(ccrs).items()
            if isinstance(obj, type) and issubclass(obj, ccrs.Projection) and
            not name.startswith('_') and name not in ['Projection']
        super(Cartopy, self).__init__(objects, **params)