-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Validation to only allow positive integers for options #27382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
def4005
e50961d
32e8c67
9618877
7a5593b
d6fa258
eb09813
8f46673
21bf599
ea09cc2
6c2ff43
25e0247
d18a01a
4e76640
80d2f1a
2d783ef
7163990
033339f
42d844e
1a25d06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -787,6 +787,7 @@ def is_instance_factory(_type): | |
ValueError if x is not an instance of `_type` | ||
|
||
""" | ||
|
||
if isinstance(_type, (tuple, list)): | ||
_type = tuple(_type) | ||
type_repr = "|".join(map(str, _type)) | ||
|
@@ -820,6 +821,31 @@ def inner(x): | |
return inner | ||
|
||
|
||
def is_pos_int(value): | ||
""" | ||
Verify that value is None or a positive int. | ||
|
||
Parameters | ||
---------- | ||
value : None or int | ||
The `value` to be checked. | ||
|
||
Raises | ||
Adam-Klaum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
------ | ||
ValueError if the value is not equal to a positive integer, 0 or None. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be formatted like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, 0 is allowed, and is necessary for existing internal code. I think you are right, at this point "is_pos_int" is technically not an accurate name. I'm fine with "is_nonnegative_int". The docstring change seems fine to me as well. I'll make the changes and do another push. |
||
""" | ||
|
||
if value is None: | ||
return | ||
|
||
elif isinstance(value, int): | ||
if value >= 0: | ||
return | ||
|
||
msg = "Value must be a positive integer, 0 or None" | ||
raise ValueError(msg) | ||
|
||
|
||
# common type validators, for convenience | ||
# usage: register_option(... , validator = is_int) | ||
is_int = is_type_factory(int) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use double backticks around None. use
:meth:`set_option`