pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Colorized output should not modify `msg` objects

Open tom65536 opened this issue 3 years ago • 0 comments

Bug description

I noticed this problem because I use colorized text output alongside with the pylint-gitlab plugin. I filed an issue there as well, but I agree with the author of the plugin that the bug should be fixed within PyLint.

I use the pylint-gitlab reporter and colorized text output in the same run of pylint (see below). The plugin receives the messages modified by the ColorizedTextReporter. This causes an exception (KeyError) because the plugin receives the ANSI-color-coded category \x1b[1mconvention\x1b[0m instead of simply convention.

The modification happens inside the handle_message method:

        msg.msg = colorize_ansi(msg.msg, msg_style)
        msg.symbol = colorize_ansi(msg.symbol, msg_style)
        msg.category = colorize_ansi(msg.category, msg_style)
        msg.C = colorize_ansi(msg.C, msg_style)

Suggested solution:

As I haven't found a way of controlling the order in which the reporters are processed (the behavior would not be a problem if one could ensure that the ColorizedTextReporter is called last), I would suggest that the reporter should be changed to work in a re-entrant manner: The ColorizedTextReporter should not modify the original fields. It should rather add some extra fields (e.g. colorized_category) or keep a list of copies of the original msg objects.

Configuration

No response

Command used

python -m pylint --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter:pylint-codeclimate.json,pylint_gitlab.GitlabPagesHtmlReporter:index.html,text:pylint.txt,colorized src

Pylint output

File "xxx\python-3.8.9\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "xxx\python-3.8.9\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint\__main__.py", line 10, in <module>
    pylint.run_pylint()
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint\__init__.py", line 25, in run_pylint
    PylintRun(argv or sys.argv[1:])
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint\lint\run.py", line 205, in __init__
    score_value = linter.generate_reports()
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint\lint\pylinter.py", line 998, in generate_reports
    self.reporter.display_messages(report_nodes.Section())
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint\reporters\multi_reporter.py", line 95, in display_messages
    rep.display_messages(layout)
  File "xxx\.nox\lint_pylint\lib\site-packages\pylint_gitlab\reporter.py", line 69, in display_messages
    "class": self._MSG_TYPES[message.category],
KeyError: '\x1b[1mconvention\x1b[0m'

Expected behavior

ANSI-colored output to the screen plus some generated files (pylint-codeclimate.json, index.html, pylint.txt)

Pylint version

pylint-2.14.x (the problem did not occur with pylint<=3.13.x)

OS / Environment

Tested on Linux and Windows

Additional dependencies

pylint-gitlab=1.0.0

tom65536 avatar Jul 21 '22 07:07 tom65536