Skip to content

Commit 51cf8d2

Browse files
authored
Remove PY_COLORS, fix spelling and don't modify in place
1 parent 4427cf3 commit 51cf8d2

File tree

6 files changed

+38
-58
lines changed

6 files changed

+38
-58
lines changed

custom_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ encodings
108108
endswith
109109
enum
110110
enums
111+
env
111112
epilog
112113
epylint
113114
epytext

doc/user_guide/usage/output.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ The colorization of the output can also be controlled through environment
2727
variables. The precedence for determining output format is as follows:
2828

2929
1. ``NO_COLOR``
30-
2. ``FORCE_COLOR``, or ``PY_COLORS``
30+
2. ``FORCE_COLOR``
3131
3. ``--output-format=...``
3232

3333
Setting ``NO_COLOR`` (to any value) will disable colorized output, while
34-
``FORCE_COLOR`` or ``PY_COLORS`` (to any value) will enable it, overriding
35-
the ``--output-format`` option if specified.
34+
``FORCE_COLOR`` (to any value) will enable it, overriding the
35+
``--output-format`` option if specified.
3636

3737

3838
Custom message formats

doc/whatsnew/fragments/3995.feature

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Support for ``NO_COLOR`` and ``FORCE_COLOR`` (and ``PY_COLORS``, as an alias to ``FORCE_COLOR``) environment variables has been added.
2-
When running `pylint`, the reporter that reports to ``stdout`` will be modified according to the requested mode.
3-
The order is: ``NO_COLOR`` > ``FORCE_COLOR``, ``PY_COLORS`` > ``--output=...``.
1+
Support for ``NO_COLOR`` and ``FORCE_COLOR`` environment variables has been added.
2+
When running `pylint`, the reporter that reports to ``stdout`` will be modified according
3+
to the requested mode.
4+
The order is: ``NO_COLOR`` > ``FORCE_COLOR`` > ``--output=...``.
45

56
Closes #3995 (https://github.com/pylint-dev/pylint/issues/3995).

pylint/lint/pylinter.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474

7575
NO_COLOR = "NO_COLOR"
7676
FORCE_COLOR = "FORCE_COLOR"
77-
PY_COLORS = "PY_COLORS"
7877

7978
WARN_FORCE_COLOR_SET = "FORCE_COLOR is set; ignoring `text` at stdout"
8079
WARN_NO_COLOR_SET = "NO_COLOR is set; ignoring `colorized` at stdout"
@@ -261,10 +260,9 @@ def _load_reporter_by_class(reporter_class: str) -> type[BaseReporter]:
261260
}
262261

263262

264-
def _handle_force_color_no_color(reporter: list[reporters.BaseReporter]) -> None:
263+
def _handle_force_color_no_color(reporters: list[reporters.BaseReporter]) -> list[reporters.BaseReporter]:
265264
"""
266-
Check ``NO_COLOR``, ``FORCE_COLOR``, ``PY_COLOR`` and modify the reporter list
267-
accordingly.
265+
Check ``NO_COLOR`` and ``FORCE_COLOR``, and return the modified reporter list accordingly.
268266
269267
Rules are presented in this table:
270268
+--------------+---------------+-----------------+------------------------------------------------------------+
@@ -281,9 +279,7 @@ def _handle_force_color_no_color(reporter: list[reporters.BaseReporter]) -> None
281279
+--------------+---------------+-----------------+------------------------------------------------------------+
282280
"""
283281
no_color = _is_env_set_and_non_empty(NO_COLOR)
284-
force_color = _is_env_set_and_non_empty(FORCE_COLOR) or _is_env_set_and_non_empty(
285-
PY_COLORS
286-
)
282+
force_color = _is_env_set_and_non_empty(FORCE_COLOR)
287283

288284
if no_color and force_color:
289285
warnings.warn(
@@ -293,34 +289,31 @@ def _handle_force_color_no_color(reporter: list[reporters.BaseReporter]) -> None
293289
)
294290
force_color = False
295291

