Vim Flake8 ignoring project config file - python

vim-flake8 seems to be ignoring my project-specific config file. If I run flake8 from the command line in my project root, it works, but when I open vim and try to run flake8 against my files, its not picking up that setting. I know this because its using a default line-length of 79, instead of my project-specific 120.
I read this post: flake8 not picking up config file, but it doesn't seem to help. It mentions a bug fixed over a year ago in the comments.
In my project root, I have a .flake8 file with a [flake8] section.
How does vim-flake8 determine what the project root is and where to look for the config file? Does it just use the directory in which Vim is opened?

I ran into a similar issue today, and I got around it by adding the following to my ~/.vimrc (or actually, my ~/.config/nvim/init.vim) file:
let g:syntastic_python_flake8_config_file='.flake8'
This was based on syntastic's official documentation on language-specific configuration files.

I ran into the same problem today. Flake8 ran fine from the command line, but inside of vim every config file seemed do be ignored by syntastic. Running flake8 inside of vim itself (with :!flake8) picked up the config.
Based on the answer from Tomi I fixed it by adding
let g:syntastic_python_flake8_args='--config=setup.cfg'
to my vim config, which should work if vim is started from the project root. Still a bit hacky but at least the flake8 config stays in a single place.

Had the same problem too on my OSX, and partially solved it. Had the latest versions of syntastic (git clone today) and flake8 3.0.4. Vim 7.4.
flake8 ran fine from command line and picked my global ~/.config/flake8. Vim did not output anything if I had the config file, but worked fine without the flake8 config file.
I partially solved the problem by having the flake8 config not in file system but in my .vimrc:
let g:syntastic_python_flake8_args='--ignore=E203,E231'
but this is not the best solution as the config is not shared.
For the initiated developers, when I enable debugging
let g:syntastic_debug = 1
I get this output:
syntastic: 4.516990: &shell = '/bin/bash', &shellcmdflag = '-c', &shellpipe = '2
>&1| tee', &shellquote = '', &shellredir = '>%s 2>&1', &shellslash = 0, &shellte
mp = 1, &shellxquote = '', &shellxescape = ''
syntastic: 4.517587: UpdateErrors (auto): default checkers
syntastic: 4.517927: CacheErrors: default checkers
syntastic: 4.518502: g:syntastic_aggregate_errors = 0
syntastic: 4.518666: getcwd() = '/Volumes/myproject/src'
syntastic: 4.525418: CacheErrors: Invoking checker: python/flake8
syntastic: 4.526113: SyntasticMake: called with options: {'errorformat': '%E%f:%
l: could not compile,%-Z%p^,%A%f:%l:%c: %t%n %m,%A%f:%l: %t%n %m,%-G%.%#', 'make
prg': 'flake8 main.py', 'env': {'TERM': 'dumb'}}
syntastic: 4.727963: system: command run in 0.201426s
syntastic: 4.729751: getLocList: checker python/flake8 returned 1
syntastic: 4.730094: getLocList: checker python/flake8 run in 0.204568s

I couldn't get g:syntastic_python_flake8_* variations working on my MacOS.
The shortcut that worked for me was to add a symlink to the project base directory:
ln -s /path/to/common/.flake8 .flake8
with this link syntastic is forwarded to the .flake8 in the desired location.

Related

Erratic behavior with Mypy in VSCode: How to troubleshooting and solve?

I have been having issues with MyPy in VSCode. I usually have my venv activated, Pylance as the language server and the MyPy extension (to perform check on editor change event).
MyPy.
MyPy is installed on the current venv, is enabled, linting in enabled, and mypy is using the active interpreter, in my settings.json I have:
{
"python.languageServer": "Pylance",
"python.linting.enabled": true,
"python.linting.mypyEnabled": true,
"mypy.runUsingActiveInterpreter": true
}
MyPy does complain about "library stubs" not found, for packages that are not typed, it also points to some errors, but some very explicit ones are not being shown.
An example:
# file_a.py
def myfunc() -> dict:
return {}
# file_b.py
from file_a import myfunc
from file_z import func_dont_exist
# No error shown here
abc: bool = myfunc()
xyz: str = func_dont_exist()
# This points to an error, highlight the "1"
abc = "abc" + 1
If I run mypy . on the root, also no errors. I have mypy.ini file on the root, ignoring the migrations folder, .venv and usiing SQLMyPy Plugin.
If I disable the ignore_errors mypy point to a bunch of errors on the migration folders (expected), which tells me that the daemon is working.
I have updated my VSCode, tried to disable every extension I have, reloaded, restarted, checked the console, and nothing.
Running out of ideas on how to go by debugging this issue, every single question I find goes in the lines of: enable mypy on the settings, check the path, enable linting etc, but I've already done all of that.
Any ideas?
I had trouble with mypy in the past, like loading up mypy plugin. It was resolved when I started vscode from terminal with the right python environment activated
conda activate my-env
cd my/vs-code/project/dir
code .
Worth a try

Flake8 times out on circle-ci

We have a flake8 build stage in our circle-ci workflow, and more often than not this step fails due to timeout:
Too long with no output (exceeded 10m0s): context deadline exceeded
At the same time, this same stage runs quite ok locally on our macbooks:
% time make lint
poetry run black .
All done! ✨ 🍰 ✨
226 files left unchanged.
isort -y
Skipped 2 files
PYTHONPATH=/path/to/project poetry run flake8 --show-source
0
make lint 44.00s user 4.90s system 102% cpu 47.810 total
We tried to debug the issue by adding the -vv flag to flake8 thinking we would get some plugin name that takes too long, but we don't even have the timestamps in the log:
flake8.processor ForkPoolWorker-31 1004 WARNING Plugin requested optional parameter "visitor" but this is not an available parameter.
flake8.processor ForkPoolWorker-8 1080 WARNING Plugin requested optional parameter "visitor" but this is not an available parameter.
flake8.bugbear ForkPoolWorker-26 1082 INFO Optional warning B950 not present in selected warnings: ['E', 'F', 'W', 'C90']. Not firing it at all.
Are there any known reasons why flake8 would freeze on CircleCI? How can one debug the issue?
When using a virtual-environment such as venv you should ignore the folder in the [flake8]-config (that's what happened to me). Assuming you are creating a virtualenv with virtualenv .venv it would look like this:
[flake8]
exclude = .venv
The same was for my coverage which was fixed by adding an omit to that config (solution found here):
# pyproject.toml file content
[tool.coverage.run]
omit = [
"tests/*",
".venv/*",
]
For now, the solution we seem to have found was to limit the number of cores running flake8:
.flake8
[flake8]
...
jobs = 6
Not sure it is the correct solution, but there you go. I will accept a better solution if there is one.
I've also experienced a timeout in circle-ci only, but it was due to the specific way dependencies are installed on the pipeline, creating a .venv folder which was not excluded in flake8 configuration.
The -v option helped me to notice the huge amount of files flkae8 was analyzing.

Makefile on Windows 10 - file not found

After some time I finally managed to successfully install python and pip and run it on my machine using Visual Studio Code.
I am working in virtual environment in python and we have a Makefile with following statement:
test:
source .env && PYTHONPATH=. PY_ENV=testing py.test ${ARGS} --duration=20
File .env lives in the main directory next to Makefile. It contains some environmental variables needed for testing certain APIs.
When I take the line out of the file and run it in my terminal, everything works fine and all tests are running etc.
However if I call the following: make test I am getting this error:
$ make test
source .env && PYTHONPATH=. PY_ENV=testing py.test --duration=20
/usr/bin/sh: line 0: source: .env: file not found
make: *** [test] Error 1
(venv)
To me it looks like when running this command from within Makefile it can't see the .env file but have no idea how to solve it.
The source command isn't looking up the file in the current working directory. As mentioned in man source:
Read and execute commands from filename in the current shell
environment and return the exit status of the last command executed
from filename. If filename does not contain a slash, filenames in
PATH are used to find the directory containing filename.
Change the file path like so:
test:
source ./.env && PYTHONPATH=. PY_ENV=testing py.test ${ARGS} --duration=20
Note that this error does not occur in bash version < 4. This is due to an implementation bug when run under POSIX mode (what make uses, since its default shell is sh, which is usually bash --posix). The correct behaviour was first mentioned in the documentation of bash-2.05 (revision 28ef6c31, file doc/bashref.info):
When Bash is not in POSIX mode, the current directory is searched if
FILENAME is not found in `$PATH'.
These older versions searched the current directory regardless of POSIX mode. It was only in bash-4.0-rc1 (revision 3185942a, file general.c) that this was corrected. Running git diff 3185942a~ 3185942a general.c outputs this section:
## -69,6 +69,7 ## posix_initialize (on)
if (on != 0)
{
interactive_comments = source_uses_path = expand_aliases = 1;
+ source_searches_cwd = 0;
}

Setting $PATH via a command with tox

Currently using tox to test a python package, and using a python library (chromedriver-binary) to install chromedriver.
This library creates a script (chromedriver-path) which when called outputs the PATH where chromedriver is installed. The usual way to use this is to run:
export PATH=$PATH:`chromedriver-path`
I've tried the following without success in tox.ini
setenv=
PATH = {env:PATH}{:}`chromedriver-path`
This errors as expected:
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'
Implying that the setenv command is never called/run.
commands=
export PATH=$PATH:`chromedriver-path
This fails with:
ERROR: InvocationError for command could not find executable export
How do I make this work?
Commands can't change their parent processes' environment variables, and thus can't change the environment variables of subsequent commands launched by forking that parent; they can only set environment variables for themselves or their own children.
If you were able to collect the output of chromedriver-path before starting tox, this would be moot. If it's only available in an environment tox itself creates, then things get a bit more interesting.
One approach you can follow is to wrap the commands that need this path entry in a shim that adds it. Consider changing:
commands=
py test ...
to:
commands=
sh -c 'PATH=$PATH:$(chromedrive-path); exec "$#"' _ py test ...

Python correctness (i.e., lint) analyzing for Notepad++

Does anyone know of anything like pylint or pychecker for notepad++? Or perhaps how to use pylint in notepad++.
If you install the Python Script plugin, then you can add a new script with the following lines to get pretty good results:
console.show()
console.clear()
console.run('cmd.exe /c '
+ 'C:\\Python26\\Scripts\\pylint.bat --reports=n -f parseable '
+ '"%s"' % notepad.getCurrentFilename())
The output will include hyperlinks to the lines with the errors/warnings (if the filenames don't have spaces in them...)
The option "-f parseable" is deprecated in the current version of Pylint.
The current equivalent alternative is:
console.run('cmd.exe /c '
+ 'C:\\Python26\\Scripts\\pylint.bat --reports=n '
+ '--msg-template="%s" %s'
% ( '{path}:{line}: {msg_id}({symbol}), {obj} {msg}', notepad.getCurrentFilename()))
Note: python path can be different e.g. C:\\Python27.
Note2: double quotes in --msg-template="..." are important
You could install PyLint using python -m pip install pylint and use it via Notepad++'s Run... command (F5):
cmd /c python -m pylint "$(FULL_CURRENT_PATH)" & pause
To get the output in Notepad++ and link to the code, use NppExec.
None of the other answers worked for me, but this does:
Install PyLint using python -m pip install pylint
Install NppExec via the Plugin Manager, press F6, and save this script as "PyLint":
NPP_SAVE
cd "$(FULL_CURRENT_PATH)"
env_set PYTHONIOENCODING=utf-8
python -u -m pylint "$(FULL_CURRENT_PATH)"
Sample output:
NPP_SAVE: C:\Users\Cees\Documents\http_ear.py
CD: C:\Users\Cees\Documents\http_ear.py
Current directory: C:\Users\Cees\Documents
ENV_SET: PYTHONIOENCODING=utf-8
$(SYS.PYTHONIOENCODING) = utf-8
python -u -m pylint "C:\Users\Cees\Documents\http_ear.py"
Process started (PID=25136) >>>
************* Module http_ear
http_ear.py:16:0: C0301: Line too long (1780/100) (line-too-long)
http_ear.py:17:0: C0301: Line too long (226/100) (line-too-long)
http_ear.py:26:0: C0304: Final newline missing (missing-final-newline)
------------------------------------------------------------------
Your code has been rated at 8.00/10 (previous run: 8.00/10, +0.00)
<<< Process finished (PID=25136). (Exit code 16)
================ READY ================
You can link the bug locations using NppExec's Console Output Filters. Press Shift+F6 and enable this filter with Red set to FF:
%FILE%:%LINE%:%CHAR%
Then double clicking a red line focuses the specified location in the editor.
You should use the Executable instead of the Batch if you want to use Pylint within NotePad++.
Go to the Configuration from Python Script and create a new .py File to run Pylint from that. (i called my file npphelper.py)
(Add that npphelper.py to Menu-items and Toolbar-icons, then you can execute it by pressing a Button.)
This will run Pylint into Notepad++, i splitted the Command into 2 Parts:
pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n'
console.show()
console.clear()
console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
Path to pylint.exe (i used a Shortname instead of Doublequotes)
The File you want to check with Pylint (actualy returns the Path from active Tab)
(You have to change the Paths so that it fits to your Installation...)
All you have to do now is saving this npphelper.py, open the Tab with your Project-File and run the npphelper.py you created for pylint. (e.g. via button)
If you don't want to use the default Configuration then generate a pylintrc Template (save them where you want). I've done it via CMD with the following Command:
pylint.exe --generate-rcfile>>myfilename.pylintrc
Then you need to change some lines into the npphelper.py:
rcfile = 'C:\\PROGRA~1\\Python35\\Scripts\\myrcfile.pylintrc'
pyLint = 'C:\\PROGRA~1\\Python35\\Scripts\\pylint.exe --reports=n --rcfile="%s"' % rcfile
console.show()
console.clear()
console.run('%s "%s"' % (pyLint, notepad.getCurrentFilename()))
I've installed Python Script 1.0.8.0 with all the Extras using the .msi File here.
(Using the PluginManager in Notepad++ gives you version 1.0.6.0 instead of 1.0.8.0)
I use Windows 7 with Notepad++ 6.9.1, Python 3.5.1 and Pylint 1.5.5.
(i installed pylint via CMD -> "pip install pylint" and updated it.)
Some more usefull Links:
How do I create a pylintrc file
PyLint “Unable to import” error - how to set PYTHONPATH?

Categories