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

Commit 2aeb691

Browse files
committed
Fixed a bug with comments after function docstrings
1 parent f24270e commit 2aeb691

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/pep257.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class Definition(Value):
119119
module = property(lambda self: self.parent.module)
120120
all = property(lambda self: self.module.all)
121121
_slice = property(lambda self: slice(self.start - 1, self.end))
122-
source = property(lambda self: ''.join(self._source[self._slice]))
123122
is_class = False
124123

125124
def __iter__(self):
@@ -129,6 +128,17 @@ def __iter__(self):
129128
def _publicity(self):
130129
return {True: 'public', False: 'private'}[self.is_public]
131130

131+
@property
132+
def source(self):
133+
"""Return the source code for the definition."""
134+
full_src = self._source[self._slice]
135+
136+
def predicate(line):
137+
return line.strip() == '' or line.strip().startswith('#')
138+
139+
filtered_src = dropwhile(predicate, reversed(full_src))
140+
return ''.join(reversed(list(filtered_src)))
141+
132142
def __str__(self):
133143
return 'in %s %s `%s`' % (self._publicity, self._human, self.name)
134144

@@ -428,7 +438,7 @@ def parse_module(self):
428438
return module
429439

430440
def parse_definition(self, class_):
431-
"""Parse a defintion and return its value in a `class_` object."""
441+
"""Parse a definition and return its value in a `class_` object."""
432442
start = self.line
433443
self.consume(tk.NAME)
434444
name = self.current.value
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Check for a bug in parsing comments after definitions."""
2+
3+
from .expected import Expectation
4+
5+
expectation = Expectation()
6+
expect = expectation.expect
7+
8+
9+
def should_be_ok():
10+
"""Just a function without violations."""
11+
12+
13+
# This is a comment that triggers a bug that causes the previous function
14+
# to generate a D202 error.

src/tests/test_definitions.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ 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', 'capitalization')
141+
test_cases = (
142+
'test',
143+
'unicode_literals',
144+
'nested_class',
145+
'capitalization',
146+
'comment_after_def_bug',
147+
)
142148
for test_case in test_cases:
143149
case_module = __import__('test_cases.{0}'.format(test_case),
144150
globals=globals(),
145151
locals=locals(),
146152
fromlist=['expectation'],
147153
level=1)
148-
# from .test_cases import test
149154
results = list(check([os.path.join(os.path.dirname(__file__),
150155
'test_cases', test_case + '.py')],
151156
select=set(ErrorRegistry.get_error_codes())))

0 commit comments

Comments
 (0)