Skip to content

Commit 5a13341

Browse files
TST: Added tests for decorated functions
Tests added to test_validate_docstrings that check that it handles decorated functions as predicted.
1 parent 877ebb3 commit 5a13341

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

scripts/tests/test_validate_docstrings.py

+64
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import wraps
12
import io
23
import random
34
import string
@@ -12,6 +13,39 @@
1213
validate_one = validate_docstrings.validate_one
1314

1415

16+
class Decorators:
17+
@staticmethod
18+
def good_decorator(func):
19+
@wraps(func)
20+
def wrapper(*args, **kwargs):
21+
"""
22+
Wrapper function.
23+
24+
This docstring should be hidden, and not used during validation.
25+
Using the @wraps decorator should allow validation to get the
26+
signature of the wrapped function when comparing against the
27+
docstring.
28+
"""
29+
return func(*args, **kwargs)
30+
31+
return wrapper
32+
33+
@staticmethod
34+
def missing_wraps(func):
35+
def wrapper(*args):
36+
"""
37+
Wrapper function.
38+
39+
The signature of this wrapper should override the underlying
40+
function. So, we should see errors regarding the parameters,
41+
like expecting documentation for *args and **kwargs and missing
42+
documentation for the underlying signature.
43+
"""
44+
return func(*args)
45+
46+
return wrapper
47+
48+
1549
class GoodDocStrings:
1650
"""
1751
Collection of good doc strings.
@@ -53,6 +87,21 @@ def sample(self):
5387
"""
5488
return random.random()
5589

90+
@Decorators.good_decorator
91+
def decorated_sample(self):
92+
"""
93+
Generate and return a random number.
94+
95+
The value is sampled from a continuous uniform distribution between
96+
0 and 1.
97+
98+
Returns
99+
-------
100+
float
101+
Random number generated.
102+
"""
103+
return random.random()
104+
56105
def random_letters(self):
57106
"""
58107
Generate and return a sequence of random letters.
@@ -634,6 +683,19 @@ def list_incorrect_parameter_type(self, kind):
634683
"""
635684
pass
636685

686+
@Decorators.missing_wraps
687+
def bad_decorator(self, kind: bool):
688+
"""
689+
The decorator is missing the @wraps, and overrides the signature
690+
of this method.
691+
692+
Parameters
693+
----------
694+
kind : bool
695+
Foo bar baz
696+
"""
697+
pass
698+
637699

638700
class BadReturns:
639701
def return_not_documented(self):
@@ -828,6 +890,7 @@ def test_good_class(self, capsys):
828890
[
829891
"plot",
830892
"sample",
893+
"decorated_sample",
831894
"random_letters",
832895
"sample_values",
833896
"head",
@@ -1002,6 +1065,7 @@ def test_bad_generic_functions(self, capsys, func):
10021065
"list_incorrect_parameter_type",
10031066
('Parameter "kind" type should use "str" instead of "string"',),
10041067
),
1068+
("BadParameters", "bad_decorator", ("Parameters {*args} not documented",)),
10051069
pytest.param(
10061070
"BadParameters",
10071071
"blank_lines",

0 commit comments

Comments
 (0)