I want to check a file using pycodestyle
. I've tried using what their docs say:
import pycodestyle
fchecker = pycodestyle.Checker('testsuite/E27.py')
file_errors = fchecker.check_all()
# I took off the show_source=True and the final print
It prints the errors, but file_errors
is the NUMBER of errors, not the errors themselves. I want the errors to be returned in a list. How can I do that using pycodestyle?
pycodestyle
is a module that checks code against the PEP8 guidelines. Usually, it is used with the command line, but I want to automate it by putting it into a script. Using the docs, you get:
import pycodestyle
fchecker = pycodestyle.Checker('testsuite/E27.py', show_source=True)
file_errors = fchecker.check_all()
print("Found %s errors (and warnings)" % file_errors)
This prints the errors and the total amount of errors. However, file_errors
is not a list- it's the number of errors.
I would want a way to get a list from pycodestyle.Checker
(or any thing in pycodestyle). How can I do that?
What I've done: I've looked on google, and skimmed pycodestyle
's docs, but nothing is mentioned.
From skimming the source code, it doesn't seem to have any way to return the errors, just print them. So you can capture its stdout instead.
from contextlib import redirect_stdout
import io
f = io.StringIO() # Dummy file
with redirect_stdout(f):
file_errors = fchecker.check_all()
out = f.getvalue().splitlines() # Get list of lines from the dummy file
print(file_errors, out)
This code is based on ForeverWintr's answer
For example, run it on a file like this:
s = 0
Output:
1 ['tmp.py:1:2: E221 multiple spaces before operator']