summaryrefslogtreecommitdiff
path: root/pylint/extensions
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2022-09-04 22:04:18 +0200
committerGitHub <noreply@github.com>2022-09-04 22:04:18 +0200
commitb1d634246b82dbdae73be9c8fdb705a619c4f801 (patch)
tree9b90226728a5fd68769e6837283108e88c6bdede /pylint/extensions
parentb00638dbe32dfda61d138f89330744b7303a7e2d (diff)
downloadpylint-git-b1d634246b82dbdae73be9c8fdb705a619c4f801.tar.gz
Fix and refactors for ``docparams`` extension (#7398)
* Fix and refactors for ``docparams`` extension 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. Co-authored-by: Hendry, Adam <adam.grant.hendry@gmail.com>
Diffstat (limited to 'pylint/extensions')
-rw-r--r--pylint/extensions/_check_docs_utils.py40
1 files changed, 27 insertions, 13 deletions
diff --git a/pylint/extensions/_check_docs_utils.py b/pylint/extensions/_check_docs_utils.py
index 747fcb4ea..d8f797b6c 100644
--- a/pylint/extensions/_check_docs_utils.py
+++ b/pylint/extensions/_check_docs_utils.py
@@ -247,7 +247,7 @@ class SphinxDocstring(Docstring):
re_multiple_simple_type = r"""
(?:{container_type}|{type})
- (?:(?:\s+(?:of|or)\s+|\s*,\s*)(?:{container_type}|{type}))*
+ (?:(?:\s+(?:of|or)\s+|\s*,\s*|\s+\|\s+)(?:{container_type}|{type}))*
""".format(
type=re_type, container_type=re_simple_container_type
)
@@ -449,7 +449,7 @@ class GoogleDocstring(Docstring):
re_multiple_type = r"""
(?:{container_type}|{type}|{xref})
- (?:(?:\s+(?:of|or)\s+|\s*,\s*)(?:{container_type}|{type}|{xref}))*
+ (?:(?:\s+(?:of|or)\s+|\s*,\s*|\s+\|\s+)(?:{container_type}|{type}|{xref}))*
""".format(
type=re_type, xref=re_xref, container_type=re_container_type
)
@@ -728,13 +728,13 @@ class NumpyDocstring(GoogleDocstring):
re.X | re.S | re.M,
)
- re_default_value = r"""((['"]\w+\s*['"])|(True)|(False)|(None))"""
+ re_default_value = r"""((['"]\w+\s*['"])|(\d+)|(True)|(False)|(None))"""
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>
(
({GoogleDocstring.re_multiple_type}) # default type declaration
(,\s+optional)? # optional 'optional' indication
@@ -742,8 +742,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,
)
@@ -794,15 +797,26 @@ class NumpyDocstring(GoogleDocstring):
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)
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")
+ # The re_param_line pattern needs to match multi-line which removes the ability
+ # to match a single line description like 'arg : a number type.'
+ # We are not trying to determine whether 'a number type' is correct typing
+ # but we do accept it as typing as it is in the place where typing
+ # should be
+ if param_type is None and re.match(r"\s*(\*{0,2}\w+)\s*:.+$", entry):
+ param_type = param_desc
+ # If the description is "" but we have a type description
+ # we consider the description to be the type
+ if not param_desc and param_type:
+ param_desc = param_type
if param_type:
params_with_type.add(param_name)