param icon indicating copy to clipboard operation
param copied to clipboard

@param.output should enforce type

Open jbednar opened this issue 4 years ago • 0 comments

Generally, Param enforces declared types where feasible, but currently it does not do so for output types:

import param

class P(param.Parameterized):
    a = param.Number(default=5, bounds=(0, 10))
    b = param.Number(default=5, bounds=(0, 10))

    @param.output(param.String)
    def product(self):
        return self.a * self.b
    
p = P(a=2, b=3)
p.product()

>>> 6

This code declares that product() returns a string, but no error is raised when the method returns a number instead. Outputs are still useful as declaration, but it seems like we can make the decorator not just record the output type, but also validate it, raising an exception in the same way that supplying something of the wrong type to a or b would.

jbednar avatar Aug 19 '21 03:08 jbednar