Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require \ for asterisks in Sphinx-style parameter docstrings #5464

Merged
merged 5 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Release date: TBA

Closes #5437

* Fixed handling of Sphinx-style parameter docstrings with asterisks. These
should be escaped with by prepending a "\".

Closes #5406

* Add ``endLine`` and ``endColumn`` keys to output of ``JSONReporter``.

Closes #5380
Expand Down
8 changes: 5 additions & 3 deletions pylint/extensions/_check_docs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ class SphinxDocstring(Docstring):
\s+
)?

(\*{{0,2}}\w+) # Parameter name with potential asterisks
\s* # whitespace
: # final colon
((\\\*{{1,2}}\w+)|(\w+)) # Parameter name with potential asterisks
\s* # whitespace
: # final colon
"""
re_param_in_docstring = re.compile(re_param_raw, re.X | re.S)

Expand Down Expand Up @@ -377,6 +377,8 @@ def match_param_docs(self):

for match in re.finditer(self.re_param_in_docstring, self.doc):
name = match.group(2)
# Remove escape characters necessary for asterisks
name = name.replace("\\", "")
params_with_doc.add(name)
param_type = match.group(1)
if param_type is not None:
Expand Down
4 changes: 2 additions & 2 deletions pylint/reporters/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def colorize_ansi(
style: Optional[str] = None,
**kwargs: Optional[str],
) -> str:
"""colorize message by wrapping it with ansi escape codes
r"""colorize message by wrapping it with ansi escape codes

:param msg: the message string to colorize

Expand All @@ -149,7 +149,7 @@ def colorize_ansi(

:param style: the message's style elements, this will be deprecated

:param **kwargs: used to accept `color` parameter while it is being deprecated
:param \**kwargs: used to accept `color` parameter while it is being deprecated

:return: the ansi escaped string
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def test_warns_missing_args_sphinx( # [missing-param-doc, inconsistent-return-s

:param named_arg: Returned
:type named_arg: object

:returns: Maybe named_arg
:rtype: object or None
"""
Expand All @@ -172,36 +173,79 @@ def test_warns_missing_kwargs_sphinx( # [missing-param-doc, inconsistent-return

:param named_arg: Returned
:type named_arg: object

:returns: Maybe named_arg
:rtype: object or None
"""
if kwargs:
return named_arg


def test_finds_args_without_type_sphinx( # [inconsistent-return-statements]
def test_finds_args_without_type_sphinx( # [missing-param-doc, inconsistent-return-statements]
named_arg, *args
):
"""The docstring

:param named_arg: Returned
:type named_arg: object

:param *args: Optional arguments

:returns: Maybe named_arg
:rtype: object or None
"""
if args:
return named_arg


def test_finds_kwargs_without_type_sphinx( # [inconsistent-return-statements]
def test_finds_kwargs_without_type_sphinx( # [missing-param-doc, inconsistent-return-statements]
named_arg, **kwargs
):
"""The docstring

:param named_arg: Returned
:type named_arg: object

:param **kwargs: Keyword arguments

:returns: Maybe named_arg
:rtype: object or None
"""
if kwargs:
return named_arg


def test_finds_args_without_type_sphinx( # [inconsistent-return-statements]
named_arg, *args
):
r"""The Sphinx docstring
In Sphinx docstrings asteriks should be escaped.
See https://github.com/PyCQA/pylint/issues/5406

:param named_arg: Returned
:type named_arg: object

:param \*args: Optional arguments

:returns: Maybe named_arg
:rtype: object or None
"""
if args:
return named_arg


def test_finds_kwargs_without_type_sphinx( # [inconsistent-return-statements]
named_arg, **kwargs
):
r"""The Sphinx docstring
In Sphinx docstrings asteriks should be escaped.
See https://github.com/PyCQA/pylint/issues/5406

:param named_arg: Returned
:type named_arg: object

:param \**kwargs: Keyword arguments

:returns: Maybe named_arg
:rtype: object or None
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@ missing-type-doc:131:0:151:12:ClassFoo:"""x, y"" missing in parameter type docum
multiple-constructor-doc:131:0:151:12:ClassFoo:"""ClassFoo"" has constructor parameters documented in class and __init__":UNDEFINED
missing-param-doc:144:4:151:12:ClassFoo.__init__:"""x"" missing in parameter documentation":UNDEFINED
missing-type-doc:144:4:151:12:ClassFoo.__init__:"""x, y"" missing in parameter type documentation":UNDEFINED
inconsistent-return-statements:154:0:165:24:test_warns_missing_args_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:154:0:165:24:test_warns_missing_args_sphinx:"""*args"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:168:0:179:24:test_warns_missing_kwargs_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:168:0:179:24:test_warns_missing_kwargs_sphinx:"""**kwargs"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:182:0:194:24:test_finds_args_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
inconsistent-return-statements:197:0:209:24:test_finds_kwargs_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-raises-doc:219:4:224:17:Foo.foo:"""AttributeError"" not documented as being raised":UNDEFINED
unreachable:245:8:245:17:Foo.foo:Unreachable code:UNDEFINED
missing-param-doc:248:4:253:30:Foo.foo:"""value"" missing in parameter documentation":UNDEFINED
missing-raises-doc:248:4:253:30:Foo.foo:"""AttributeError"" not documented as being raised":UNDEFINED
missing-type-doc:248:4:253:30:Foo.foo:"""value"" missing in parameter type documentation":UNDEFINED
unreachable:284:8:284:17:Foo.foo:Unreachable code:UNDEFINED
useless-param-doc:288:4:302:12:Foo.test_useless_docs_ignored_argument_names_sphinx:"""_, _ignored"" useless ignored parameter documentation":UNDEFINED
useless-type-doc:288:4:302:12:Foo.test_useless_docs_ignored_argument_names_sphinx:"""_"" useless ignored parameter type documentation":UNDEFINED
inconsistent-return-statements:154:0:166:24:test_warns_missing_args_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:154:0:166:24:test_warns_missing_args_sphinx:"""*args"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:169:0:181:24:test_warns_missing_kwargs_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:169:0:181:24:test_warns_missing_kwargs_sphinx:"""**kwargs"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:184:0:198:24:test_finds_args_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:184:0:198:24:test_finds_args_without_type_sphinx:"""*args"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:201:0:215:24:test_finds_kwargs_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-param-doc:201:0:215:24:test_finds_kwargs_without_type_sphinx:"""**kwargs"" missing in parameter documentation":UNDEFINED
inconsistent-return-statements:218:0:234:24:test_finds_args_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
inconsistent-return-statements:237:0:253:24:test_finds_kwargs_without_type_sphinx:Either all return statements in a function should return an expression, or none of them should.:UNDEFINED
missing-raises-doc:263:4:268:17:Foo.foo:"""AttributeError"" not documented as being raised":UNDEFINED
unreachable:289:8:289:17:Foo.foo:Unreachable code:UNDEFINED
missing-param-doc:292:4:297:30:Foo.foo:"""value"" missing in parameter documentation":UNDEFINED
missing-raises-doc:292:4:297:30:Foo.foo:"""AttributeError"" not documented as being raised":UNDEFINED
missing-type-doc:292:4:297:30:Foo.foo:"""value"" missing in parameter type documentation":UNDEFINED
unreachable:328:8:328:17:Foo.foo:Unreachable code:UNDEFINED
useless-param-doc:332:4:346:12:Foo.test_useless_docs_ignored_argument_names_sphinx:"""_, _ignored"" useless ignored parameter documentation":UNDEFINED
useless-type-doc:332:4:346:12:Foo.test_useless_docs_ignored_argument_names_sphinx:"""_"" useless ignored parameter type documentation":UNDEFINED