How to enable pep8.E501 fixing on file formatting shortcut? - python

When I'm using Ctrl+Shift+I shortcut it fixes all errors like invalid count of blank lines. But E501 (too long line) is not fixing. Line saves its length. I've found solutions for ignoring that error but I'd like to translate file to PEP8 fully. How to do it?

Related

How do I make autopep8 allow a comment at the end of a line?

I'm trying to format my code with autopep8 (because this appears to be the default one in VSCode). But there is a problem with it:
I am using a lib that doesn't care about typing, so I need to suffix some lines with # type:ignore to make the linter ignore that particular file. But when formatting (with autopep8) the formatter moves the comment to the line above. Then my linter doesn't ignore the line like it should.
So with e.g. this code:
somevar = dumblib.foobar() #type:ignore
I hit Shift+Alt+F to format, and then the code is formatted to this:
# type:ignore
somevar = dumblib.foobar()
And then, the linter complains that the type of "somevar" is unknonwn.
How can I prevent autopep8 from moving the comment during a reformat?
It's a relatively old question but in case others also struggle with this, I will leave a solution that helped me.
The autopep8 rule that is responsible for moving comments above the code line is nothing but a max-line-length. I was able to fix this by increasing the maximum line length. To do this I created a configuration file in my project (.pep8) and configured vscode to use it (in the autopep8 extension settings).
Configuration file contents:
[pep8]
max-line-length = 160

mypy error on same line regardless of code content

I'm getting a mypy syntax error on line 36 in every single one of my files. If the file is shorter than 36 lines it just highlights the last line. The error is invariably the same:
invalid syntax mypy(error)
Apparently this issue may have happened before, but without a clear solution. Is this a known bug? How can I fix this?
Here is a clear example:
I found the reason: I have another file where there is a syntax mistake on line 36. Even though I closed that file, somehow mypy was still highlighting that same line in all of the files I had open. After I fixed that mistake in that one file, the mysterious syntax errors were gone in all the other files as well.
This seems like a bug to me, but I'm not sure if it's mypy or vscode (the IDE i'm using)? In any case my conclusion from this is: if you get inexplicable errors on the same line in all files, check for a mistake on that line even in closed files and fix it.

Syntax error when I try to import any module

C:\Users\sanji\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/sanji/PycharmProjects/pythonProject2/file.py
Traceback (most recent call last):
File "C:\Users\sanji\PycharmProjects\pythonProject2\file.py", line 1, in
import tkinter
File "C:\Users\sanji\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1769
if self._name in self.master.children:
^
SyntaxError: invalid syntax
Process finished with exit code 1
In the __init__.py file for Tkinter, versions 3.9.2 through 3.9.5 inclusive have that statement appearing only on lines 2556 and 2581. See here, for example.
In the 3.9 variants before that, it's on various similar lines, around 2552 and 2577 give or take a couple of lines for small file changes.
It has never been anywhere near line 1769 in any of the 3.9 releases (including the release candidates).
So, given it's complaining about the file in the path containing ...Python\Python39\lib\..., I don't think we need to look at any other versions. I would say it's a safe bet that the file has become corrupted somehow.
You should check that file for validity and, if there's something wrong with it (probable), you may need to re-install Python to fix it. We can probably only speculate as to how it became corrupted (if indeed it did).
Maybe someone accidentally edited it or maybe you have a problematic disk. It's hard to say without seeing the entire file (or at least some twenty lines around the one the error's reported on).

How do I resolve a none type error within a package (ecco_v4_py)

I recently had to recreate an environment after updating to Mac OS Big Sur and running into a common issue with Spyder not opening. A key component of that environment is the ecco_v4_py package necessary for processing ECCO state estimate data.
I managed to get the package installed into the environment, but whenever I try to load it in with import ecco_v4_py as ecco I get the following error.
File "/Users/drew/anaconda3/envs/Salt/lib/python3.9/inspect.py", line 632, in cleandoc
lines = doc.expandtabs().split('\n')
AttributeError: 'NoneType' object has no attribute 'expandtabs'
I have tried creating a fresh environment with just ecco_v4_py and Spyder, but that gives the same error. I have also tried creating a fresh environment with a downgraded version of the package, but that also causes this error.
Looking more deeply into the issue, I go to the line in the library code that is throwing the error.
def cleandoc(doc):
"""Clean up indentation from docstrings.
Any whitespace that can be uniformly removed from the second line
onwards is removed."""
try:
lines = doc.expandtabs().split('\n') #This is the line that is failing
except UnicodeError:
return None
else:
# Find minimum indentation of any non-blank lines after first line.
margin = sys.maxsize
for line in lines[1:]:
content = len(line.lstrip())
if content:
indent = len(line) - content
margin = min(margin, indent)
# Remove indentation.
if lines:
lines[0] = lines[0].lstrip()
if margin < sys.maxsize:
for i in range(1, len(lines)): lines[i] = lines[i][margin:]
# Remove any trailing or leading blank lines.
while lines and not lines[-1]:
lines.pop()
while lines and not lines[0]:
lines.pop(0)
return '\n'.join(lines)
But when I run just this block it works, so I'm not sure why it throws an error when I try to import the package.
Small edit: I've also tried installing an earlier version of python, but this does not solve the problem. I tried editing the file that was throwing the error to fix the problem, but further errors appear down the line.
Additional edit: I managed to get the ecco_v4_py package to work appropriately by manually going in and editing files as errors showed up. For me it required changing except UnicodeError to except (UnicodeError, AttributeError). For the remaining errors I needed to force certain variables to be interpreted as strings using the str() command. I'm still not sure if there will be unforeseen consequences of taking this brute-force approach, so I haven't marked this as the answer.

Is it possible to ignore one single specific line with pylint, without changes to the source code?

Is it possible to ignore one single specific line with pylint? has solutions that requires littering source code with comments what is undesirable for me.
Is there way to specify that given instance is false positive in arguments of the pylint command line tool?
I don't think it is possible to ignore a specific line without changing the source code. However, it is certainly possible to ignore warning types in the .pylintrc or at the top of file.
I'd go with the approach of creating a .pylintrc to suit your needs. For example, if you want to completely ignore W0612: unused-variable then you can create a .pylintrc file and add message names to disable as noted in pytest docs.
.pylintrc
disable=unused-variable,
...,
Alternatively, you can disable messages on a case by case basis at the top of the file, and just tell your students to ignore it:
Top of file
# pylint: disable=unused-variable, wildcard-import, method-hidden
# pylint: enable=too-many-lines

Categories

Resources