Skip to content

Commit

Permalink
Clarified step error messages and other docs and links (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbednar authored Feb 11, 2022
1 parent 3784a1d commit fb6f4d9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc/comparisons.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ In practice, most programmers simply skip validation or implement it only partia

## Runtime checking

Python 3 type annotations allow users to specify types for attributes and function returns, but these types are not normally checked at runtime, and so they do not have the same role of validating user input or programmer error as the type declarations in Params, Traits, Traitlets, pydantic, and attr. They also are limited to the type, so they cannot enforce constraints on range ('state' must be in the list ['Alabama', 'Alaska',...]). Thus even if type hinting is used, programmers still need to write code to actually validate the inputs to functions and methods, which is the role of packages like Param and Traitlets. Note that Pydantic focuses on [generating valid outputs at runtime](https://github.com/samuelcolvin/pydantic/issues/578focused) rather than detecting invalid inputs, so it may or may not be suitable for the same types of applications as the other libraries discussed here.
Python 3 type annotations allow users to specify types for attributes and function returns, but these types are not normally checked at runtime, and so they do not have the same role of validating user input or programmer error as the type declarations in Params, Traits, Traitlets, pydantic, and attr. They also are limited to the type, so they cannot enforce constraints on range ('state' must be in the list ['Alabama', 'Alaska',...]). Thus even if type hinting is used, programmers still need to write code to actually validate the inputs to functions and methods, which is the role of packages like Param and Traitlets. Note that Pydantic focuses on [generating valid outputs at runtime](https://github.com/samuelcolvin/pydantic/issues/578) rather than detecting invalid inputs, so it may or may not be suitable for the same types of applications as the other libraries discussed here.


## Generality and ease of integration with your project
Expand Down
2 changes: 0 additions & 2 deletions doc/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ or
pip install param
```

The very latest changes can be obtained via `conda install -c pyviz/label/dev param` or `pip install https://github.com/ioam/param/archive/master.zip`.

## Using Param to get simple, robust code

The `param` library gives you Parameters, which are used in Parameterized classes.
Expand Down
10 changes: 5 additions & 5 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def _validate_value(self, val, allow_None):

def _validate_step(self, val, step):
if step is not None and not _is_number(step):
raise ValueError("Step parameter can only be None or a "
raise ValueError("Step can only be None or a "
"numeric value, not type %r." % type(step))

def _validate(self, val):
Expand Down Expand Up @@ -936,7 +936,7 @@ def _validate_value(self, val, allow_None):

def _validate_step(self, val, step):
if step is not None and not isinstance(step, int):
raise ValueError("Step parameter can only be None or an "
raise ValueError("Step can only be None or an "
"integer value, not type %r" % type(step))


Expand Down Expand Up @@ -1931,7 +1931,7 @@ def _validate_value(self, val, allow_None):
def _validate_step(self, val, step):
if step is not None and not isinstance(step, dt_types):
raise ValueError(
"Step parameter can only be None, a datetime "
"Step can only be None, a datetime "
"or datetime type, not type %r." % type(val)
)

Expand All @@ -1952,7 +1952,7 @@ def deserialize(cls, value):

class CalendarDate(Number):
"""
CalendarDate parameter of date type.
Parameter specifically allowing dates (not datetimes).
"""

def __init__(self, default=None, **kwargs):
Expand All @@ -1971,7 +1971,7 @@ def _validate_value(self, val, allow_None):

def _validate_step(self, val, step):
if step is not None and not isinstance(step, dt.date):
raise ValueError("Step parameter can only be None or a date type.")
raise ValueError("Step can only be None or a date type.")

@classmethod
def serialize(cls, value):
Expand Down
8 changes: 4 additions & 4 deletions tests/API1/testnumberparameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ class Q(param.Parameterized):
self.assertEqual(qobj.param['q'].step, 0.5)

def test_step_invalid_type_number_parameter(self):
exception = "Step parameter can only be None or a numeric value"
exception = "Step can only be None or a numeric value"
with self.assertRaisesRegex(ValueError, exception):
param.Number(step='invalid value')

def test_step_invalid_type_integer_parameter(self):
exception = "Step parameter can only be None or an integer value"
exception = "Step can only be None or an integer value"
with self.assertRaisesRegex(ValueError, exception):
param.Integer(step=3.4)

def test_step_invalid_type_datetime_parameter(self):
exception = "Step parameter can only be None, a datetime or datetime type"
exception = "Step can only be None, a datetime or datetime type"
with self.assertRaisesRegex(ValueError, exception):
param.Date(dt.datetime(2017,2,27), step=3.2)

def test_step_invalid_type_date_parameter(self):
exception = "Step parameter can only be None or a date type"
exception = "Step can only be None or a date type"
with self.assertRaisesRegex(ValueError, exception):
param.CalendarDate(dt.date(2017,2,27), step=3.2)

0 comments on commit fb6f4d9

Please sign in to comment.