Skip to content

Commit 83c4830

Browse files
committed
add inverse check and fix repeated check
1 parent 9bcba56 commit 83c4830

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

Diff for: numpydoc/tests/test_validate.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def bad_parameter_spacing(self, a, b):
949949

950950
def no_documented_optional(self, a=5):
951951
"""
952-
Missing optional.
952+
Missing optional in docstring.
953953
954954
Parameters
955955
----------
@@ -958,6 +958,19 @@ def no_documented_optional(self, a=5):
958958
"""
959959
pass
960960

961+
def no_default_when_documented_optional(self, a, b):
962+
"""
963+
Missing default in signature.
964+
965+
Parameters
966+
----------
967+
a : int, optional
968+
One way to denote optional.
969+
b : int, default 5
970+
Another way to denote optional.
971+
"""
972+
pass
973+
961974

962975
class BadReturns:
963976
def return_not_documented(self):
@@ -1397,7 +1410,15 @@ def test_bad_generic_functions(self, capsys, func):
13971410
(
13981411
"BadParameters",
13991412
"no_documented_optional",
1400-
('Parameter "a" is optional but not documented',),
1413+
('Parameter "a" is optional but not documented, or vice versa',),
1414+
),
1415+
(
1416+
"BadParameters",
1417+
"no_default_when_documented_optional",
1418+
(
1419+
'Parameter "a" is optional but not documented, or vice versa',
1420+
'Parameter "b" is optional but not documented, or vice versa',
1421+
),
14011422
),
14021423
# Returns tests
14031424
("BadReturns", "return_not_documented", ("No Returns section found",)),

Diff for: numpydoc/validate.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"PR09": 'Parameter "{param_name}" description should finish with "."',
8080
"PR10": 'Parameter "{param_name}" requires a space before the colon '
8181
"separating the parameter name and type",
82-
"PR11": 'Parameter "{param_name}" is optional but not documented',
82+
"PR11": 'Parameter "{param_name}" is optional but not documented, ' "or vice versa",
8383
"RT01": "No Returns section found",
8484
"RT02": "The first line of the Returns section should contain only the "
8585
"type, unless multiple values are being returned",
@@ -583,7 +583,8 @@ def validate(obj_name):
583583

584584
for param, kind_desc in doc.doc_all_parameters.items():
585585
if not param.startswith("*"): # Check can ignore var / kwargs
586-
if not doc.parameter_type(param):
586+
param_type = doc.parameter_type(param)
587+
if not param_type:
587588
if ":" in param:
588589
errs.append(error("PR10", param_name=param.split(":")[0]))
589590
else:
@@ -593,13 +594,19 @@ def validate(obj_name):
593594
errs.append(error("PR05", param_name=param))
594595
# skip common_type_error checks when the param type is a set of
595596
# options
596-
if "{" in doc.parameter_type(param):
597+
if "{" in param_type:
597598
continue
598599
common_type_errors = [
599600
("integer", "int"),
600601
("boolean", "bool"),
601602
("string", "str"),
602603
]
604+
605+
# check that documented optional param has default in sig
606+
if "optional" in param_type or "default" in param_type:
607+
if param not in doc.optional_signature_parameters_names:
608+
errs.append(error("PR11", param_name=param))
609+
603610
for wrong_type, right_type in common_type_errors:
604611
if wrong_type in doc.parameter_type(param):
605612
errs.append(
@@ -611,13 +618,14 @@ def validate(obj_name):
611618
)
612619
)
613620

614-
for param in doc.optional_signature_parameters_names:
615-
type = doc.parameter_type(param)
616-
if "optional" not in type and "{" not in type and "default" not in type:
617-
errs.append(error("PR11", param_name=param))
618-
619621
errs.extend(_check_desc(kind_desc[1], "PR07", "PR08", "PR09", param_name=param))
620622

623+
# check param with default in sig is documented as optional
624+
for param in doc.optional_signature_parameters_names:
625+
type = doc.parameter_type(param)
626+
if "optional" not in type and "{" not in type and "default" not in type:
627+
errs.append(error("PR11", param_name=param))
628+
621629
if doc.is_function_or_method:
622630
if not doc.returns:
623631
if doc.method_returns_something:

0 commit comments

Comments
 (0)