typedecorator icon indicating copy to clipboard operation
typedecorator copied to clipboard

Typechecking functions accepting classes (as opposed to instances)

Open mejedi opened this issue 10 years ago • 2 comments

A little feature request here: it would be great if it was possible to validate a class passed via function parameter using issubclass().

mejedi avatar Dec 31 '14 15:12 mejedi

Thanks for the suggestion @mejedi . It certainly looks like a useful use-case. I'm trying to think of a good way to represent this check (as opposed to the instance chek) in the signature, any ideas?

It also might be possible to cover this use-case if #5 is implemented, so you could do arbitrary checks, eg:

def subclass(base):
    return lambda cls: isinstance(cls, type) and issubclass(cls, base)


@params(cls=subclass(MyClass))
def myfunc(cls):
    ...

Thoughts?

senko avatar Jan 01 '15 21:01 senko

Thoughts?

The library is already capable of expressing complex assertions in function signatures, I mean Nullable and Union. Extending this with a Class is probably not too ugly.

mejedi avatar Jan 07 '15 00:01 mejedi