Skip to content

BUG: Partial fix for docstring validation for parameters #28765

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

Merged
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
79e59f4
Use signature instead of getfullargspec
ChiefMilesEdgeworth Oct 2, 2019
d65e26c
Account for args and kwargs on same line
ChiefMilesEdgeworth Oct 3, 2019
9bff615
Styling Changes
ChiefMilesEdgeworth Oct 3, 2019
3814799
BUG: Use signature instead of getfullargspec
ChiefMilesEdgeworth Oct 2, 2019
271defc
BUG: Account for args and kwargs on same line
ChiefMilesEdgeworth Oct 3, 2019
7095b99
Styling Changes
ChiefMilesEdgeworth Oct 3, 2019
294b116
DOC: Idxmin/max fixed
ChiefMilesEdgeworth Oct 3, 2019
94bd76b
Merge remote-tracking branch 'origin/fix_docstring_validation' into f…
ChiefMilesEdgeworth Oct 3, 2019
adc77e8
BUG: Fixed desc being loaded as list instead of str
ChiefMilesEdgeworth Oct 3, 2019
877ebb3
CLN: Missed styling changes
ChiefMilesEdgeworth Oct 3, 2019
5a13341
TST: Added tests for decorated functions
ChiefMilesEdgeworth Oct 4, 2019
7d420be
CLN: Missed whitespace changes
ChiefMilesEdgeworth Oct 4, 2019
17176c2
TST: Revert change to arg handling
ChiefMilesEdgeworth Oct 7, 2019
5263577
Merge pull request #1 from pandas-dev/master
ChiefMilesEdgeworth Oct 7, 2019
b1b7a34
Better test and readability improvement
ChiefMilesEdgeworth Oct 8, 2019
3b6f88c
Merge remote-tracking branch 'origin/master'
ChiefMilesEdgeworth Oct 8, 2019
5a236f3
Fixed PEP8 Requirements
ChiefMilesEdgeworth Oct 8, 2019
894f7d1
Merge remote-tracking branch 'upstream/master'
ChiefMilesEdgeworth Oct 10, 2019
d687909
Merge remote-tracking branch 'upstream/master'
ChiefMilesEdgeworth Oct 11, 2019
fed3b70
Merge branch 'master' into fix_docstring_validation
ChiefMilesEdgeworth Oct 12, 2019
449d5d1
Remove bad parameter test
ChiefMilesEdgeworth Oct 13, 2019
c9bdb28
Use lru_cache instead of custom decorator
ChiefMilesEdgeworth Oct 13, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions scripts/tests/test_validate_docstrings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import wraps
import io
import random
import string
Expand All @@ -12,6 +13,24 @@
validate_one = validate_docstrings.validate_one


class Decorators:
@staticmethod
def good_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
"""
Wrapper function.

This docstring should be hidden, and not used during validation.
Using the @wraps decorator should allow validation to get the
signature of the wrapped function when comparing against the
docstring.
"""
return func(*args, **kwargs)

return wrapper


class GoodDocStrings:
"""
Collection of good doc strings.
Expand Down Expand Up @@ -68,6 +87,23 @@ def sample(self):
"""
return random.random()

@Decorators.good_decorator
def decorated_sample(self, max):
"""
Generate and return a random integer between 0 and max.

Parameters
----------
max : int
The maximum value of the random number.

Returns
-------
int
Random number generated.
"""
return random.randint(0, max)

def random_letters(self):
"""
Generate and return a sequence of random letters.
Expand Down Expand Up @@ -870,6 +906,7 @@ def test_good_class(self, capsys):
"plot",
"swap",
"sample",
"decorated_sample",
"random_letters",
"sample_values",
"head",
Expand Down
24 changes: 17 additions & 7 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,24 +430,34 @@ def doc_parameters(self):

@property
def signature_parameters(self):
def add_stars(param_name: str, info: inspect.Parameter):
"""
Add stars to *args and **kwargs parameters
"""
if info.kind == inspect.Parameter.VAR_POSITIONAL:
return f"*{param_name}"
elif info.kind == inspect.Parameter.VAR_KEYWORD:
return f"**{param_name}"
else:
return param_name

if inspect.isclass(self.obj):
if hasattr(self.obj, "_accessors") and (
self.name.split(".")[-1] in self.obj._accessors
):
# accessor classes have a signature but don't want to show this
return tuple()
try:
sig = inspect.getfullargspec(self.obj)
sig = inspect.signature(self.obj)
except (TypeError, ValueError):
# Some objects, mainly in C extensions do not support introspection
# of the signature
return tuple()
params = sig.args
if sig.varargs:
params.append("*" + sig.varargs)
if sig.varkw:
params.append("**" + sig.varkw)
params = tuple(params)

params = tuple(
add_stars(parameter, sig.parameters[parameter])
for parameter in sig.parameters
)
if params and params[0] in ("self", "cls"):
return params[1:]
return params
Expand Down