|
| 1 | +from functools import wraps |
1 | 2 | import io
|
2 | 3 | import random
|
3 | 4 | import string
|
|
12 | 13 | validate_one = validate_docstrings.validate_one
|
13 | 14 |
|
14 | 15 |
|
| 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 | + |
15 | 49 | class GoodDocStrings:
|
16 | 50 | """
|
17 | 51 | Collection of good doc strings.
|
@@ -53,6 +87,21 @@ def sample(self):
|
53 | 87 | """
|
54 | 88 | return random.random()
|
55 | 89 |
|
| 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 | + |
56 | 105 | def random_letters(self):
|
57 | 106 | """
|
58 | 107 | Generate and return a sequence of random letters.
|
@@ -634,6 +683,19 @@ def list_incorrect_parameter_type(self, kind):
|
634 | 683 | """
|
635 | 684 | pass
|
636 | 685 |
|
| 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 | + |
637 | 699 |
|
638 | 700 | class BadReturns:
|
639 | 701 | def return_not_documented(self):
|
@@ -828,6 +890,7 @@ def test_good_class(self, capsys):
|
828 | 890 | [
|
829 | 891 | "plot",
|
830 | 892 | "sample",
|
| 893 | + "decorated_sample", |
831 | 894 | "random_letters",
|
832 | 895 | "sample_values",
|
833 | 896 | "head",
|
@@ -1002,6 +1065,7 @@ def test_bad_generic_functions(self, capsys, func):
|
1002 | 1065 | "list_incorrect_parameter_type",
|
1003 | 1066 | ('Parameter "kind" type should use "str" instead of "string"',),
|
1004 | 1067 | ),
|
| 1068 | + ("BadParameters", "bad_decorator", ("Parameters {*args} not documented",)), |
1005 | 1069 | pytest.param(
|
1006 | 1070 | "BadParameters",
|
1007 | 1071 | "blank_lines",
|
|
0 commit comments