Is it possible to change max-line-length settings for one file out of a project (while performing all other checks defined in rc file on it)?
Ideally, it should behave like inline pylint: disable=x comments.
I've tried putting this line at the module level:
# pylint: max-line-length=240
PyLint failed to recognize it:
my_file.py:15: [E0011(unrecognized-inline-option), ] Unrecognized file option 'max-line-length
Edit: I know I can disable line-too-long check entirely, but to be honest I'd like to avoid doing so, just in case anyone would try to extend this module and add lines even longer than they are now.
You can create .pylintrc file in your python script to overwrite pylint settings and put inside
[FORMAT]
max-line-length=240
edit 240 based on your choice.
According to the doc, I think you cannot modify the pylint config in line.
but you can disable the warning for only one or few line(s) with # pylint: disable=line-too-long:
# disable for only one line
ridiculously_long_variable_name = "this is not a ridiculously long and useless python line" # pylint: disable=line-too-long
# disable for few (or more) lines
# pylint: disable=line-too-long
ridiculously_long_variable_name = "this is not a ridiculously long and useless python line"
# pylint: enable=line-too-long
pylint --max-line-length=240
works for me.
You can pass the additional as below:
C:\Python27\Scripts\pylint.exe --max-line-length=240 <PATH TO FILE>
Related
The following Python fragment code gets analyzed by Pylint:
if type(result) is array.array:
read = result.tobytes()
... with the following error for the last line:
E:401,22: Instance of 'int' has no 'tobytes' member\
(but some types could not be inferred) (maybe-no-member)
The result variable is received from an external function. How can I change (correct) the code to make Pylint understand? Or how can I tell it that the result of the function can have other types than int? Or how can I tell it to ignore that particular line? (I favor an answer in this order of the questions)
For some reason, pylint doesn't get 'result' may be of the array type (and will be for sure under the 'if' branch). There is currently no way to tell pylint about that, though it will hopefully be possible at some point. So for now, you can only disable the warning for that specific line by adding # pylint: disable=maybe-no-member after the offending statement or right above it. For example:
if type(result) is array.array:
read = result.tobytes() # pylint: disable=maybe-no-member
or
if type(result) is array.array:
# pylint: disable=maybe-no-member
read = result.tobytes()
I disabled all no-member warnings by passing this command line option to pylint
--disable=E1101
While using PyLint you can generate a configuration pylint file using these command in your terminal
$ pylint --generate-rcfile > .pylintrc
the generated file handles the errors and warnings that you have to check.
for the no member in particular you can go to line 560, you can find that it ignored.
this configurations will be applied to all your code files.
and you can edit this configuration file, to meet your requirements.
Instead of
result.tobytes(),
use
getattr(result, 'tobytes')()
I am using pylint 0.27 with python 2.7.3. Pylint has a known bug which hits when it analyses code having a .next() call. As given in http://www.logilab.org/122793 link, it fails with the given traceback.
I cannot change my python and pylint versions but I would like to workaround this problem by disabling pylint on the piece of code that has .next() call by adding a #pylint: MAGIC comment in my code.
I could find support for disabling pylint checking on a file using #pylint: skip-file but I am interested at doing this at function level or rather at line level.
Any other workarounds are also welcomed!
You can accomplish this by adding the pylint comment as the first line of the function body like so:
def wont_raise_pylint():
# pylint: disable=W0212
some_module._protected_member()
some_module._protected_member()
def will_still_raise_pylint():
some_module._protected_member()
When I refactor code, I wrap the portion I have yet to touch
in a class ToDo and start that class with a directive
to ignore all messages. At the end of the class the directive
goes out of scope. MWE:
def unused_variable(foo=''):
"generate W0613"
class ToDo:
#pylint: disable = E, W, R, C
#disables W0613 and E0213
def unused_variable(foo=''):
"generate W0613 and E0213"
def main(foo=''):
"generate W0613"
To disable pylint entirely for a function without needing to enumerate each pylint violation code, specify all to the disable, eg:
def foo():
# pylint: disable=all
"""You put the disable right under the function signature
to disable pylint for the entire function scope.
"""
pass
Unfortunatly you won't be able to locally avoid the error you encounter.
One could expect to locate in the source code the offending piece of code, and then locally disable the involved message(s), but this won't work because it will still be run. This is because when locally disabling a message, the code detecting this message is run, only the message isn't be emitted in the end.
However, it may work if you globally disable this message (depending on the way it's implemented). In your case it seems unfortunatly that you would have to skip the whole 'logging' checker.
To sum up, to avoid your traceback you may either:
locally use pylint: skip-file, in which case every pylint's features will be activated but the whole offending file will be skipped
globally disable the 'logging' checker (--disable=logging), in which case the whole code (including the offending file) will be checked but without the 'logging' checker messages
I know that I can disable pylint warnings by leaving a comment # pylint: disable=XXXX.
How do I do the same thing for pep257 errors?
1 C0110 Exported classes should have docstrings. [pep257]
2 C0110 Exported definitions should have docstrings. [pep257]
I am writing unit tests and (I believe) I do not need to worry about docstrings for every single test method - everything is quite self-explanatory.
I am using the https://github.com/klen/python-mode.
Assuming you followed the recommended pathogen installation,
.vim/bundle/python-mode/pylint.ini
has a disable = line to which you may add C0110
Question is simple, I have two lines in my Python script:
# pylint: disable=C0301, W0212
#!/usr/bin/env python
Both of them have to be on the first line otherwise they will not work.
How do you suggest I resolve this?
The following goes on the first line:
#!/usr/bin/env python
Pylint messages can go on any line. As the documentation says:
Is it possible to locally disable a particular message? This may be done by adding “#pylint: disable=W0123,E4567” at the desired block level or at the end of the desired line of code
So the message needs to be at the same block level (so the second line would work as well as that's the same block / scope). Furthermore the documentation says:
Is there a way to disable a message for a particular module only? Yes, you can disable or enable (globally disabled) messages at the module level by adding the corresponding option in a comment at the top of the file:
So it doesn't say: "first line of the file"; as long as the disable message is at the top of the file then it's fine.
Pylint disable comments do not need to be in the top line to work.
A lot of our modules start with:
try:
import json
except ImportError:
from django.utils import simplejson as json # Python 2.4 fallback.
...and it's the only Pyflakes warning in the entire file:
foo/bar.py:14: redefinition of unused 'json' from line 12
How can I get Pyflakes to ignore this?
(Normally I'd go read the docs but the link is broken. If nobody has an answer, I'll just read the source.)
If you can use flake8 instead - which wraps pyflakes as well as the pep8 checker - a line ending with
# NOQA
(in which the space is significant - 2 spaces between the end of the code and the #, one between it and the NOQA text) will tell the checker to ignore any errors on that line.
I know this was questioned some time ago and is already answered.
But I wanted to add what I usually use:
try:
import json
assert json # silence pyflakes
except ImportError:
from django.utils import simplejson as json # Python 2.4 fallback.
Yep, unfortunately dimod.org is down together with all goodies.
Looking at the pyflakes code, it seems to me that pyflakes is designed so that it will be easy to use it as an "embedded fast checker".
For implementing ignore functionality you will need to write your own that calls the pyflakes checker.
Here you can find an idea: http://djangosnippets.org/snippets/1762/
Note that the above snippet only for for comments places on the same line.
For ignoring a whole block you might want to add 'pyflakes:ignore' in the block docstring and filter based on node.doc.
Good luck!
I am using pocket-lint for all kind of static code analysis. Here are the changes made in pocket-lint for ignoring pyflakes: https://code.launchpad.net/~adiroiban/pocket-lint/907742/+merge/102882
To quote from the github issue ticket:
While the fix is still coming, this is how it can be worked around, if you're wondering:
try:
from unittest.runner import _WritelnDecorator
_WritelnDecorator; # workaround for pyflakes issue #13
except ImportError:
from unittest import _WritelnDecorator
Substitude _unittest and _WritelnDecorator with the entities (modules, functions, classes) you need
-- deemoowoor
Here is a monkey patch for pyflakes that adds a # bypass_pyflakes comment option.
bypass_pyflakes.py
#!/usr/bin/env python
from pyflakes.scripts import pyflakes
from pyflakes.checker import Checker
def report_with_bypass(self, messageClass, *args, **kwargs):
text_lineno = args[0] - 1
with open(self.filename, 'r') as code:
if code.readlines()[text_lineno].find('bypass_pyflakes') >= 0:
return
self.messages.append(messageClass(self.filename, *args, **kwargs))
# monkey patch checker to support bypass
Checker.report = report_with_bypass
pyflakes.main()
If you save this as bypass_pyflakes.py, then you can invoke it as python bypass_pyflakes.py myfile.py.
http://chase-seibert.github.com/blog/2013/01/11/bypass_pyflakes.html
Flake gives you some options to ignore violations.
My favorite one is to make it explicit and ignore the specific violation inline:
my invalid code # noqa: WS03
And you have the others already cited options.
Ignore all validations in the line:
my invalid code # NOQA
Ignore all validations in the file. Put in its first line:
# flake8: noqa: E121, E131, E241, F403, F405
Or configure to ignore it as a parameter in you flake8 configuration.
You can also import with __import__. It's not pythonic, but pyflakes does not warn you anymore. See documentation for __import__ .
try:
import json
except ImportError:
__import__('django.utils', globals(), locals(), ['json'], -1)
I created a little shell script with some awk magic to help me. With this all lines with import typing, from typing import or #$ (latter is a special comment I am using here) are excluded ($1 is the file name of the Python script):
result=$(pyflakes -- "$1" 2>&1)
# check whether there is any output
if [ "$result" ]; then
# lines to exclude
excl=$(awk 'BEGIN { ORS="" } /(#\$)|(import +typing)|(from +typing +import )/ { print sep NR; sep="|" }' "$1")
# exclude lines if there are any (otherwise we get invalid regex)
[ "$excl" ] &&
result=$(awk "! /^[^:]+:(${excl}):/" <<< "$result")
fi
# now echo "$result" or such ...
Basically it notes the line numbers and dynamically creates a regex out it.
For flake8, which is recommended alternative (compare flake8 vs pyflakes here)
Add the first line like:
# flake8: noqa: E121, E131, E241, F403, F405
in general:
# flake8: noqa: <code>[, <code> ...]
This way in one line you can silent the entire file and do it for many ignore statements at once, which is often a case.