Pycharm's code style inspection: ignore/switch off specific rules - python

I'm trying to import existing project into PyCharm. I can refactor the code so that PyCharm will be pleased, but we like to have spaces around colons in dictionaries, like this: {"A" : "B"}. We also like aligning assignments:
a = 1
abc = 3
Is there a way to configure PyCharm, so that he'll ignore all errors/warnings related to this specific stuff?

Using PyCharm 5 (community edition), you can do the following: Code –> Inspect Code. Then select the required inspection error, and click on the "Suppress" option or "Ignore errors like this" option on right hand side.
Please look at the screenshot below:
When you choose the "Suppress" option, it adds a comment as shown in the screenshot below:
Suppressing can be done at the statement, or function/method, levels. If trying to suppress an argument to a function, the suppression only works at the function level (meaning it also suppresses other name reference violations that might exist within that function).
You also have the option of switching off "PEP8 coding style violations" altogether (by ticking the box shown below), or explicitly managing "Ignore Errors" manually. Screenshot given below:
In general, you should perhaps question why you are suppressing PEP8 guidelines. However, sometimes it appears necessary, for instance when using the pytest module, it is necessary to shadow variables, etc, which the PEP8 Inspection complains about in. In such cases, this feature in PyCharm is very helpful.

If you're ok to ignore all matching issues, you can just press Alt-Enter (or click on the lightbulb) and choose "Disable Inspection". Saves you going into the settings and trying to figure out the inspection rule that matches.
From http://iambigblind.blogspot.jp/2013/02/configuring-pep8py-support-in-pycharm-27.html

#Krzysztof Stanisławek, function is different as Pycharm follows the PEP8 coding style, so it is recommended that there is no whitespace between the function variables and ":", if you don't want this, you can disable it by
"Settings"-> "Editor"-> "Inspections"->"PEP8 coding style violation"
However, this is not recommended.

I got the same issue, and the neat solution has been pointed by #fantabolous, from configuring PEP8.py support in PyCharm 2.7
Example before
Adding the error code either manually or with "Alt+Enter" on error highlight
The error code can be found here
After the changes
It's great to select precisely some rules instead of disabling all warning from PEP8. Thanks to the previous comments.

to have spaces around colons in dictionaries, configure Settings > Editor > Python Spaces
Other > Before ':'
and
Other > After ':'

Related

Python VS code detect incorrect argument name

How can I make VS code detect incorrect argument names?
def test_function(name1, name2, name3):
print(name1)
print(name3)
print(name2)
test_function(name1=1, name2=2, name4=4)
Currently, VS code doesn't flag this as an error. Detecting this would be very useful since argument names change. Otherwise you get an error at run time which is far too late.
VS code is a text editor that one can use to write code in multiple languages but is not a Python interpreter. What you can do is install an extension to do that. For example: Python from Microsoft, has a linter that shows you what problems your code has, including the kind of problem you're describing. Make sure you read the docs to configure it correctly.

How to deal with PyCharm's "Expected type X, got Y instead"

When using PyCharm, Pycharm's code style inspection gives me the warning Expected type 'Union[ndarray, Iterable]', got 'float' instead in the editor if I write np.array(0.0). When I write np.array([0.0]) I get no warning.
When coding
from scipy.special import expit
expit(0.0)
I get Expected type 'ndarray', got 'float' instead, while
expit(np.array([0.0]))
solves that.
What I think Pycharm's code style inspection wants to tell me is there's a possibility of a type error, but I am not sure how I should react to that in the sense of good programming. Is PyCharm right to scold me and should I use the long versions or should I keep my short versions for readability and speed of coding?
If I should not change my code to the long versions - can I get rid of the Pycharm's code style inspection warning, or is that a bad idea, because they may be correct in other cases, and I am not able to tune the warnings that specifically?
If you want to ignore it for that specific line,
then you can put
# noinspection PyTypeChecker
above the line of code that is being marked.
PyCharm determines from the type-hints of the source code that the arguments you pass are incorrect.
How to disable
Your question simplifies to one of figuring out how to disable this type checking. However, please be warned,
Switching off the inspection completely is not a good solution. Most
of the time PyCharm gets it right and this provides useful feedback.
If it's getting it wrong, it's best to raise a ticket with them to see
if it can be fixed.
You can do that like this:
Go to Settings/Preferences
On the sidebar, click Inspections (under the Editor category)
Expand the Python tab
Scroll down to Type Checker and uncheck it
PyCharm should now stop issuing warnings about incorrect function arguments.
Look at the specifications of the expit function. Nothing there says it's permissible to provide a scalar argument - it calls for a numpy.ndarray. PyCharm is smart enough to tell you that any iterable (and hence a list) is acceptable, but this message isn't a warning - it's telling you your code as written does not meet the function's specifications. As #JonClements points out in a comment, numpy's scalar broadcasting feature will allow this code to run, but PyCharm isn't smart enough to deduce this.
NOTE: the answer from #cs95 explains how to disable the type checking, and it may be more appropriate as the accepted answer.
As #brunodd said on a comment before, the correct tag to uncheck is Type checker.
Go to Settings/Preferences (Ctrl + Alt + S)
On the sidebar Inspections
Python tab
Uncheck Type Checker and hit Apply
This is really annoying, PyCharm fails to identify some types even though its right.

