Ignoring a specific flake8 rule for a folder - python

I am using flake8, flake8-docstrings and many other flake8 plugins in our project
I want to disable flake8-docstrings only for our test folder.
I want to avoid running flake8 twice because it would mean that running flake8 wouldn't be the straight forward flake8 . anymore. Not only that would mess with my ide settings, that would also be another excuse for the other developers in my project to not run flake8.
Is there a way to configure flake8 to exclude specific rules just for a specific folder?

There is not currently a native option for this.
There is a proposal to add support for this in the configuration file, though no current implementation exists.
There is flake8-per-file-ignores which is a plugin that accomplishes this feature
Update: per-file-ignores has been included in core as of flake8 3.7.x
The easiest way to use it is in the configuration file:
[flake8]
per-file-ignores =
tests/*: D101
(disclaimer: I am the current flake8 maintainer)

have more complicated case when you need to ignore rule for ALL same named folders of a project
When you have a monorepo with several services and need to ignore a rule for for all files in all tests folders accross the "globe"
this way not works:
per_file_ignores =
*/tests/*: S101

Related

How to add custom Flake8 rules?

Is there any way to add custom rules to flake8?
There are bunch of rules here https://lintlyci.github.io/Flake8Rules/ but I can't find the rules' source code in flake8's git repo.
I want to write a custom rule.
You need to write your own Flake8 plugin.
From the Flake8's docs:
Flake8 is useful on its own but a lot of Flake8’s popularity is due to its extensibility. Our community has developed plugins that augment Flake8’s behaviour. Most of these plugins are uploaded to PyPI. The developers of these plugins often have some style they wish to enforce.
You can't exactly add custom rules to Flake8 itself, apart from submitting a feature-request and hoping it gets accepted. But it allows you to publish a plugin that can be installed in addition to Flake8, and Flake8 can then find it and use it along with its own built-in checks.
They have nice developer documentation on how to write one: Writing Plugins for Flake8. If it's a custom rule that you want checked, then it's probably a Check Plugin. It receives the same info about the code as its built-in checkers does, then you need to write your own code for checking the lines.
You can also check out the source codes of existing Flake8 plugins for inspiration, such as:
flake8-quotes (source)
flake8-return (source)

How to disable specific PEP8 warnings in PyCharm at the repository level?

Looking to find a way, if possible, to disable specific PEP8 warnings for a Python project loaded in PyCharm at the repository level (i.e. saving a repository-committed configuration file which can apply PEP8 configuration hints to any user loading a project in PyCharm).
In a situation where other developers may contribute to a project using PyCharm. I personally do not use PyCharm myself (just a text editor) and building/linting is performed through various tox environments. There are select PEP8 rules that I am particularly not fond of, such as the promotion on injecting two blank lines in specific areas of the code (e.g. E305). While linters can be configured to ignore specific PEP8 rules and developers can invoke a command (like tox) with the same linter configuration, developers using PyCharm will still see these warnings in their environment. For example:
The problem I experience is developers will make (undesired) adjustments to the implementation and submit them for changes in pull requests. While developers can dismiss warnings themselves, I do not want developers to have to assume/interpret which PEP8 rules the project follows (aside from what may be mentioned in a CONTRIBUTING document). In addition, while source files can be modified with a # noqa comment to hint to the IDE to ignore an issue on that line, I am looking for an alternative way to ignore specific PEP8 rules without peppering various # noqa hints throughout the implementation.
For example, looking for a way to disable all E305 warnings in a theoretical .pycharm file such as follows:
[pycharm]
ignore = E305
After some additional investigation, there does not appear to be support (as of 2020-09-23; v2020.2) for repository-specific PEP8 warning customization.
The IDE uses pycodestyle for PEP8 processing. Testing out options from pycodestyle's configuration has shown that user-specific configurations will disable various PEP8 hints in the IDE; however, the goal is avoid having the user to configure anything in this case. While pycodestyle indicates that it will also look at setup.cfg and tox.ini, the IDE does not invoke pycodestyle in a way to use them. Examining how the IDE invokes pycodestyle shows that it will feed a source's contents through stdin for information. pycodestyle only implicitly loads supported project-specific configurations (i.e. setup.cfg or tox.ini) based on a common prefix of provided input files (if provided) on the existing working directory pycodestyle is invoked on -- unfortunately, since PyCharm does not change the working directory when invoking pycodestyle to a project's root, project-specific configurations cannot be implicitly loaded.
An issue has been created on their issue tracker:
PY-44684 | support pycodestyle project-specific configuration settings (pep8)
https://youtrack.jetbrains.com/issue/PY-44684
There is way to do it, but at the moment it requires the corresponding PyCharm's project settings to be kept in VCS. For every reported pycodestyle.py error there is a dedicated quick fix "Ignore errors like this" that includes the respective error code in the settings of "PEP 8 coding style violation" inspection (these codes are then passed directly to pycodestyle.py via its --ignore option).
You can then find and edit these codes in the inspection settings.
If a per-project inspection profile was configured, these settings are saved in .idea/inspectionProfiles/Your_Profile_Name.xml and this way can be shared with the team or external contributors.
Obviously, it won't work if you don't want to expose IDE-specific config files in the repository, especially when there is a better, tool-independent place for such preferences. The reported PY-44684 is a legitimate issue, I agree.

Can I specify conflicting packages with Python's setuptools?

I wrote a plugin for pytest which adds command line option. Another plugin adds command line option with the same name. Thus, they shouldn't be both installed at the same time.
Is there anything I can configure with my setup.py script to prevent user from doing so?
There isn't a convenient option for setup to specify conflicts (more importantly, it looks like setuptools are not capable to detect conflicts reliably), but you can use access to installed packages (the solution based on pkg_resources) described in this answer to write your own code in setup.py script which would detect and handle conflicts. This is not perfect, but can be a workaround.

PyCharm and buildout with another package under development

I have a specialized buildout package, say, my.buildout, and under src there is MyProject package (which does not have buidlout.cfg by itself, but of course do have setup.py). These are the relevant lines in buildout.cfg in addition to sources:
develop = src/MyProject
auto-checkout = MyProject
When I run bootstrap and bin/buildout, src/MyProject is automatically checked out, and bin/pserve script (and many other scripts under bin) contains paths to all dependencies.
The python interpreter is coming from one of the virtual envs I have. Note, that dependencies are installed by buildout under eggs, not in the virtual env.
I want PyCharm to understand whereabouts of both MyProject and the eggs, that is, everything, which is normally available when the project is running.
Tried to add my.buildout as a project, added correct python interpreter. When I go into MyProject, dependencies are underlined in red.
Also tried to add MyProject as a project, with the same result.
I am aware of this answer:
PyCharm doesn't recognize Buildout dependencies
but I have set up interpreter to the one in the #! of the bin/pserve, and "bin/py" script, which is used as a wrapper, naturally can't be added as an interpreter.
Is it possible to have my.buildout and MyProject as they are, or is buildout support of PyCharm intended for some different buildout structure / development workflow?
(and sometimes there are many projects under development is src, I've simplified)
I am very new to PyCharm (trying it out), so may be missing something obvious.
Update: Of course, I've enabled Buildout support in the settings, and used the full path to my.buildout/bin/buildout script.
Just to make it clearer. In the bin/pserve script (generated by buildout), which is used to run the app, a lot of paths are inserted into sys.path. PyCharm just does not read those. The question is how to make it aware of those paths.
Update 2: And even more: when I give bin/py as a buidlout script in the Settings, the project panel faithfully lists "Buildout Eggs" (from sys.path?), but still shows "Package requirements .... " Install / Ignore suggestion.
Ok, so after adding .../bin/py (instead of bin/buildout, which does not contain but two paths) as a "Use paths from script" in the Setting > Build, Execution, Deployment > Buildout Support I am getting lookups right!
(Even though PyCharm still suggests to install requirements)

Using proper virtualenv and flake8 config with Flycheck

How can I use configured virtualenv and flake8 settings file (setup.cfg in root of project), for flycheck in emacs?
There are a number of plugins for working with virtualenvs. For example, with pyvenv installed I can use the pyvenv-workon command to select a virtualenv from $WORKON_HOME.
virtualenvwrapper and python-environment provide similar support, and all three are available on MELPA.
Flycheck has an option flycheck-flake8rc:
Configuration file for `python-flake8'.
If you have per-project configuration files, it might be easiest to create .dir-locals.el files in the root of each project that sets flycheck-flake8rc to the appropriate value, e.g.
((python-mode
(flycheck-flake8rc . "/path/to/setup.cfg")))
I would advise not tracking .dir-locals.el in whatever version control system you are using, though of course that is up to you.
I spent some time to find right solution and decided to implement my own:
flycheck-local-flake8
This is plugin for flycheck-flake8 checker, simply uses flake8 from required virtualenv and setup.cfg from root of the python project.

Categories

Resources