-
-
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 5 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,27 @@ def inner(x): | |
return inner | ||
|
||
|
||
def is_pos_int(value): | ||
""" | ||
Raises ValueError if type(x) is not equal to None or int, or if x is not | ||
positive in the case of a int type. | ||
|
||
|
||
Parameters | ||
---------- | ||
value - the value to be checked | ||
""" | ||
|
||
if not (value is None or isinstance(value, int)): | ||
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. Use Can you restructure this a bit? if value is None:
return value
elif isinstance(value, numbers.Integral):
if value < 0:
raise
return value
raise ValueError(...) |
||
msg = "Value must be an instance of <class 'NoneType'>|<class 'int'>" | ||
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 most people won't be familiar with NoneType. Should probably say like |
||
raise ValueError(msg) | ||
|
||
elif isinstance(value, int): | ||
if value < 0: | ||
msg = "int values must be positive for this option" | ||
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.
@jreback comment for returns section is lost in diff so re-commenting here but I'm surprised this isn't failing the CI either
@datapythonista do we ignore private docstrings in the validation? Vaguely recall some comment around that but didn't see code to confirm
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.
Sorry, shall I add the following?
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.
@WillAyd yes, we don't validate docstrings of private functions. Ideally we should validate everything, but with all the pending work in the public API that has an impact in the web docs, I don't think we should spend time in fixing the private docstrings yet (and if we don't fix the existing cases, we can't easily validate the new ones).
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.
Thanks for the clarification - do you know where that exclusion happens? Didn't see it in code_checks and couldn't find on quick glance in validate_docstrings.py
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.
Ups sorry, was going to mention and I forgot. It's not that we exclude them, it's that we get the docstrings to consider from the
.rst
files of the API docs. The code here is a good starting point to understand that: https://github.com/pandas-dev/pandas/blob/master/scripts/validate_docstrings.py#L873There 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.
@Adam-Klaum these need to be updated to follow the doctoring stander. See https://dev.pandas.io/development/contributing.html#updating-a-pandas-docstring