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

Commit f24270e

Browse files
committed
Added new error code (D403) for capitalization issues
1 parent 20dc16f commit f24270e

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/pep257.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717
import sys
1818
import copy
19+
import string
1920
import logging
2021
import tokenize as tk
2122
from itertools import takewhile, dropwhile, chain
@@ -454,9 +455,9 @@ def parse_definition(self, class_):
454455
docstring = self.parse_docstring()
455456
decorators = self._accumulated_decorators
456457
self._accumulated_decorators = []
457-
log.debug("parsing nested defintions.")
458+
log.debug("parsing nested definitions.")
458459
children = list(self.parse_definitions(class_))
459-
log.debug("finished parsing nested defintions for '%s'", name)
460+
log.debug("finished parsing nested definitions for '%s'", name)
460461
end = self.line - 1
461462
else: # one-liner definition
462463
docstring = self.parse_docstring()
@@ -678,6 +679,11 @@ def to_rst(cls):
678679
'%r, not %r')
679680
D402 = D4xx.create_error('D402', 'First line should not be the function\'s '
680681
'"signature"')
682+
D402 = D4xx.create_error('D402', 'First line should not be the function\'s '
683+
'"signature"')
684+
D403 = D4xx.create_error('D403', 'First word of the first line should be '
685+
'properly capitalized', '%r, not %r')
686+
681687

682688

683689
class AttrDict(dict):
@@ -1592,6 +1598,18 @@ def check_no_signature(self, function, docstring): # def context
15921598
if function.name + '(' in first_line.replace(' ', ''):
15931599
return D402()
15941600

1601+
@check_for(Function)
1602+
def check_capitalized(self, function, docstring):
1603+
"""D403: First word of the first line should be properly capitalized.
1604+
1605+
The [first line of a] docstring is a phrase ending in a period.
1606+
1607+
"""
1608+
if docstring:
1609+
first_word = eval(docstring).split()[0]
1610+
if first_word != first_word.capitalize():
1611+
return D403(first_word.capitalize(), first_word)
1612+
15951613
# Somewhat hard to determine if return value is mentioned.
15961614
# @check(Function)
15971615
def SKIP_check_return_type(self, function, docstring):
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""A valid module docstring."""
2+
3+
from .expected import Expectation
4+
5+
expectation = Expectation()
6+
expect = expectation.expect
7+
8+
9+
@expect("D403: First word of the first line should be properly capitalized "
10+
"('Do', not 'do')")
11+
def not_capitalized():
12+
"""do something."""
13+
14+
15+
# Make sure empty docstrings don't generate capitalization errors.
16+
@expect("D103: Missing docstring in public function")
17+
def empty_docstring():
18+
""""""
19+
20+
21+
@expect("D403: First word of the first line should be properly capitalized "
22+
"('Get', not 'GET')")
23+
def all_caps():
24+
"""GET the request."""

src/tests/test_definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def test_token_stream():
138138

139139
def test_pep257():
140140
"""Run domain-specific tests from test.py file."""
141-
test_cases = ('test', 'unicode_literals', 'nested_class')
141+
test_cases = ('test', 'unicode_literals', 'nested_class', 'capitalization')
142142
for test_case in test_cases:
143143
case_module = __import__('test_cases.{0}'.format(test_case),
144144
globals=globals(),

src/tests/test_pep257.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def function_with_bad_docstring(foo):
135135
return foo
136136
''')
137137
expected_error_codes = set(('D100', 'D400', 'D401', 'D205', 'D209',
138-
'D210'))
138+
'D210', 'D403'))
139139
mock_open = mock.mock_open(read_data=function_to_check)
140140
from .. import pep257
141141
with mock.patch.object(pep257, 'tokenize_open', mock_open, create=True):

0 commit comments

Comments
 (0)