Skip to content

Better behavior for Integer defaults #205

@jbednar

Description

@jbednar
class P(param.Parameterized):
    val = param.Integer(4.0)

Should either coerce the provided value into an integer (which would be safe in this case, but may not be desirable if e.g. provided 4.3), or it should have a sensible error message. Instead it results in:

ValueError                                Traceback (most recent call last)
<ipython-input-32-f6658de6e3bd> in <module>()
----> 1 class P(param.Parameterized):
      2     val = param.Integer(4.0)
      3 
      4 

<ipython-input-32-f6658de6e3bd> in P()
      1 class P(param.Parameterized):
----> 2     val = param.Integer(4.0)
      3 
      4 

~/anaconda/envs/ds/lib/python3.6/site-packages/param/__init__.py in __init__(self, default, bounds, softbounds, inclusive_bounds, **params)
    588         self.inclusive_bounds = inclusive_bounds
    589         self._softbounds = softbounds
--> 590         if not callable(default): self._check_value(default)
    591 
    592 

~/anaconda/envs/ds/lib/python3.6/site-packages/param/__init__.py in _check_value(self, val)
    750 
    751         if not isinstance(val,int):
--> 752             raise ValueError("Parameter '%s' must be an integer."%self._attrib_name)
    753 
    754         self._checkBounds(val)

ValueError: Parameter 'None' must be an integer.

All things considered, I think that since we are explicitly declaring it to be an Integer, we should try converting whatever we are given into an integer. In any case, we need to fix the broken attrib name here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: type/value stuffsystem of parameter type/value checking, inheritnace, etc etcstatus: discussionDiscussion. Not yet a specific feature/bug. Likely to result in multiple PRs/issues.type-featureFeature request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions