-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take the FORCE_COLOR
and NO_COLOR
environnement variables into account
#3995
Comments
I noticed that if
then output is not colored. Steps to reproduce:
Unfortunately, importing isort has a side effect of overwriting sys.stdout! I added import sys
print(f"before 'import isort.api': {sys.stdout = }")
import isort.api
print(f"after 'import isort.api': {sys.stdout = }") Then I ran pylint: pylint --output-format=colorized test1.py > report3.log report3.log shows that sys.stdout is overwritten:
colorama source code indicates that it strips ANSI sequences in our usecase (see # should we strip ANSI sequences from our output?
if strip is None:
strip = conversion_supported or (not self.stream.closed and not self.stream.isatty())
self.strip = strip Interestingly, if PYCHARM_HOSTED environment variable is set, colorama doesn't modify Not sure where is the right place to fix the problem. |
It is a shame that pylint never managed to act on |
What is |
Command line argument for controlling color were always prove to be hard or impossible to use because the user does not always have the power to change the final arguments of each called application (think about multiple layers). Instead environment variables are passed by default to subprocesses, so they care easier to use. For example on CI you can easily define one of them in order to either activate or deactivate coloring, based on your supported features. There is even a famous website https://no-color.org/ which popularized one of the options, but when they started they failed to think about the opposite use case, still searching only you will find that most tools added support for it. Among those that I know to support it: rich, hatch, pytest, molecule, ansible-lint. Apparently pylint does not support even the PY_COLORS which was very popular to but people argues that too limitative to python. I guess we could say that NO_COLOR and FORCE_COLOR are the least biased and most future approaches, at least they do not add the APP_ suffix which for these two was mistake because it would have never scale. If you have 200 cli tools, do you want to add 200 variables with APP_FORCE_COLOR? ;) - So far I am not aware of any practical use-case where it was need to enable colors for one app but not for others (even if that would be possible by overriding the vars). I hope it helps. Feel free to look at one implementation from https://github.com/pycontribs/enrich/blob/main/src/enrich/console.py#L65 which implemented most known ones. That was written at a moment when rich itself did not has support, but later added making is less needed. To give a practical example: when using pylint as a pre-commit hook, one must add the options inside pre-commit hook because pylint does not have a variable to control coloring. Basically the repo owner have to forced his preference inside the code, as there is no way to enabled/disable it without changing the code. |
Implementing |
WorkaroundSet an environment variable
|
I'd PR those, if repo thinks they'd be interested. Of course, it might get trickier with multiple |
I think we want to support What should happen for each possible combination of FORCE_COLOR / NO_COLOR / --output-format=colorized ? How can we make this consistent ? Are we making breaking changes to keep this sane (it's the proper season) ? Modifying the value of
|
IMHO order is:
❔ 😅
Supporting at least Indeed pylint is a nice player - its output is by-default ASCII, and can be colorized.
That would be my go-to approach to solve this. I think hooking anywhere else might be too hard - since
That we can do. Also notify the user that environment variables overrid the given configuration, if you think so |
Hmm I think we have three states for each env var, false, true and not set. It gets pretty complicated, what do you think of this recap, did I understand what you said ?
|
There's a good comment there summarizing how to use these env vars: termcolor/termcolor#25 (comment) |
I think it will be better to just go with So, we end up with
which is more manageable. And also seems to be the consensus. Including the warnings (if we go with them), there won't be any magic ✨ why X is happening. PS: Thank you @hydrargyrum for the link 🙃 |
Maybe |
Indeed, https://no-color.org/ (and the aforementioned comment) states:
|
Hmm so it's |
I didn't test it with code yet - but I promise that thin wrapper will have explicit tests 😅 |
FORCE_COLOR
and NO_COLOR
environnement variables into account
Signed-off-by: Stavros Ntentos <[email protected]>
Signed-off-by: Stavros Ntentos <[email protected]>
Signed-off-by: Stavros Ntentos <[email protected]>
Signed-off-by: Stavros Ntentos <[email protected]>
Steps to reproduce
pylint --output-format=colorized project/
Current behavior
Output without color
Expected behavior
Output with color
pylint --version output
Additional information
I´m running pylint within an GitLab Pipeline
The text was updated successfully, but these errors were encountered: