Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 145daec

Browse files
shacharooNurdok
authored andcommitted
Fix section mix bug (#424)
* fix the bug and add docs * update docs
1 parent 3b1646a commit 145daec

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

docs/error_codes.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Default conventions
1010
-------------------
1111

1212
Not all error codes are checked for by default. There are three
13-
conventions that may be used by pydocstyle: pep257, numpy and google.
13+
conventions that may be used by pydocstyle: ``pep257``, ``numpy`` and ``google``.
1414

1515
The pep257 convention, which is enabled by default in pydocstyle,
1616
checks for all of the above errors except for D203, D212, D213, D214,
@@ -28,6 +28,17 @@ running pydocstyle from the command line or by specifying the
2828
convention in a configuration file. See the :ref:`cli_usage` section
2929
for more details.
3030

31+
.. note::
32+
33+
It makes no sense to check the same docstring for both ``numpy`` and ``google``
34+
conventions. Therefore, if we successfully detect that a docstring is in the
35+
``numpy`` style, we don't check it for ``google``.
36+
37+
The reason ``numpy`` style takes precedence over ``google`` is that the
38+
heuristics of detecting it are better, and we don't want to enforce users to
39+
provide external hints to `pydocstyle` in order to let it know which style
40+
docstrings are written in.
41+
3142
Publicity
3243
---------
3344

docs/release_notes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Bug Fixes
2828
``\N...`` anymore. These are considered intended elements of the docstring
2929
and thus should not be escaped by using a raw docstring (#365).
3030
* Fix decorator parsing (#411).
31+
* Google-style sections no longer cause false errors when used with
32+
Numpy-style sections (#388, #424).
3133

3234
4.0.1 - August 14th, 2019
3335
-------------------------

src/pydocstyle/checker.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ def _suspected_as_section(_line):
824824
False)
825825
for i in suspected_section_indices)
826826

827-
828827
# Now that we have manageable objects - rule out false positives.
829828
contexts = (c for c in contexts if ConventionChecker._is_docstring_section(c))
830829

@@ -833,12 +832,11 @@ def _suspected_as_section(_line):
833832
for a, b in pairwise(contexts, None):
834833
end = -1 if b is None else b.original_index
835834
yield SectionContext(a.section_name,
836-
a.previous_line,
837-
a.line,
838-
lines[a.original_index + 1:end],
839-
a.original_index,
840-
b is None)
841-
835+
a.previous_line,
836+
a.line,
837+
lines[a.original_index + 1:end],
838+
a.original_index,
839+
b is None)
842840

843841
def _check_numpy_sections(self, lines, definition, docstring):
844842
"""NumPy-style docstring sections checks.
@@ -860,10 +858,14 @@ def _check_numpy_sections(self, lines, definition, docstring):
860858
Yields all violation from `_check_numpy_section` for each valid
861859
Numpy-style section.
862860
"""
861+
found_any_numpy_section = False
863862
for ctx in self._get_section_contexts(lines,
864863
self.NUMPY_SECTION_NAMES):
864+
found_any_numpy_section = True
865865
yield from self._check_numpy_section(docstring, definition, ctx)
866866

867+
return found_any_numpy_section
868+
867869
def _check_google_sections(self, lines, definition, docstring):
868870
"""Google-style docstring section checks.
869871
@@ -895,8 +897,10 @@ def check_docstring_sections(self, definition, docstring):
895897
lines = docstring.split("\n")
896898
if len(lines) < 2:
897899
return
898-
yield from self._check_numpy_sections(lines, definition, docstring)
899-
yield from self._check_google_sections(lines, definition, docstring)
900+
901+
found_numpy = yield from self._check_numpy_sections(lines, definition, docstring)
902+
if not found_numpy:
903+
yield from self._check_google_sections(lines, definition, docstring)
900904

901905

902906
parse = Parser()

src/tests/test_cases/sections.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,14 @@ def test_missing_args_static_method(a, x, y, z=3, t=1): # noqa: D213, D407
414414
Yet another parameter.
415415
416416
"""
417+
418+
@staticmethod
419+
def test_mixing_numpy_and_google(danger): # noqa: D213
420+
"""Repro for #388.
421+
422+
Parameters
423+
----------
424+
danger
425+
Zoneeeeee!
426+
427+
"""

0 commit comments

Comments
 (0)