diff --git a/flake8_rst/rst.py b/flake8_rst/rst.py index 124a1ca..b10a3c4 100644 --- a/flake8_rst/rst.py +++ b/flake8_rst/rst.py @@ -28,32 +28,34 @@ re.MULTILINE, ) +EXPRESSIONS = (RST_RE, ANCHOR_RE) + def find_sourcecode(src): - for expression in [RST_RE, ANCHOR_RE]: - for match in expression.finditer(src): - origin_code = match.group('code') + matches = (match for expression in EXPRESSIONS for match in expression.finditer(src)) + for match in matches: + origin_code = match.group('code') - try: - min_indent = min(INDENT_RE.findall(origin_code)) - except ValueError: - min_indent = '' + try: + min_indent = min(INDENT_RE.findall(origin_code)) + except ValueError: + min_indent = '' - indent = len(min_indent) - code = textwrap.dedent(origin_code) + indent = len(min_indent) + code = textwrap.dedent(origin_code) - if '>>>' in code: - indent += 4 - lines = [] + if '>>>' in code: + indent += 4 + lines = [] - for i, line in enumerate(code.split('\n')): - for anchor in ANCHORS: - if line.startswith(anchor): - lines.append(line[len(anchor):]) - break + for i, line in enumerate(code.split('\n')): + for anchor in ANCHORS: + if line.startswith(anchor): + lines.append(line[len(anchor):]) + break - code = '\n'.join(lines) + code = '\n'.join(lines) - line_number = src[:match.start()].count('\n') + match.group('before').count('\n') + line_number = src[:match.start()].count('\n') + match.group('before').count('\n') - yield code.rstrip(), indent, line_number + yield code.rstrip(), indent, line_number diff --git a/tests/data/example_10.py b/tests/data/example_10.py index e6e2ae6..c1337e5 100644 --- a/tests/data/example_10.py +++ b/tests/data/example_10.py @@ -16,4 +16,4 @@ >>> print('This line is highlighted.') This line is highlighted. -""" \ No newline at end of file +""" diff --git a/tests/result_py2/result_10.py b/tests/result_py2/result_10.py index 53c8124..45204c1 100644 --- a/tests/result_py2/result_10.py +++ b/tests/result_py2/result_10.py @@ -1,8 +1,8 @@ ('test_precisely', - [('F821', 20, 10, "undefined name 'LdaModel'", None), - ('F821', 20, 26, "undefined name 'mm'", None), - ('F821', 20, 38, "undefined name 'id2word'", None), - ('F821', 22, 10, "undefined name 'LdfModel'", None), - ('F821', 22, 26, "undefined name 'cm'", None), - ('F821', 22, 38, "undefined name 'id2word'", None)], + [('F821', 21, 10, "undefined name 'LdaModel'", None), + ('F821', 21, 26, "undefined name 'mm'", None), + ('F821', 21, 38, "undefined name 'id2word'", None), + ('F821', 23, 10, "undefined name 'LdfModel'", None), + ('F821', 23, 26, "undefined name 'cm'", None), + ('F821', 23, 38, "undefined name 'id2word'", None)], {'logical lines': 5, 'physical lines': 6, 'tokens': 52}) diff --git a/tests/result_py3/result_10.py b/tests/result_py3/result_10.py index 53c8124..45204c1 100644 --- a/tests/result_py3/result_10.py +++ b/tests/result_py3/result_10.py @@ -1,8 +1,8 @@ ('test_precisely', - [('F821', 20, 10, "undefined name 'LdaModel'", None), - ('F821', 20, 26, "undefined name 'mm'", None), - ('F821', 20, 38, "undefined name 'id2word'", None), - ('F821', 22, 10, "undefined name 'LdfModel'", None), - ('F821', 22, 26, "undefined name 'cm'", None), - ('F821', 22, 38, "undefined name 'id2word'", None)], + [('F821', 21, 10, "undefined name 'LdaModel'", None), + ('F821', 21, 26, "undefined name 'mm'", None), + ('F821', 21, 38, "undefined name 'id2word'", None), + ('F821', 23, 10, "undefined name 'LdfModel'", None), + ('F821', 23, 26, "undefined name 'cm'", None), + ('F821', 23, 38, "undefined name 'id2word'", None)], {'logical lines': 5, 'physical lines': 6, 'tokens': 52})