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

Commit d9c35a9

Browse files
lordmauveNurdok
authored andcommitted
Select best imperative by longest prefix match
1 parent ee4e264 commit d9c35a9

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/pydocstyle/checker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from .parser import (Package, Module, Class, NestedClass, Definition, AllError,
1414
Method, Function, NestedFunction, Parser, StringIO,
1515
ParseError)
16-
from .utils import log, is_blank, pairwise
16+
from .utils import log, is_blank, pairwise, common_prefix_length
1717
from .wordlists import IMPERATIVE_VERBS, IMPERATIVE_BLACKLIST, stem
1818

1919

@@ -448,8 +448,12 @@ def check_imperative_mood(self, function, docstring): # def context
448448
return
449449

450450
if correct_forms and check_word not in correct_forms:
451+
best = max(
452+
correct_forms,
453+
key=lambda f: common_prefix_length(check_word, f)
454+
)
451455
return violations.D401(
452-
next(iter(correct_forms)).capitalize(),
456+
best.capitalize(),
453457
first_word
454458
)
455459

src/pydocstyle/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,16 @@ def pairwise(
2525
a, b = tee(iterable)
2626
_ = next(b, default_value)
2727
return zip_longest(a, b, fillvalue=default_value)
28+
29+
30+
def common_prefix_length(a: str, b: str) -> int:
31+
"""Return the length of the longest common prefix of a and b.
32+
33+
>>> common_prefix_length('abcd', 'abce')
34+
3
35+
36+
"""
37+
for common, (ca, cb) in enumerate(zip(a, b)):
38+
if ca != cb:
39+
return common
40+
return common + 1

src/tests/test_cases/test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ def docstring_ignore_some_violations_but_catch_D401(): # noqa: E501,D400,D415
387387
pass
388388

389389

390+
@expect(
391+
"D401: First line should be in imperative mood "
392+
"('Initiate', not 'Initiates')"
393+
)
394+
def docstring_initiates():
395+
"""Initiates the process."""
396+
397+
398+
@expect(
399+
"D401: First line should be in imperative mood "
400+
"('Initialize', not 'Initializes')"
401+
)
402+
def docstring_initializes():
403+
"""Initializes the process."""
404+
405+
390406
@wraps(docstring_bad_ignore_one)
391407
def bad_decorated_function():
392408
"""Bad (E501) but decorated"""

src/tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Unit test for pydocstyle utils.
2+
3+
Use tox or py.test to run the test suite.
4+
"""
5+
from pydocstyle import utils
6+
7+
8+
__all__ = ()
9+
10+
11+
def test_common_prefix():
12+
"""We can find the common prefix of two strings."""
13+
assert utils.common_prefix_length('abcd', 'abce') == 3
14+
15+
16+
def test_no_common_prefix():
17+
"""If two strings have no common prefix, return the empty string."""
18+
assert utils.common_prefix_length('abcd', 'cdef') == 0
19+
20+
21+
def test_differ_length():
22+
"""We can find a common prefix of two strings differing in length."""
23+
assert utils.common_prefix_length('abcd', 'ab') == 2

0 commit comments

Comments
 (0)