From 5d5227280cb69a15a15cc48781b9811fdb02b728 Mon Sep 17 00:00:00 2001 From: umututku03 Date: Fri, 29 Sep 2023 15:12:40 -0400 Subject: [PATCH 01/11] overlines for plain-reporter snippet to distinguish errors --- python_ta/reporters/plain_reporter.py | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 4eddba60f..5ce18a29c 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -3,6 +3,7 @@ from pylint.interfaces import IReporter from .core import NewMessage, PythonTaReporter +from .node_printers import LineType class PlainReporter(PythonTaReporter): @@ -83,3 +84,51 @@ def _colour_messages_by_type(self, messages: Dict[str, List[NewMessage]]) -> str result += self._BREAK return result + + def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = "") -> str: + """Format given source code line as specified and return as str. + + Called by _build_snippet, relies on _colourify. + """ + snippet = self._add_line_number(lineno, linetype) + + if linetype == LineType.ERROR: + start_col = slice_.start or 0 + end_col = slice_.stop or len(text) + + if text[:start_col]: + # by default, self._colourify returns the text itself + snippet += self._colourify("black", text[:start_col]) + prespace = len(snippet) * self._SPACE + snippet += self._colourify("highlight", text[slice_]) + if text[end_col:]: + snippet += self._colourify("black", text[end_col:]) + snippet = self._overline_helper(snippet=snippet, text=text[slice_], prespace=prespace) + elif linetype == LineType.CONTEXT: + snippet += self._colourify("grey", text) + # snippet += "‾" + elif linetype == LineType.OTHER: + snippet += text + elif linetype == LineType.DOCSTRING: + space_c = len(text) - len(text.lstrip(" ")) + snippet += space_c * self._SPACE + prespace = snippet # at this point, we just have spaces + snippet += self._colourify("highlight", text.lstrip(" ")) + snippet = self._overline_helper( + snippet=snippet, text=text.lstrip(" "), prespace=prespace + ) + snippet += space_c * self._SPACE + + snippet += self._BREAK + return snippet + + def _overline_helper(self, snippet: str, text: str, prespace: str) -> str: + """ + Helper method _add_line. Adds the Unicode203E (the overline character "‾") under any + part that is highlighted as ERROR. Returns the corresponding snippet as a result. + """ + overline = "‾" * len(text) + snippet += self._BREAK # So that we add overline under the line (row) that error occurs + snippet += prespace + overline + + return snippet From a32730d7d963b7911157ee299e393425931e9e7a Mon Sep 17 00:00:00 2001 From: umututku03 Date: Thu, 5 Oct 2023 00:14:58 -0400 Subject: [PATCH 02/11] Overline for colour reporter --- python_ta/reporters/color_reporter.py | 37 +++++++++++++++++++++++++-- python_ta/reporters/plain_reporter.py | 2 -- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index a9569a753..dea4989d4 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -3,6 +3,7 @@ from colorama import Back, Fore, Style, colorama_text from pylint.interfaces import IReporter +from .node_printers import LineType from .plain_reporter import PlainReporter @@ -37,6 +38,39 @@ def print_messages(self, level: str = "all") -> None: with colorama_text(wrap=(sys.platform == "win32"), strip=False): super().print_messages(level) + def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = "") -> str: + """Format given source code line as specified and return as str. + + Called by _build_snippet, relies on _colourify. + """ + snippet = self._add_line_number(lineno, linetype) + if linetype == LineType.ERROR: + start_col = slice_.start or 0 + end_col = slice_.stop or len(text) + if text[:start_col]: + snippet += self._colourify("black", text[:start_col]) + prespace = snippet + snippet += self._colourify("highlight", text[slice_]) + if text[end_col:]: + snippet += self._colourify("black", text[end_col:]) + snippet = self._overline_helper(snippet=snippet, text=text[slice_], prespace=prespace) + elif linetype == LineType.CONTEXT: + snippet += self._colourify("grey", text) + elif linetype == LineType.OTHER: + snippet += text + elif linetype == LineType.DOCSTRING: + space_c = len(text) - len(text.lstrip(" ")) + snippet += space_c * self._SPACE + prespace = snippet # at this point, we just have spaces + snippet += self._colourify("highlight", text.lstrip(" ")) + snippet = self._overline_helper( + snippet=snippet, text=text.lstrip(" "), prespace=prespace + ) + snippet += space_c * self._SPACE + + snippet += self._BREAK + return snippet + @classmethod def _colourify(cls, colour_class: str, text: str) -> str: """ @@ -48,6 +82,5 @@ def _colourify(cls, colour_class: str, text: str) -> str: """ colour = cls._COLOURING[colour_class] new_text = text.lstrip(" ") - space_count = len(text) - len(new_text) new_text = new_text.replace(" ", cls._SPACE) - return (space_count * cls._SPACE) + colour + new_text + cls._COLOURING["reset"] + return colour + new_text + cls._COLOURING["reset"] diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 5ce18a29c..0411f4ebb 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -95,7 +95,6 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = if linetype == LineType.ERROR: start_col = slice_.start or 0 end_col = slice_.stop or len(text) - if text[:start_col]: # by default, self._colourify returns the text itself snippet += self._colourify("black", text[:start_col]) @@ -106,7 +105,6 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = snippet = self._overline_helper(snippet=snippet, text=text[slice_], prespace=prespace) elif linetype == LineType.CONTEXT: snippet += self._colourify("grey", text) - # snippet += "‾" elif linetype == LineType.OTHER: snippet += text elif linetype == LineType.DOCSTRING: From 910d4f226bf749c4f9c2acd044fc3aa8ca470f2c Mon Sep 17 00:00:00 2001 From: umututku03 Date: Fri, 6 Oct 2023 17:25:40 -0400 Subject: [PATCH 03/11] Fixing the issue with the ColourReporter --- python_ta/reporters/color_reporter.py | 12 ++++-------- python_ta/reporters/plain_reporter.py | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index dea4989d4..ea984c4a5 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -44,16 +44,16 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = Called by _build_snippet, relies on _colourify. """ snippet = self._add_line_number(lineno, linetype) + if linetype == LineType.ERROR: start_col = slice_.start or 0 end_col = slice_.stop or len(text) + if text[:start_col]: snippet += self._colourify("black", text[:start_col]) - prespace = snippet snippet += self._colourify("highlight", text[slice_]) if text[end_col:]: snippet += self._colourify("black", text[end_col:]) - snippet = self._overline_helper(snippet=snippet, text=text[slice_], prespace=prespace) elif linetype == LineType.CONTEXT: snippet += self._colourify("grey", text) elif linetype == LineType.OTHER: @@ -61,12 +61,7 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = elif linetype == LineType.DOCSTRING: space_c = len(text) - len(text.lstrip(" ")) snippet += space_c * self._SPACE - prespace = snippet # at this point, we just have spaces snippet += self._colourify("highlight", text.lstrip(" ")) - snippet = self._overline_helper( - snippet=snippet, text=text.lstrip(" "), prespace=prespace - ) - snippet += space_c * self._SPACE snippet += self._BREAK return snippet @@ -82,5 +77,6 @@ def _colourify(cls, colour_class: str, text: str) -> str: """ colour = cls._COLOURING[colour_class] new_text = text.lstrip(" ") + space_count = len(text) - len(new_text) new_text = new_text.replace(" ", cls._SPACE) - return colour + new_text + cls._COLOURING["reset"] + return (space_count * cls._SPACE) + colour + new_text + cls._COLOURING["reset"] diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 0411f4ebb..737f80ec1 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -91,7 +91,6 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = Called by _build_snippet, relies on _colourify. """ snippet = self._add_line_number(lineno, linetype) - if linetype == LineType.ERROR: start_col = slice_.start or 0 end_col = slice_.stop or len(text) From b4e0cf0834a2d2f18b38de699eadb1d9c099f09a Mon Sep 17 00:00:00 2001 From: umututku03 Date: Fri, 6 Oct 2023 19:05:02 -0400 Subject: [PATCH 04/11] Adding super call for the ColourReporter --- python_ta/reporters/color_reporter.py | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index ea984c4a5..ed600c127 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -43,28 +43,7 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = Called by _build_snippet, relies on _colourify. """ - snippet = self._add_line_number(lineno, linetype) - - if linetype == LineType.ERROR: - start_col = slice_.start or 0 - end_col = slice_.stop or len(text) - - if text[:start_col]: - snippet += self._colourify("black", text[:start_col]) - snippet += self._colourify("highlight", text[slice_]) - if text[end_col:]: - snippet += self._colourify("black", text[end_col:]) - elif linetype == LineType.CONTEXT: - snippet += self._colourify("grey", text) - elif linetype == LineType.OTHER: - snippet += text - elif linetype == LineType.DOCSTRING: - space_c = len(text) - len(text.lstrip(" ")) - snippet += space_c * self._SPACE - snippet += self._colourify("highlight", text.lstrip(" ")) - - snippet += self._BREAK - return snippet + return super(PlainReporter, self)._add_line(lineno, linetype, slice_, text) @classmethod def _colourify(cls, colour_class: str, text: str) -> str: From 587b8f1c4f8832cf81a3a3345f704882c7907f5b Mon Sep 17 00:00:00 2001 From: umututku03 Date: Mon, 9 Oct 2023 22:58:54 -0400 Subject: [PATCH 05/11] Fixing PlainReporter._add_line and dependancy issues. --- python_ta/reporters/color_reporter.py | 7 ----- python_ta/reporters/plain_reporter.py | 37 ++++++++------------------- 2 files changed, 10 insertions(+), 34 deletions(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index ed600c127..b28a52cad 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -38,13 +38,6 @@ def print_messages(self, level: str = "all") -> None: with colorama_text(wrap=(sys.platform == "win32"), strip=False): super().print_messages(level) - def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = "") -> str: - """Format given source code line as specified and return as str. - - Called by _build_snippet, relies on _colourify. - """ - return super(PlainReporter, self)._add_line(lineno, linetype, slice_, text) - @classmethod def _colourify(cls, colour_class: str, text: str) -> str: """ diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 737f80ec1..26dbcdde2 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -90,34 +90,17 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = Called by _build_snippet, relies on _colourify. """ - snippet = self._add_line_number(lineno, linetype) + snippet_so_far = super()._add_line(lineno, linetype, slice_, text) if linetype == LineType.ERROR: - start_col = slice_.start or 0 - end_col = slice_.stop or len(text) - if text[:start_col]: - # by default, self._colourify returns the text itself - snippet += self._colourify("black", text[:start_col]) - prespace = len(snippet) * self._SPACE - snippet += self._colourify("highlight", text[slice_]) - if text[end_col:]: - snippet += self._colourify("black", text[end_col:]) - snippet = self._overline_helper(snippet=snippet, text=text[slice_], prespace=prespace) - elif linetype == LineType.CONTEXT: - snippet += self._colourify("grey", text) - elif linetype == LineType.OTHER: - snippet += text + start = slice_.start or 0 + prespace = (7 + start) * self._SPACE + snippet_so_far = self._overline_helper(snippet_so_far, text[slice_], prespace) + elif linetype == LineType.DOCSTRING: - space_c = len(text) - len(text.lstrip(" ")) - snippet += space_c * self._SPACE - prespace = snippet # at this point, we just have spaces - snippet += self._colourify("highlight", text.lstrip(" ")) - snippet = self._overline_helper( - snippet=snippet, text=text.lstrip(" "), prespace=prespace - ) - snippet += space_c * self._SPACE - - snippet += self._BREAK - return snippet + prespace = (7 + len(text) - len(text.lstrip(" "))) * self._SPACE + snippet_so_far = self._overline_helper(snippet_so_far, text.lstrip(" "), prespace) + + return snippet_so_far def _overline_helper(self, snippet: str, text: str, prespace: str) -> str: """ @@ -125,7 +108,7 @@ def _overline_helper(self, snippet: str, text: str, prespace: str) -> str: part that is highlighted as ERROR. Returns the corresponding snippet as a result. """ overline = "‾" * len(text) - snippet += self._BREAK # So that we add overline under the line (row) that error occurs snippet += prespace + overline + snippet += self._BREAK # So that we add overline under the line (row) that error occurs return snippet From b0f98350cdbae8e5884774f144e598eafe27107c Mon Sep 17 00:00:00 2001 From: umututku03 Date: Tue, 10 Oct 2023 17:46:50 -0400 Subject: [PATCH 06/11] Clarification for add_line method --- python_ta/reporters/plain_reporter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 26dbcdde2..c5918e9aa 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -93,7 +93,9 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = snippet_so_far = super()._add_line(lineno, linetype, slice_, text) if linetype == LineType.ERROR: start = slice_.start or 0 - prespace = (7 + start) * self._SPACE + prespace = ( + 7 + start + ) * self._SPACE # number 7 for prespaces included for adding line number snippet_so_far = self._overline_helper(snippet_so_far, text[slice_], prespace) elif linetype == LineType.DOCSTRING: From 30fd1393eceb8caf4d1d6f3500ce099225716559 Mon Sep 17 00:00:00 2001 From: Utku Egemen Umut <121046082+umututku03@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:51:39 -0400 Subject: [PATCH 07/11] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61177e0e5..4f41ff372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased +### Enhancements +- Both PlainReporter and ColorReporter emphasize specific code chunks by using overline characters under any part that is highlighted as ERROR. + ## [2.6.2] - 2023-09-22 ### Bug fixes From 7f9d444c96bee5d82320d26ec14fce7dd4af54ee Mon Sep 17 00:00:00 2001 From: umututku03 Date: Tue, 10 Oct 2023 17:53:05 -0400 Subject: [PATCH 08/11] Getting rid of unused import in ColorReporter --- python_ta/reporters/color_reporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index b28a52cad..a9569a753 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -3,7 +3,6 @@ from colorama import Back, Fore, Style, colorama_text from pylint.interfaces import IReporter -from .node_printers import LineType from .plain_reporter import PlainReporter From 04a5d7f223b4929b780939577756866e506d1b73 Mon Sep 17 00:00:00 2001 From: Utku Egemen Umut <121046082+umututku03@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:53:49 -0400 Subject: [PATCH 09/11] Update color_reporter.py --- python_ta/reporters/color_reporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_ta/reporters/color_reporter.py b/python_ta/reporters/color_reporter.py index b28a52cad..a9569a753 100644 --- a/python_ta/reporters/color_reporter.py +++ b/python_ta/reporters/color_reporter.py @@ -3,7 +3,6 @@ from colorama import Back, Fore, Style, colorama_text from pylint.interfaces import IReporter -from .node_printers import LineType from .plain_reporter import PlainReporter From eca5148ca2cb0f4a3bb728ef35adeba1b1d444a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 03:23:51 +0000 Subject: [PATCH 10/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9725a92ff..ef7ee0878 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased - ### Enhancements + - Both PlainReporter and ColorReporter emphasize specific code chunks by using overline characters under any part that is highlighted as ERROR. ## [2.6.3] - 2023-10-09 From e3ae6a241693755c8b75ff25285618a241788c19 Mon Sep 17 00:00:00 2001 From: umututku03 Date: Mon, 16 Oct 2023 17:40:29 -0400 Subject: [PATCH 11/11] Fixing styling issues for reporters. --- python_ta/reporters/core.py | 6 ++++-- python_ta/reporters/plain_reporter.py | 18 ++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/python_ta/reporters/core.py b/python_ta/reporters/core.py index e0e09065a..c233a9166 100644 --- a/python_ta/reporters/core.py +++ b/python_ta/reporters/core.py @@ -62,6 +62,8 @@ class PythonTaReporter(BaseReporter): _BREAK = "\n" _COLOURING = {} _PRE_LINE_NUM_SPACES = 2 + _NUM_LENGTH_SPACES = 3 + _AFTER_NUM_SPACES = 2 # The error messages to report, mapping filename to a list of messages. messages: Dict[str, List[Message]] @@ -192,11 +194,11 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = def _add_line_number(self, lineno: int, linetype: LineType) -> str: """Return a formatted string displaying a line number.""" pre_spaces = self._PRE_LINE_NUM_SPACES * self._SPACE - spaces = 2 * self._SPACE + spaces = self._AFTER_NUM_SPACES * self._SPACE if lineno is not None: number = "{:>3}".format(lineno) else: - number = 3 * self._SPACE + number = self._NUM_LENGTH_SPACES * self._SPACE if linetype == LineType.ERROR: return pre_spaces + self._colourify("gbold-line", number) + spaces diff --git a/python_ta/reporters/plain_reporter.py b/python_ta/reporters/plain_reporter.py index 1a76a50d8..322d4b821 100644 --- a/python_ta/reporters/plain_reporter.py +++ b/python_ta/reporters/plain_reporter.py @@ -88,27 +88,25 @@ def _add_line(self, lineno: int, linetype: LineType, slice_: slice, text: str = Called by _build_snippet, relies on _colourify. """ + lineno_spaces = self._PRE_LINE_NUM_SPACES + self._NUM_LENGTH_SPACES + self._AFTER_NUM_SPACES snippet_so_far = super()._add_line(lineno, linetype, slice_, text) if linetype == LineType.ERROR: start = slice_.start or 0 prespace = ( - 7 + start + lineno_spaces + start ) * self._SPACE # number 7 for prespaces included for adding line number - snippet_so_far = self._overline_helper(snippet_so_far, text[slice_], prespace) + snippet_so_far += self._overline_helper(text[slice_], prespace) elif linetype == LineType.DOCSTRING: - prespace = (7 + len(text) - len(text.lstrip(" "))) * self._SPACE - snippet_so_far = self._overline_helper(snippet_so_far, text.lstrip(" "), prespace) + prespace = (lineno_spaces + len(text) - len(text.lstrip(" "))) * self._SPACE + snippet_so_far += self._overline_helper(text.lstrip(" "), prespace) return snippet_so_far - def _overline_helper(self, snippet: str, text: str, prespace: str) -> str: + def _overline_helper(self, text: str, prespace: str) -> str: """ - Helper method _add_line. Adds the Unicode203E (the overline character "‾") under any + Helper method _add_line. Adds the Unicode U+203E (the overline character "‾") under any part that is highlighted as ERROR. Returns the corresponding snippet as a result. """ overline = "‾" * len(text) - snippet += prespace + overline - snippet += self._BREAK # So that we add overline under the line (row) that error occurs - - return snippet + return prespace + overline + self._BREAK