wemake-python-styleguide
wemake-python-styleguide copied to clipboard
Add `wemake-plain` output formatter
It should be the same as wemake
, but without colors. Why?
When I try to save the output to file, I get this:
106:29 WPS220 Found too deep nesting: 28 > 20
[34mtry[39;49;00m:
^
And I want to have this:
106:29 WPS220 Found too deep nesting: 28 > 20
try:
^
I can help with this if it is still available!
Thanks a lot, @ydocsgnillats!
To make sure I'm understanding correctly, I need to make an output formatter that is similar to the existing one, but without including different colors/styling? What file is the existing output formatter in?
I need to make an output formatter that is similar to the existing one, but without including different colors/styling?
Yes.
What file is the existing output formatter in?
formatter.py
🙂
Hey @sobolevn, can I take this one?
Sure! Thanks!
@sobolevn I couldn't reproduce the bug. Am I missing something?
Making a test file test.py
:
# test.py
1 * 0
and running:
$ flake8 --format wemake test.py > output
gives in output
:
test.py
2:1 WPS428 Found statement that has no effect
1 * 0
^
2:1 WPS345 Found meaningless number operation
1 * 0
^
2:5 WPS345 Found meaningless number operation
1 * 0
^
Full list of violations and explanations:
https://wemake-python-stylegui.de/en/0.14.1/pages/usage/violations/
Or we can add NO_COLOR
support https://no-color.org/
Which is better in my perspective
Ok, just to make sure I understand correctly,
Current behaviour:
- Output has color codes if outputted to the terminal
- Output doesn't have color codes if piped into another program or a file
- Setting
NO_COLOR
does not change the behaviour
Desired behaviour
- Presence of color codes depends solely on whether or not
NO_COLOR
environment variable is defined
Am I correct?
Output has color codes if outputted to the terminal
Correct!
Output doesn't have color codes if piped into another program or a file
Well, that's complicated. Because it might be very tricky. Different shells, different terminals, different OSes, different programs, different pipe methods, etc. I cannot say that it works. But, I cannot say that it does not work.
Presence of color codes depends solely on whether or not NO_COLOR environment variable is defined
Yes, this way we would have smooth way to control the output without the need to tweak the formatter manually.
Hey I'm a beginner and complete newbie. I would like to attempt and solve this issue. Can you please guide me through the process and assign me this issue?
Sure, @0x00zer0day! You need to tweak https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/formatter.py#L52 class. My understanding of this problem has changed.
So:
- If
FORCE_COLOR
env var is set to0
, do not add colored output - Otherwise, keep things as-is
Sure, @0x00zer0day! You need to tweak https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/formatter.py#L52 class. My understanding of this problem has changed.
So:
- If
FORCE_COLOR
env var is set to0
, do not add colored output- Otherwise, keep things as-is
Just making sure if I'm on the right track here, I need to check if the system environment variables has the FORCE_COLOR
flag set to 0
to disable colored output right?
Yes :)
@sobolevn, Sorry for being a complete noob here but does this seem correct? :
class WemakeFormatter(BaseFormatter): # noqa: WPS214
"""
We need to format our style :term:`violations <violation>` beatifully.
The default formatter does not allow us to do that.
What things do we miss?
1. Spacing, everything is just mixed up and glued together
2. Colors and decoration, some information is easier
to gather just with colors or underlined text
3. Grouping, we need explicit grouping by filename
4. Incomplete and non-informative statistics
"""
if bool(os.environ.get('FORCE_COLOR')) is False:
_doc_url: ClassVar[str] = DOCS_URL_TEMPLATE.format(pkg_version)
# API:
def after_init(self):
"""Called after the original ``init`` is used to set extra fields."""
self._lexer = PythonLexer()
self._formatter = TerminalFormatter()
...
Nope, you should change these three functions: https://github.com/wemake-services/wemake-python-styleguide/blob/d1d6f9541b1da85f3fb547ad761b290456c623e1/wemake_python_styleguide/formatter.py#L203-L238
@sobolevn , So if i understand this correctly, I should perform os.environ.get('FORCE_COLOR')
check on these three functions right?
Yes :)
@sobolevn, Is this correct?
# Formatting text:
force_color_check = str(int(bool(environ.get('FORCE_COLOR'))))
def _bold(text: str) -> str:
r"""
Returns bold formatted text.
>>> _bold('Hello!')
'\x1b[1mHello!\x1b[0m'
"""
# return '\033[1m{0}\033[0m'.format(text)
if force_color_check == '0':
return '\033[1m{0}\033[0m'.format(text)
else:
return text
def _underline(text: str) -> str:
r"""
Returns underlined formatted text.
>>> _underline('Hello!')
'\x1b[4mHello!\x1b[0m'
"""
if force_color_check == '0':
return '\033[4m{0}\033[0m'.format(text)
else:
return text
def _highlight(source: str, lexer, formatter) -> str:
"""
Highlights source code. Might fail.
See also:
https://github.com/wemake-services/wemake-python-styleguide/issues/794
"""
if force_color_check == '0':
try:
return highlight(source, lexer, formatter)
except Exception: # pragma: no cover
# Might fail on some systems, when colors are set incorrectly,
# or not available at all. In this case code will be just text.
return source
else:
return source
_DISABLE_COLORS: Final = environ.get('FORCE_COLOR', '1') == '0'
def _bold(text: str) -> str:
if _DISABLE_COLORS:
return text
return ...
@sobolevn pardon my sloppy code. Have opened up a PR, please do check if I have done anything wrong