296-
if no_color:
297-
for idx, rep in enumerate(list(reporter)):
298-
if not isinstance(rep, ColorizedTextReporter):
299-
continue
300-
301-
if rep.out.buffer is sys.stdout.buffer:
302-
warnings.warn(
303-
WARN_NO_COLOR_SET,
304-
ReporterWarning,
305-
stacklevel=2,
306-
)
307-
reporter.pop(idx)
308-
reporter.append(TextReporter())
309-
310-
elif force_color:
311-
for idx, rep in enumerate(list(reporter)):
312-
# pylint: disable=unidiomatic-typecheck # Want explicit type check
313-
if type(rep) is not TextReporter:
314-
continue
292+
final_reporters: list[reporters.BaseReporter] = []
293+
294+
for idx, rep in enumerate(reporters):
295+
if no_color and isinstance(rep, ColorizedTextReporter) and rep.out.buffer is sys.stdout.buffer:
296+
warnings.warn(
297+
WARN_NO_COLOR_SET,
298+
ReporterWarning,
299+
stacklevel=2,
300+
)
301+
final_reporters.append(TextReporter())
302+
303+
304+
# pylint: disable=unidiomatic-typecheck # Want explicit type check
305+
elif force_color and type(rep) is TextReporter and rep.out.buffer is sys.stdout.buffer:
306+
warnings.warn(
307+
WARN_FORCE_COLOR_SET,
308+
ReporterWarning,
309+
stacklevel=2,
310+
)
311+
final_reporters.append(ColorizedTextReporter())
315312

316-
if rep.out.buffer is sys.stdout.buffer:
317-
warnings.warn(
318-
WARN_FORCE_COLOR_SET,
319-
ReporterWarning,
320-
stacklevel=2,
321-
)
322-
reporter.pop(idx)
323-
reporter.append(ColorizedTextReporter())
313+
else:
314+
final_reporters.append(rep)
315+
316+
return final_reporters
324317

325318

326319
# pylint: disable=too-many-instance-attributes,too-many-public-methods
@@ -508,7 +501,7 @@ def _load_reporters(self, reporter_names: str) -> None:
508501
# Extend the lifetime of all opened output files
509502
close_output_files = stack.pop_all().close
510503

511-
_handle_force_color_no_color(sub_reporters)
504+
sub_reporters = _handle_force_color_no_color(sub_reporters)
512505

513506
if len(sub_reporters) > 1 or output_files:
514507
self.set_reporter(

tests/lint/test_pylinter.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
FORCE_COLOR,
2121
MANAGER,
2222
NO_COLOR,
23-
PY_COLORS,
2423
WARN_BOTH_COLOR_SET,
2524
PyLinter,
2625
_handle_force_color_no_color,
@@ -139,11 +138,6 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
139138
[True, False],
140139
ids=lambda no_color: f"{no_color=}",
141140
)
142-
@pytest.mark.parametrize(
143-
"py_colors",
144-
[True, False],
145-
ids=lambda py_colors: f"{py_colors=}",
146-
)
147141
@pytest.mark.parametrize(
148142
"force_color",
149143
[True, False],
@@ -153,16 +147,12 @@ def test_handle_force_color_no_color(
153147
monkeypatch: pytest.MonkeyPatch,
154148
recwarn: pytest.WarningsRecorder,
155149
no_color: bool,
156-
py_colors: bool,
157150
force_color: bool,
158151
text_reporters: tuple[str],
159152
colorized_reporters: tuple[str],
160153
) -> None:
161154
monkeypatch.setenv(NO_COLOR, "1" if no_color else "")
162155
monkeypatch.setenv(FORCE_COLOR, "1" if force_color else "")
163-
monkeypatch.setenv(PY_COLORS, "1" if py_colors else "")
164-
165-
force_color = force_color or py_colors
166156

167157
if STDOUT_TEXT in text_reporters or STDOUT_TEXT in colorized_reporters:
168158
monkeypatch.setattr(sys, STDOUT_TEXT, io.TextIOWrapper(io.BytesIO()))

tests/lint/test_utils.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,17 @@ def test_issue_template_on_fatal_errors(capsys: pytest.CaptureFixture) -> None:
7070
("", False),
7171
(0, True),
7272
(1, True),
73-
(2, True),
7473
(False, True),
75-
("no", True),
76-
("off", True),
7774
("on", True),
78-
(True, True),
79-
("yes", True),
8075
],
8176
ids=repr,
8277
)
8378
def test_is_env_set_and_non_empty(
84-
monkeypatch: pytest.MonkeyPatch, value: Any, expected: bool
79+
monkeypatch: pytest.MonkeyPatch, value: object, expected: bool
8580
) -> None:
8681
"""Test the function returns True if the environment variable is set and non-empty."""
8782
env_var = "TEST_VAR"
8883
if value is not None:
8984
monkeypatch.setenv(env_var, str(value))
9085

91-
assert _is_env_set_and_non_empty(env_var) == expected
86+
assert _is_env_set_and_non_empty(env_var) is expected

0 commit comments

Comments
 (0)