Disable on-the-fly PEP8 checks, only check when saving file

I'm using PyCharm Community Edition 4.5.4, and I hate how it notifies me of every little "mistake" I make even when I have full intention of fixing it within 30 seconds.
My style is to kind of write everything at once (instead of finishing one thing before moving to the other), and thus every second word in my code gets highlighted as variable 'x' is not used or Unresolved reference 'x' because I already moved to an other section of my code, intenting to finish the for loop later. If I do something like:
for x in my_list:
pass
And then move to define my_list on top of the file, it will instantly highlight Local variable 'x' is not used. I wish to write my whole code freely, and then after hitting save, I wanna know what mistakes I made.
Is there any way to disable the PEP8 checker, so it would only check when I actually save the file, instead of when I type anything at all?
I have had problems with this issue too.
Unfortunately, there seems to be no documented way of doing what you're requesting. The PyCharm Articles on Code Inspection and Configuring Inspections really don't hint at any such possibility.
Additionally the config file in ~/.PyCharm40/config/inspection/Default.xml isn't what you would call rich in options (note: I have no idea if more options exist, couldn't really find appropriate documentation).
Since pep8.py is apparently ran continuously as a background process in PyCharm, I also checked whether a configuration of these processes was possible. Unfortunately (again), no useful results were found.
To make things worse, there seems to be no relevant plugin available in their plugin repository to allow for further tweaking of the inspection tool.
The other option I tried was by changing the settings in PyCharm and resort to manual calls to pep8. I unselected the inspections for pep8 from Settings | Editor | Inspections | Python tab and then ran the manual inspection by pressing Ctrl + Alt + Shift + I and entering the two pep options. It does not seem to catch the same coding convention errors.
You probably have two options now, one is switching to another IDE as Adam Smith suggested (or noted, actually) and second is trying to maybe get some help on the PyCharm forum.

PyCharm code completion setup for underscore - less priority for protected and mangled names?

I am using PyCharm but I do not like default code completion which shows _protected and __mangled names higher than public.
How can I force to show names starting with underscore at the bottom of code completion list and public at the top?
Some editors like PyDev is doing it much better without configuration.
Screen asked by #jole showing my problem:
This is not configurable in PyCharm; there is an open issue for changing the default behavior of the completion in this context.

Difference in solarized gvim theme

I am using a Solarized theme for (g)vim, and having an issue with small difference between the screenshot on the site and what I am actually having.
The screenshot for python on the theme's site is here:
(source: ethanschoonover.com)
, and mine is
They have a small difference (that I can see) in import statement (color is different). It sort of drives me crazy. Is my case correct, and that's what supposed to happen, or my settings are wrong?
I've installed the theme using vim-addon-manager plugin system.
Thanks.
The first thing you can check is what :highlight groupings are being applied to your import statement, I have a key mapping for a function which can do this:
" Show the stack of syntax hilighting classes affecting whatever is under the
" cursor.
function! SynStack()
echo join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), " > ")
endfunc
nnoremap <F8> :call SynStack()<CR>
If you :source a file containing this or put this in your .vimrc and reopen vim, we can now check what highlighting is being applied to import.
When I press F8 on an import I receive the message pythonInclude, now if you don't see that there may be something wrong with the parsing of python file and the keyword import hasn't been picked up on. We can then look at what rules are being applied to that-
:highlight pythonInclude
This gives me-
pythonInclude xxx links to Include
If we follow this then we can see what rules are being applied to Include group. This will probably go deeper, Include xxx links to PreProc. If you get this far your highlighting groups are probably correct. If you didn't link to PreProc there maybe another plugin overwriting the highlight group, or a similar issue. You can then check what colors the PreProc is setting. You can override a highlight group link, :highlight link pythonInclude Function this will now display imports as the same color as Functions!
You can also modify the colors and formatting applied to different highlight groupings however I will leave you to discover those on your own. Much more help can be found in :h :highlight, but I hope this helps start your debugging process!

Categories

Resources