mattijn / pycodestyle_magic Goto Github PK
View Code? Open in Web Editor NEWmagic functions for flake8 and pycodestyle module in Jupyter Lab and Notebook
License: MIT License
magic functions for flake8 and pycodestyle module in Jupyter Lab and Notebook
License: MIT License
I have a bug when trying to use flake8.
Here is my code:
%pip install flake8 pycodestyle_magic
%load_ext pycodestyle_magic
%flake8_on
a=1
When doing that, I have this error:
Error in callback <bound method VarWatcher.auto_run_flake8 of <pycodestyle_magic.VarWatcher object at 0x7feb812c7d90>> (for post_run_cell):
AttributeError: '_io.StringIO' object has no attribute 'buffer'
Technical details:
Is there a bug or something like that?
Using pycodestyle in "cell-at-a-time" mode, with %%pycodestyle as the first line in a cell seems to be very sensitive the spacing around colons.
In particular, a space before a colon that introduces a new block, such as:
for i in range(5) :
will crash it with an error that appears to be related to parsing windows drive names, though I'm using this on linux.
Similar errors occur in defining a dictionary if there is a space before a colon or no space after a colon.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-ec9d1225e653> in <module>()
----> 1 get_ipython().run_cell_magic('pycodestyle', '', 'for i in range(4) :\n pass')
/usr/lib64/python3.6/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2115 magic_arg_s = self.var_expand(line, stack_depth)
2116 with self.builtin_trap:
-> 2117 result = fn(magic_arg_s, cell)
2118 return result
2119
/home/p210nb/.local/lib64/python3.6/site-packages/pycodestyle_magic.py in pycodestyle(line, cell, auto)
173 #logger.info(line)
174 # on windows drive path also contains :
--> 175 line, col, error = line.split(':')[-4:]
176 # do not subtract 1 for line for %%pycodestyle, inc pre py3.6 string
177 if auto:
ValueError: too many values to unpack (expected 3)
When you load the extension rpy2.ipython
(for the cell magic %%R
), the magic %%pycodestyle
starts behaving weirdly. It stops showing the cell output and from the second run starts showing the warning for the line that doesn't exist. If you remove %%pycodestyle
, everything is back to normal. The same happens if you use %%flake8
but the warning disappears. Do you have an idea what could be the reason? Thank you very much!
python: 3.6.9
pycodestyle_magic: 0.5
rpy2: 2.9.4
OS: Linux 3.10.0-1062.9.1.el7.x86_64
I've recently been exploring this magic as an implicit study support tool for students learning Python coding using Jupyter notebooks, importing additional flake8
packages to enhance the range of error messages provided including pep8-naming
and flake8-docstrings
.
One of the things have noticed about the docstrings linter is that in a notebook cell it throws a D100
error calling out missing docstrings for modules. I want to call out missing docstrings in functions but do not require comments about a function. Typically this would require calling something like:
flake --ignore D100 filename
or a comma separate list of errors for ignoring multiple errors.
In pycodestyle_magic
, it would be useful to be able to run the %flake8_on
command with a similar set of excluding items, such as:
%flake8_on --exclude X123,Y456
When running code cells that contain IPython directives to execute commands on the command line (eg ! echo 'Hello world"
) or cell block magics (eg %%capture
), the code linter will report an error.
Whilst it is possible to include a %flake8_on
magic in the preceding code cell and a restorative %flake8_on
command at the end of such a cell to disable warnings associated with such cells, it would be useful if there was a way of disabling the execution of the flake8 linter in a couple of ways:
%%
;%flake8_disable
or %flake8_quiet
magic.There is a code example in the comment which uses an old name for the magic. I think pycodestyle
itself was renamed from pep8
at some point.
Pull request coming up in a minute.
Now https://pypi.org/project/pycodestyle_magic/ has no readme and no relevant classifiers (like supported python versions). It would be nice to fix that.
It looks like the max_line_length
option for flake8
is being ignored in my setting. Can you see what I'm doing wrong?
Note that if I include --ignore E231
the warning message is suppressed, so that option seems to be working.
Here's the raw code for my example:
%load_ext pycodestyle_magic
%flake8_on --max_line_length 60
import numpy as np
import pandas as pd
import string
pd.DataFrame(np.random.randn(100,26), columns=list(string.ascii_uppercase))
len("pd.DataFrame(np.random.randn(100,26), columns=list(string.ascii_uppercase))")
Hi everyone,
I encountered this issue and didn't know why it was an error
Importing Pandas, Create DF
import pandas as pd
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
It works well until I want to validate/filter based on some conditions, and pycodestyle_magic
stops
df2 = df.loc[
(df.col1 == True), ["col1", "col2"]
]
This code should work without problems. The formatting was created by black
on .py
file and copy to Jupyter notebook.
Error content:
ValueError Traceback (most recent call last)
File [c:\Users\kim_l\anaconda3\envs\machinelearning\lib\site-packages\pycodestyle_magic.py:46](file:///C:/Users/kim_l/anaconda3/envs/machinelearning/lib/site-packages/pycodestyle_magic.py:46), in VarWatcher.auto_run_pycodestyle(self, result)
45 def auto_run_pycodestyle(self, result):
---> 46 pycodestyle(1, result.info.raw_cell, auto=True)
47 if result.error_before_exec:
48 print('Error before execution: %s' % result.error_before_exec)
File [c:\Users\kim_l\anaconda3\envs\machinelearning\lib\site-packages\pycodestyle_magic.py:175](file:///C:/Users/kim_l/anaconda3/envs/machinelearning/lib/site-packages/pycodestyle_magic.py:175), in pycodestyle(line, cell, auto)
170 stdout = sys.stdout.getvalue().splitlines()
172 for line in stdout:
173 #logger.info(line)
174 # on windows drive path also contains :
--> 175 line, col, error = line.split(':')[-4:]
176 # do not subtract 1 for line for %%pycodestyle, inc pre py3.6 string
177 if auto:
ValueError: too many values to unpack (expected 3)
I'm curious βΒ is there a chance that we can get pycodestyle_magic to run automatically on each cell? Or must I invoke it manually?
Thanks for this great plugin!
Hi mattijn
Using your magic flake8 within jupyterlab works well for me, except that it does not seem to take into account my local .flake8 config file (to set longer line length)
the .flake8 file is in the same directory as ma .ipynb
am I missing something obvious?
Jerome
Hi bro, I found this error on every cell execution. Any idea what does it tell?
Error in callback <bound method VarWatcher.auto_run_flake8 of <pycodestyle_magic.VarWatcher object at 0x7f63240e30f0>> (for post_run_cell):
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: auto_run_flake8() missing 1 required positional argument: 'result'
I am using pycodestyle_magic as a linter in Jupyter. I am following the instructions at
https://github.com/mattijn/pycodestyle_magic
But I get error with 1-cell checking be it '%%pycodestyle' or '%%flake8'.
I've recently installed pycodestyle-0.5 on a number of linux systems (Ubuntu and Gentoo), installed with pip and on all of them cannot use the auto-checking commands: %pycodestyle_on and %flake8_on.
%pycodestyle_on gives the error:
Error in callback <bound method VarWatcher.auto_run_pycodestyle of <pycodestyle_magic.VarWatcher object at 0x7f742c0687f0>> (for post_run_cell):
TypeError Traceback (most recent call last)
TypeError: auto_run_pycodestyle() missing 1 required positional argument: 'result'
as soon as it is invoked, and then in any cell that is executed. %flake8_on produces a similar error.
I have pycodestyle-2.3.1 and python-3.6.
Thanks for this great magic function @mattijn. Is it possible to configure the extension to look for a flake8 configuration file or pycodestyle configuration file (project level or global)? I see that a few codes are now ignored by default.
I wish to use same flake8 rules for .py and .ipynb files. So it makes sense to allow to pass config path to flake8, like %flake8_on --config=setup.cfg
. After this flake8 should apply all parameters fron the config.
I could try to implement this if it makes sense for the maintainer. Does it?
Hi, thanks for the cell magic. pycodestyle
messages are off by one line, using Notebook server 5.5.0, Python 3.6.3, IPython 6.4.0.
If I import the extension
%load_ext pycodestyle_magic
Then run it for a trivial, but PEP8 violating cell
Observe that pycodestyle reports the problematic line to be number 1, though in reality the violating line is number 2.
This is perhaps due to cell magic lines being stripped from the temporary file that pycodestyle_magic writes. If we add some empty lines, we are still off by (only) one
The flake8-markdown
appears to offer support for linting code blocks in markdown files.
It would be useful if this could be applied to code blocks in notebook markdown cells, although I'm not sure how the reports could be displayed given the notebook structure (markdown cells don't have an output?), or how the code could be invoked. (The python-markdown
extension might provide a useful crib in this respect?)
One possibility for the report display would be to put it into a dismissible HTML block inserted into the markdown cell just after each code block:
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert">x</button>
Report here...
</div>
If I run pandas.read_csv?
cell, pycodestyle_magic will argue, that the syntax is incorrect.
It really is incorrect python syntax, but it also a common used jupyter feature, su it seems a good idea to skip check on code cell if it has only one line and it ends with question mark.
Does it make sense to you?
Hi, I use pycodestyle_magic
. This magic code is very nice.
When I check pythgon dict code style by %%pycodestyle
, an error occurs.
for example, when I try
%%pycodestyle
{"a":0,"b":1}
then return ValueError
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-2c11b8fb9c7b> in <module>
----> 1 get_ipython().run_cell_magic('pycodestyle', '', '{"a":0,"b":1}\n')
~/python3.8/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2379 with self.builtin_trap:
2380 args = (magic_arg_s, cell)
-> 2381 result = fn(*args, **kwargs)
2382 return result
2383
~/python3.8/site-packages/pycodestyle_magic.py in pycodestyle(line, cell, auto)
173 #logger.info(line)
174 # on windows drive path also contains :
--> 175 line, col, error = line.split(':')[-4:]
176 # do not subtract 1 for line for %%pycodestyle, inc pre py3.6 string
177 if auto:
ValueError: too many values to unpack (expected 3)
It seems that the cause is that ":" is included in the code. right?
Is there a way around this error?
I'm using Pycodestyle, but can't ignore errors.
My code is the following:
%pip install flake8 pycodestyle_magic
%load_ext pycodestyle_magic
%pycodestyle_on --ignore E225
a=1
I'm expecting no warning, no error.
But here is the result:
1:2: E225 missing whitespace around operator
This is not what I was expecting.
Technical details:
pycodestyle==2.8.0
pycodestyle-magic==0.5
Python version = 3.8.8
Is there a bug or something like that?
I with there was some strict mode (%flake8_on --strict
), which executes code in a cell only if it has no flake8 violations. If cell has errors, code in cell is not executed at all.
This will force developers to refactor their code on the go, not before commit. This usually reduces amount of bugs and increases readability.
Does it make sense to you?
After loading the pycodestyle_magic extension, the first time a cell is executed with %%pycodestyle at the beginning, nothing happens. Is that the intended behaviour? It seems to do the job on subsequent calls - but it never does anything the first time after the extension is loaded.
Hi,
I use the current version pycodestyle-magic-0.5
.
At the beginning of the notebook I do
%load_ext pycodestyle_magic
%pycodestyle_on
#%flake8_on # does not work
Then the following throws a ValueError
t = True
if t1 == True:
print('1')
i.e.
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/anaconda3/lib/python3.8/site-packages/pycodestyle_magic.py in auto_run_pycodestyle(self, result)
44
45 def auto_run_pycodestyle(self, result):
---> 46 pycodestyle(1, result.info.raw_cell, auto=True)
47 if result.error_before_exec:
48 print('Error before execution: %s' % result.error_before_exec)
<decorator-gen-123> in pycodestyle(line, cell, auto)
/usr/local/anaconda3/lib/python3.8/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
218 # but it's overkill for just that one bit of state.
219 def magic_deco(arg):
--> 220 call = lambda f, *a, **k: f(*a, **k)
221
222 # Find get_ipython() in the caller's namespace
/usr/local/anaconda3/lib/python3.8/site-packages/pycodestyle_magic.py in pycodestyle(line, cell, auto)
173 #logger.info(line)
174 # on windows drive path also contains :
--> 175 line, col, error = line.split(':')[-4:]
176 # do not subtract 1 for line for %%pycodestyle, inc pre py3.6 string
177 if auto:
ValueError: too many values to unpack (expected 3)
Best,
skwde
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.