Skip to content

Commit

Permalink
Highlight blank lines and provide additional suggestions for E9989 pe…
Browse files Browse the repository at this point in the history
…p8-errors (#913)
  • Loading branch information
Bruce-8 authored Jun 24, 2023
1 parent eb8630d commit 35d2487
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- The `UnnecessaryIndexingChecker` now checks for a greater variety of loop/comprehension indexes.
- Modified configuration behaviour so when providing a config file, it only needs to contain the configuration options you want overridden.
- Added the option `load_default_config` to `check_errors` and `check_all` to specify whether to automatically load the PythonTA default config.
- For the message display of E9989 `pep8-errors`, all "blank line" messages are now custom-rendered, i.e., blank lines
are now highlighted instead of function signatures and instruction strings are added for required blank lines that
are missing.

### Bug Fixes

Expand Down
128 changes: 128 additions & 0 deletions python_ta/reporters/node_printers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from astroid import nodes

NEW_BLANK_LINE_MESSAGE = "# INSERT NEW BLANK LINE HERE"


def render_message(msg, node, source_lines):
"""Render a message based on type."""
Expand Down Expand Up @@ -128,6 +130,131 @@ def render_missing_space_in_doctest(msg, _node, source_lines=None):
yield from render_context(line + 1, line + 3, source_lines)


def render_pep8_errors(msg, _node, source_lines=None):
"""Render a PEP8 error message."""
if "expected 1 blank line," in msg.msg:
yield from render_pep8_errors_e301(msg, _node, source_lines)
elif "expected 2 blank lines," in msg.msg:
yield from render_pep8_errors_e302(msg, _node, source_lines)
elif "too many blank lines" in msg.msg:
yield from render_pep8_errors_e303(msg, _node, source_lines)
elif "blank lines found after function decorator" in msg.msg:
yield from render_pep8_errors_e304(msg, _node, source_lines)
elif "expected 2 blank lines after class or function definition" in msg.msg:
yield from render_pep8_errors_e305(msg, _node, source_lines)
elif "expected 1 blank line before a nested definition" in msg.msg:
yield from render_pep8_errors_e306(msg, _node, source_lines)
else:
yield from render_generic(msg, _node, source_lines)


def render_blank_line(line):
"""Render a blank line for a PEP8 error message."""
yield (line + 1, slice(None, None), LineType.ERROR, " " * 28)


def render_pep8_errors_e301(msg, _node, source_lines=None):
"""Render a PEP8 expected 1 blank line message."""
line = msg.line - 1
yield from render_context(line - 1, line + 1, source_lines)
body = source_lines[line]
indentation = len(body) - len(body.lstrip())
yield (
None,
slice(None, None),
LineType.ERROR,
body[:indentation] + NEW_BLANK_LINE_MESSAGE + " " * indentation,
)
yield from render_context(msg.line, msg.line + 2, source_lines)


def render_pep8_errors_e302(msg, _node, source_lines=None):
"""Render a PEP8 expected 2 blank lines message."""
line = msg.line - 1
if "found 0" in msg.msg:
yield from render_context(line - 1, line + 1, source_lines)
yield from (
(
None,
slice(None, None),
LineType.ERROR,
NEW_BLANK_LINE_MESSAGE,
)
for _ in range(0, 2)
)
else:
line -= 1
yield from render_context(line - 1, line + 1, source_lines)
yield from render_blank_line(line)
yield (None, slice(None, None), LineType.ERROR, NEW_BLANK_LINE_MESSAGE)
yield from render_context(msg.line, msg.line + 2, source_lines)


def render_pep8_errors_e303(msg, _node, source_lines=None):
"""Render a PEP8 too many blank lines message."""
line = msg.line - 1
while source_lines[line - 1].strip() == "":
line -= 1
yield from render_context(line - 1, line + 1, source_lines)
body = source_lines[msg.line - 1]
indentation = len(body) - len(body.lstrip())
yield from (
(curr_line, slice(None, None), LineType.ERROR, " " * (indentation + 28))
for curr_line in range(line + 1, msg.line)
)
yield from render_context(msg.line, msg.line + 2, source_lines)


def render_pep8_errors_e304(msg, _node, source_lines=None):
"""Render a PEP8 blank lines found after function decorator message."""
line = msg.line - 1
while source_lines[line - 1].strip() == "":
line -= 1
yield from render_context(line - 1, line + 1, source_lines)
yield from (
(curr_line, slice(None, None), LineType.ERROR, " " * 28)
for curr_line in range(line + 1, msg.line)
)
yield from render_context(msg.line, msg.line + 2, source_lines)


def render_pep8_errors_e305(msg, _node, source_lines=None):
"""Render a PEP8 expected 2 blank lines after class or function definition message."""
line = msg.line - 1
if "found 0" in msg.msg:
yield from render_context(line - 1, line + 1, source_lines)
yield from (
(
None,
slice(None, None),
LineType.ERROR,
NEW_BLANK_LINE_MESSAGE,
)
for _ in range(0, 2)
)
else:
line -= 1
yield from render_context(line - 1, line + 1, source_lines)
yield from render_blank_line(line)
yield (None, slice(None, None), LineType.ERROR, NEW_BLANK_LINE_MESSAGE)
yield from render_context(msg.line, msg.line + 2, source_lines)


def render_pep8_errors_e306(msg, _node, source_lines=None):
"""Render a PEP8 expected 1 blank line before a nested definition message."""
line = msg.line - 1
yield from render_context(line - 1, line + 1, source_lines)
body = source_lines[line]
indentation = len(body) - len(body.lstrip())
yield (
None,
slice(None, None),
LineType.ERROR,
body[:indentation] + NEW_BLANK_LINE_MESSAGE + " " * indentation,
)
yield from render_context(msg.line, msg.line + 2, source_lines)


CUSTOM_MESSAGES = {
"missing-module-docstring": render_missing_docstring,
"missing-class-docstring": render_missing_docstring,
Expand All @@ -137,6 +264,7 @@ def render_missing_space_in_doctest(msg, _node, source_lines=None):
"missing-return-type": render_missing_return_type,
"too-many-arguments": render_too_many_arguments,
"missing-space-in-doctest": render_missing_space_in_doctest,
"pep8-errors": render_pep8_errors,
}


Expand Down

0 comments on commit 35d2487

Please sign in to comment.