Skip to content

Commit

Permalink
fix(regex): fix re_only_desc and add lookaheads
Browse files Browse the repository at this point in the history
The `re_only_desc` regex did not match for white and characters after
`\n`, so some description-only lines weren't getting matched. In
addition, lookaheads were added to `re_param_line` (i.e. make sure the
type group is not followed by a new line (`\n`)). Lastly, named groups
(ala Perl regular expressions) were added for slightly improved clarity.

The hyperlink in the body of test `regression_6211` was causing
problems, so this was moved into a reStructuredText directive.

Finally, the added

    `from __future__ import annotations`

moved all lines in

	`tests/functional/ext/docparams/docparams.py`

down one line, so the line numbers in `docparams.txt` were off by 1,
causing tests to fail. This was corrected.
  • Loading branch information
adam-grant-hendry committed Aug 27, 2022
1 parent b241a92 commit 3aaba09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
25 changes: 15 additions & 10 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,10 @@ class NumpyDocstring(GoogleDocstring):

re_param_line = re.compile(
rf"""
\s* (\*{{0,2}}\w+)(\s?(:|\n)) # identifier with potential asterisks
\s* (?P<param_name>\*{{0,2}}\w+)(\s?(:|\n)) # identifier with potential asterisks
\s*
(
(?P<param_type>
(?!=\n)
(
({GoogleDocstring.re_multiple_type}) # default type declaration
(,\s+optional|,\s+{re_default_param})? # optional 'optional' indication
Expand All @@ -745,8 +746,11 @@ class NumpyDocstring(GoogleDocstring):
(
{{({re_default_value},?\s*)+}} # set of default values
)?
\n)?
\s* (.*) # optional description
(?:$|\n)
)?
(
\s* (?P<param_desc>.*) # optional description
)?
""",
re.X | re.S,
)
Expand Down Expand Up @@ -797,15 +801,16 @@ def match_param_docs(self) -> tuple[set[str], set[str]]:
continue

# check if parameter has description only
re_only_desc = re.match(r"\s* (\*{0,2}\w+)\s*:?\n", entry)
re_only_desc = re.match(r"\s*(\*{0,2}\w+)\s*:?\n\s*\w*$", entry)
print(f're_only_desc: {re_only_desc}')
if re_only_desc:
param_name = match.group(1)
param_desc = match.group(2)
param_name = match.group('param_name')
param_desc = match.group('param_type')
param_type = None
else:
param_name = match.group(1)
param_type = match.group(3)
param_desc = match.group(4)
param_name = match.group('param_name')
param_type = match.group('param_type')
param_desc = match.group('param_desc')

if param_type:
params_with_type.add(param_name)
Expand Down
11 changes: 6 additions & 5 deletions tests/functional/ext/docparams/docparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,18 @@ def params_with_pipe(arg1: int | bool, arg2: str | None = None) -> None:


def regression_6211(number: int = 0) -> None:
"""This is a regression test for issue #6211.
"""This is a regression test for issue `#6211`_.
False negative of "missing param doc" was being issued when "default" used in
NumPy-style docs. This test should return no errors.
See https://github.com/PyCQA/pylint/issues/6211
Parameter
---------
Parameters
----------
number : int, default 0
The number parameter
.. _`#6211`:
https://github.com/PyCQA/pylint/issues/6211
"""

print(number)
32 changes: 16 additions & 16 deletions tests/functional/ext/docparams/docparams.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
missing-return-doc:4:0:4:18:_private_func1:Missing return documentation:HIGH
missing-return-type-doc:4:0:4:18:_private_func1:Missing return type documentation:HIGH
missing-yield-doc:9:0:9:18:_private_func2:Missing yield documentation:HIGH
missing-yield-type-doc:9:0:9:18:_private_func2:Missing yield type documentation:HIGH
missing-raises-doc:14:0:14:18:_private_func3:"""Exception"" not documented as being raised":HIGH
missing-any-param-doc:19:0:19:16:public_func1:"Missing any documentation in ""public_func1""":HIGH
missing-return-doc:24:0:24:30:_async_private_func1:Missing return documentation:HIGH
missing-return-type-doc:24:0:24:30:_async_private_func1:Missing return type documentation:HIGH
missing-yield-doc:29:0:29:30:_async_private_func2:Missing yield documentation:HIGH
missing-yield-type-doc:29:0:29:30:_async_private_func2:Missing yield type documentation:HIGH
missing-raises-doc:34:0:34:30:_async_private_func3:"""Exception"" not documented as being raised":HIGH
missing-any-param-doc:39:0:39:28:async_public_func1:"Missing any documentation in ""async_public_func1""":HIGH
differing-param-doc:44:0:44:23:differing_param_doc:"""param"" differing in parameter documentation":HIGH
differing-param-doc:55:0:55:35:differing_param_doc_kwords_only:"""param"" differing in parameter documentation":HIGH
missing-type-doc:66:0:66:20:missing_type_doc:"""par1"" missing in parameter type documentation":HIGH
missing-type-doc:76:0:76:32:missing_type_doc_kwords_only:"""par1"" missing in parameter type documentation":HIGH
missing-return-doc:5:0:5:18:_private_func1:Missing return documentation:HIGH
missing-return-type-doc:5:0:5:18:_private_func1:Missing return type documentation:HIGH
missing-yield-doc:10:0:10:18:_private_func2:Missing yield documentation:HIGH
missing-yield-type-doc:10:0:10:18:_private_func2:Missing yield type documentation:HIGH
missing-raises-doc:15:0:15:18:_private_func3:"""Exception"" not documented as being raised":HIGH
missing-any-param-doc:20:0:20:16:public_func1:"Missing any documentation in ""public_func1""":HIGH
missing-return-doc:25:0:25:30:_async_private_func1:Missing return documentation:HIGH
missing-return-type-doc:25:0:25:30:_async_private_func1:Missing return type documentation:HIGH
missing-yield-doc:30:0:30:30:_async_private_func2:Missing yield documentation:HIGH
missing-yield-type-doc:30:0:30:30:_async_private_func2:Missing yield type documentation:HIGH
missing-raises-doc:35:0:35:30:_async_private_func3:"""Exception"" not documented as being raised":HIGH
missing-any-param-doc:40:0:40:28:async_public_func1:"Missing any documentation in ""async_public_func1""":HIGH
differing-param-doc:45:0:45:23:differing_param_doc:"""param"" differing in parameter documentation":HIGH
differing-param-doc:56:0:56:35:differing_param_doc_kwords_only:"""param"" differing in parameter documentation":HIGH
missing-type-doc:67:0:67:20:missing_type_doc:"""par1"" missing in parameter type documentation":HIGH
missing-type-doc:77:0:77:32:missing_type_doc_kwords_only:"""par1"" missing in parameter type documentation":HIGH

0 comments on commit 3aaba09

Please sign in to comment.