Why does the default pylintrc have so many disabled messages? - python

If you run pylint --generate-rcfile > pylintrc and look at the default rc file you'll see the following list of disabled warnings.
Why are they disabled by default?
disable=print-statement,
parameter-unpacking,
unpacking-in-except,
old-raise-syntax,
backtick,
long-suffix,
old-ne-operator,
old-octal-literal,
import-star-module-level,
non-ascii-bytes-literal,
raw-checker-failed,
bad-inline-option,
locally-disabled,
locally-enabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
apply-builtin,
basestring-builtin,
buffer-builtin,
cmp-builtin,
coerce-builtin,
execfile-builtin,
file-builtin,
long-builtin,
raw_input-builtin,
reduce-builtin,
standarderror-builtin,
unicode-builtin,
xrange-builtin,
coerce-method,
delslice-method,
getslice-method,
setslice-method,
no-absolute-import,
old-division,
dict-iter-method,
dict-view-method,
next-method-called,
metaclass-assignment,
indexing-exception,
raising-string,
reload-builtin,
oct-method,
hex-method,
nonzero-method,
cmp-method,
input-builtin,
round-builtin,
intern-builtin,
unichr-builtin,
map-builtin-not-iterating,
zip-builtin-not-iterating,
range-builtin-not-iterating,
filter-builtin-not-iterating,
using-cmp-argument,
eq-without-hash,
div-method,
idiv-method,
rdiv-method,
exception-message-attribute,
invalid-str-codec,
sys-max-int,
bad-python3-import,
deprecated-string-function,
deprecated-str-translate-call,
deprecated-itertools-function,
deprecated-types-field,
next-method-defined,
dict-items-not-iterating,
dict-keys-not-iterating,
dict-values-not-iterating

From the documentation's Frequently Asked Questions...
Why are there a bunch of messages disabled by default?
pylint does have some messages disabled by default, either because
they are prone to false positives or that they are opinionated enough
for not being included as default messages. But most of the disabled
messages are from the Python 3 porting checker, which is disabled by
default. It needs special activation with the --py3k flag.

I think that such default rc file is designed to apply pylint to python2 code without tons of errors and warnings. Note: most of the disabled statements belongs to python2 syntax and standard library api:
print-statement - print was a statement in Python2, in Python3 it is a function
old-raise-syntax - there was except Exception, e syntax that is invalid for Python3, in Python3 except Exception as e is only valid
xrange-builtin - xrange was replaced with range
etc.
So, with this default rc you can use pylint for python2 code to find out such things as redefined-outer-name, line-too-long and other bad things without getting annoying errors and warnings for valid Python2 syntax and standard library calls.

Related

Show all non-critical typing errors as warnings

In VSCode's settings.json, I enabled PyLance's type checking:
"python.analysis.typeCheckingMode": "basic"
This shows all typing issues as errors (underlined in red), even when code is valid Python and will run with no issue.
For example, the following code is valid Python, and works:
if 4 % 2 == 0:
a = 3
print(a)
...but PyLance shows an error because of the case where a is unbound:
I want to only mark as "errors" the actual syntax errors that will be rejected by Python, and mark everything else as warnings. I can do it for one category with:
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "warning"
}
How can I do that for all such errors?
Sorry, but I am afraid you only can override the diagnostic severity explicitly one by one.
Such as set "reportUnboundVariable": "warning", to change the error to warning which you have metioned in the above.
But, there is no way to change all of them one time.

PEP8 plugin breaks my Type Hint code in Sublime 3

I had to disable the format on save setting, because Python PEP8 Autoformat plugin reformatted my code, causing a syntax error.
My code (focus on last lines):
from typing import List, Tuple
from my_enent import MyEvent
def my_preprocessor(raw_event, context: object, env: MyEnv) \
-> Tuple[dict, VideoFreezeEvent]:
if isinstance(raw_event, dict) and 'Output' in raw_event:
# comments
raw_state_machine_event = json.loads(raw_state_machine_event['Output'])
# comments
parallel_outputs = raw_state_machine_event.get(
'my_data').get('parallel_outputs')
if len(parallel_outputs) > 0:
state_machine_event = parallel_outputs[0]
my_list: List[MyEvent] = [
my_util.populate_dataclass(MyEvent, event)
for event in parallel_outputs
]
another_event = events_list[0]
After the plugin reformats the code, the relevant part of the code that causes the syntax error becomes:
if len(parallel_outputs) > 0:
state_machine_event = parallel_outputs[0]
my_list:
List[MyEvent] = [
my_util.populate_dataclass(MyEvent, event)
for event in parallel_outputs
]
another_event = events_list[0]
How can I prevent/teach the plugin to not break this code please?
Some package settings that might be the way through, if a passage exists in the first place:
{
// list codes for fixes; used by --ignore and --select
"list-fixes": false,
// do not fix these errors / warnings (e.g. [ "E501" , "E4" , "W"])
"ignore": [],
// select errors / warnings (e.g. ["E4", "W"])
"select": [],
// Maximum line length
"max-line-length": 79
}
Your linter sounds like it is rather out of date, as it neither recognizes the walrus operator := or your type annotations. Looking at the plugin's Package Control page, you can see that up at the top it says "MISSING", which means the source code repo is gone, most likely because it's not being maintained anymore. The package was last modified 5 years ago, and there are no recent installations, so there's very strong evidence it's dead.
As a replacement plugin, I'd highly recommend Anaconda
(not related to the Anaconda Python distribution). It works great (mostly), is under active development with frequent updates, bugfixes, and new features, and does code completion and code intelligence along with linting/autoformatting. The website goes through all the configuration you need to do, and how to turn off and on the different features. There are several different linting/formatting options to choose from, including AutoPEP8, PyFlakes, and PyLint. I really like it.
(And no, I'm not associated with it or its author in any way.)
I found workaround. Go to plugin settings (Preferences -> Package Settings -> Python PEP8 Autoformat -> …), add ignore rule, e.g.:
{
// Workaround for typing hints
"ignore": ["E701"],
}
I guess it ignores this warning: https://www.flake8rules.com/rules/E701.html
Seems not very harmful.
I installed Black via pip, and used sublack Sublime plugin, which appears to be running smoothly.
Anaconda Sublime plugin suggested by MattDMo is cool, but a bit slow (with the default settings at least), any my Mac laptop is fairly new.

Python slimit minimizer unwanted warning output

from slimit import minify
if __name__ == "__main__":
print("start")
# Normally, I pass real JavaScript. For this issue, an empty string reproduces problem.
minify("", mangle=True)
print("exit")
This triggers the following console output.
start
WARNING: Couldn't write lextab module <module 'slimit.lextab' from '/Users/kurtostfeld/samba/wrapad/venv/lib/python2.7/site-packages/slimit/lextab.pyc'>. Won't overwrite existing lextab module
WARNING: yacc table file version is out of date
WARNING: Token 'IMPORT' defined, but not used
WARNING: Token 'BLOCK_COMMENT' defined, but not used
WARNING: Token 'ENUM' defined, but not used
WARNING: Token 'EXTENDS' defined, but not used
WARNING: Token 'LINE_COMMENT' defined, but not used
WARNING: Token 'LINE_TERMINATOR' defined, but not used
WARNING: Token 'CONST' defined, but not used
WARNING: Token 'EXPORT' defined, but not used
WARNING: Token 'CLASS' defined, but not used
WARNING: Token 'SUPER' defined, but not used
WARNING: There are 10 unused tokens
WARNING: Couldn't create <module 'slimit.yacctab' from '/Users/kurtostfeld/samba/wrapad/venv/lib/python2.7/site-packages/slimit/yacctab.pyc'>. Won't overwrite existing tabmodule
exit
These warnings are flooding my application console output. How can I use minify without generating warnings?
I'm using Python 2.7.12, and what is currently the latest library versions: slimit 0.8.1, ply 3.10.
According to this issue on Github, slimit depends of the ply package. After few tries, it seems that theses warnings appear since version 3.8 of ply. . You could update ply to 3.6 which is the last version that doesn't bring these messages :
pip uninstall ply -y && pip install ply==3.6
It solved my problem.
UPDATE
Install a sooner version of ply was really a bad work around since some of my tests were failing. Original slimit version seems not maintained well so I suggest to update to a newer version, metatoaster did a good job to improve it and fixed that problem of warning message. The solution for me was to uninstall slimit and then install it's version:
pip install git+https://github.com/metatoaster/slimit.git#egg=slimit
FINAL UPDATE In fact, slimit seems not maintened anymore and its successor is called calmjs, there is few differences but it is really more stable and don't shows these annoying warning message. See: https://github.com/calmjs/calmjs.parse
Switching versions did not change anything for me, I found another workaround: simply delete (or move, if you want to be cautious) the mentioned files (yourpython/site-packages/slimit/yacctab.py and yourpython/site-packages/slimit/lextab.py).
I believe the module will re-create these files and stop bothering you with warning messages.
Slimit uses ply under the hood, which uses logging from stdlib. AFAICS slimit does not allow you to pass the logging parameters that ply's lex and yacc expect.
While you therefor can't (directly) access ply's logging, you should be able to suppress those messages my raising the global logging level:
import logging
...
logging.disable(logging.CRITICAL)
minify("", mangle=True)
logging.disable(logging.NOTSET)
You can use this parser, too: https://github.com/PiotrDabkowski/pyjsparser
It works and is easy to use. It does not handle comments though.
(Neither does calmjs seems to handle comments fully: Its parse function has a parameter to indicate that you want the comments, but as of now, some comments seem to get lost.)
Here was the solution that I went with. I made a custom variant of two slimit functions that pass an extra errorlog=ply.yacc.NullLogger() call to the ply.yacc.yacc function.
class SlimitNoLoggingParser(Parser):
"""
This is a simple customized variant to slimit.parser.Parser.
The only difference is that this passes a errorlog=ply.yacc.NullLogger() to ply.yacc.yacc to suppress unwanted
stderr logging output.
"""
def __init__(self, lex_optimize=True, lextab=lextab,
yacc_optimize=True, yacctab=yacctab, yacc_debug=False):
self.lex_optimize = lex_optimize
self.lextab = lextab
self.yacc_optimize = yacc_optimize
self.yacctab = yacctab
self.yacc_debug = yacc_debug
self.lexer = Lexer()
self.lexer.build(optimize=lex_optimize, lextab=lextab)
self.tokens = self.lexer.tokens
self.parser = ply.yacc.yacc(
module=self, optimize=yacc_optimize,
errorlog=ply.yacc.NullLogger(),
debug=yacc_debug, tabmodule=yacctab, start='program')
# https://github.com/rspivak/slimit/issues/29
# lexer.auto_semi can cause a loop in a parser
# when a parser error happens on a token right after
# a newline.
# We keep record of the tokens that caused p_error
# and if the token has already been seen - we raise
# a SyntaxError exception to avoid looping over and
# over again.
self._error_tokens = {}
# This is a simply variant of slimit.minify that suppresses unwanted noisy stderr logging output.
def warning_free_minify(text, mangle=False, mangle_toplevel=False):
parser = SlimitNoLoggingParser(lex_optimize=False)
tree = parser.parse(text)
if mangle:
mangler.mangle(tree, toplevel=mangle_toplevel)
minified = ECMAMinifier().visit(tree)
return minified

Python futurize without replacing / with old_div

I am using futurize --stage2 which aplies a number of source-source transformation to make the code python2 and python3 compatible. One of those fixes is that all divisions a/b are replaced by old_div(a/b), which I would like to avoid (there is too many, and many of them are replaced unnecessarily, such as math.pi/2.. The documentation says that --nofix (or -x) can be used to avoid running a certain fixes, but trying --nofix=fix_divison or --nofix=libfuturize.fixes.fix_divison has no effect with --stage2. Can someone perhaps help how to ignore that particular fixer otherwise?
Omit the prefix fix_.
futurize --nofix=division ...
Depending on version you're using, you may need to specify the division_safe:
futurize --nofix=division_safe ...

fuse utimensat problem

I am developing fuse fs at python (with fuse-python bindings). What method I need to implement that touch correctly work? At present I have next output:
$ touch m/My\ files/d3elete1.me
touch: setting times of `m/My files/d3elete1.me': Invalid argument
File exists "d3elete1.me":
$ ls -l m/My\ files/d3elete1.me
-rw-rw-rw- 1 root root 0 Jul 28 15:28 m/My files/d3elete1.me
Also I was trying to trace system calls:
$ strace touch m/My\ files/d3elete1.me
...
open("m/My files/d3elete1.me", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_LARGEFILE, 0666) = 3
dup2(3, 0) = 0
close(3) = 0
utimensat(0, NULL, NULL, 0) = -1 EINVAL (Invalid argument)
close(0) = 0
...
As you see utimensat failed. I was trying to implement empty utimens and utime but its are not even called.
Try launching fuse with the -f option. Fuse will stay in foreground and you can see errors in the console.
You must implement utimens and getattr. Not all the system calls necessarily map directly to the C calls you might be expecting. Many of them are used internally by FUSE to check and navigate your filesystem, depending on which FUSE options are set.
I believe in your case FUSE is preceding it's interpretation of utimesat to utimens, with a getattr check to verify that the requested file is present, and has the expected attributes.
Update0
This is a great coincidence. There is a comment below suggestion that the issue likes with the fact that FUSE does not support utimensat. This is not the case. I had the exact same traceback you've provided while using fuse-python on Ubuntu 10.04. I poked around a little, it would appear that the fuse-python 0.2 bindings are for FUSE 2.6, it may be that a slight change has introduced this error (FUSE is now at version 2.8). My solution was to stop using fuse-python (the code is an ugly mess), and I found an alternate binding fusepy. I've not looked back, and had no trouble since.
I highly recommend you take a look, your initialization code will be cleaner, and minimal changes are required to adapt to to the new binding. Best of all, it's only one module, and an easy read.

Categories