jbkahn / flake8-print Goto Github PK
View Code? Open in Web Editor NEWflake8
License: MIT License
flake8
License: MIT License
Traceback (most recent call last):
File "C:\Python27\Lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File ".tox\flake8\Scripts\flake8.EXE\__main__.py", line 9, in <module>
File ".tox\flake8\lib\site-packages\flake8\main.py", line 33, in main
report = flake8_style.check_files()
File ".tox\flake8\lib\site-packages\pep8.py", line 1670, in check_files
self.input_dir(path)
File ".tox\flake8\lib\site-packages\pep8.py", line 1706, in input_dir
runner(os.path.join(root, filename))
File ".tox\flake8\lib\site-packages\flake8\engine.py", line 95, in input_file
return fchecker.check_all(expected=expected, line_offset=line_offset)
File ".tox\flake8\lib\site-packages\pep8.py", line 1412, in check_all
self.check_ast()
File ".tox\flake8\lib\site-packages\pep8.py", line 1359, in check_ast
for lineno, offset, text, check in checker.run():
File ".tox\flake8\lib\site-packages\flake8_print.py", line 27, in run
errors = check_tree_for_print_statements(self.tree, noqa)
File ".tox\flake8\lib\site-packages\flake8_print.py", line 48, in check_tree_for_print_statements
if ((isinstance(node, ast.Call) and node.func.id == 'print') or (hasattr(ast, 'Print') and isinstance(node, ast.Print) and node.lineno not in noqa)) and node.lineno not in noqa:
AttributeError: 'Attribute' object has no attribute 'id'
There's a version 5.0.0 on PyPI, but no trace of it on GitHub. Where's the code?
You could (although I haven't tested it, and I'm afraid don't have time to do a PR) tokenize the source file to identify lines with # noqa
comments:
import tokenize
import ast
with open('somefile') as fin:
source = fin.read()
parsed = ast.parse(source)
tokens = tokenize.generate_tokens(lambda L=iter(source.splitlines()): next(L))
noqa = {
token[2][0] for token in tokens
if token[0] == tokenize.COMMENT and token[1].endswith('noqa')
}
Then carry on with parsed
as you are, but exclude messages whose line is present in noqa
...
We don't need a python2 specific code.
Hi,
It would be great to add all the print functions of traceback.print_*
in this library detection as well.
I'm using this plugin as a way to stop the usage of prints without the use of loggers, traceback.print_*
sometimes are used in this context
try:
a = 1/0
except Exception as e:
print(e)
traceback.print_exc()
I would like to prohibit these functions as well since they follow the same merit, have the print
name in it, and from the standard library
While running flake8 linter with flake8-print I get an error due to flake8-print bringing in version 2.4.0 of pycodestyle. Suggested PR to fix #32
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2416, in resolve
return functools.reduce(getattr, self.attrs, module)
AttributeError: module 'pycodestyle' has no attribute 'break_around_binary_operator'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 178, in load_plugin
self._load(verify_requirements)
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 150, in _load
self._plugin = resolve()
File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2418, in resolve
raise ImportError(str(exc))
ImportError: module 'pycodestyle' has no attribute 'break_around_binary_operator'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/flake8", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python3.6/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/usr/local/lib/python3.6/site-packages/flake8/main/application.py", line 396, in run
self._run(argv)
File "/usr/local/lib/python3.6/site-packages/flake8/main/application.py", line 383, in _run
self.initialize(argv)
File "/usr/local/lib/python3.6/site-packages/flake8/main/application.py", line 365, in initialize
self.find_plugins()
File "/usr/local/lib/python3.6/site-packages/flake8/main/application.py", line 191, in find_plugins
self.check_plugins.load_plugins()
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 423, in load_plugins
plugins = list(self.manager.map(load_plugin))
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 311, in map
yield func(self.plugins[name], *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 421, in load_plugin
return plugin.load_plugin()
File "/usr/local/lib/python3.6/site-packages/flake8/plugins/manager.py", line 186, in load_plugin
raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "pycodestyle.break_around_binary_operator" due to module 'pycodestyle' has no attribute 'break_around_binary_operator'.
I usually use python pprint
module when debugging.
Would it be nice to add another rule for this case? especially since it's not third-party but standard python.
Let me know if it seems useful, i'd be glad to submit a PR
Hello,
it would be nice to know why someone would want to prevent the use of print()
?
Maybe you can extend the README file to explain it?
Hello,
This is a great work.
Thank you.
I want help to add a flake8-pdb to check pdb sentence similar to print sentence.
I shared my code here:
http://stackoverflow.com/questions/25249586/flag-print-statements-in-python-code#comment39491676_25249854
I want know if our work together to make a standard inherit check for flake8.
Or you prefer make a new project to flake8-pdb, flake8-other-check...
What do you think?
If a specific error exclusion is specified in # noqa per http://flake8.pycqa.org/en/3.0.4/user/ignoring-errors.html#in-line-ignoring-errors, the plugin ignores the entire line. Should only ignore if flake8-print errors (T001-T004) are specified.
I took a quick look at pycodestyle.noqa, and it looks like it doesn't provide support for easily checking for specific codes unfortunately.
When upgrading to latest version of flake8-print (3.0.0) and using flake8 (3.5.0) I get the above error
[anders:~/tmp] [e] 7s $ flake8 -j 1 e/
Traceback (most recent call last):
File "/home/anders/tmp/e/bin/flake8", line 11, in <module>
sys.exit(main())
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/main/application.py", line 396, in run
self._run(argv)
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/main/application.py", line 384, in _run
self.run_checks()
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/main/application.py", line 310, in run_checks
self.file_checker_manager.run()
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/checker.py", line 321, in run
self.run_serial()
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/checker.py", line 305, in run_serial
checker.run_checks()
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/checker.py", line 579, in run_checks
self.run_ast_checks()
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8/checker.py", line 493, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8_print.py", line 88, in run
parser.visit(self.tree)
File "/usr/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/usr/lib/python2.7/ast.py", line 249, in generic_visit
self.visit(item)
File "/usr/lib/python2.7/ast.py", line 241, in visit
return visitor(node)
File "/home/anders/tmp/e/local/lib/python2.7/site-packages/flake8_print.py", line 49, in visit_FunctionDef
if arg.arg in PRINT_FUNCTION_NAMES:
AttributeError: 'Name' object has no attribute 'arg'
Not sure if this is a supported usecase, but it looks like this library breaks when trying to install from a setup.py file.
Looks like a recurrence of #20 with the switch to poetry.
Is there a way to select a directory? I really only care about prints in my package; in scripts, examples, and tests they are valid. I want to keep running the rest of flake8 on the other directories, though. I think I can make this work via pre-commit and splitting the check in two, but is there a better way? Thank you for this useful package!
Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.
Python 2.6 needs explicit argument specifiers.
Could you publish this tool on pypi , so that it is easier to install.
Hey! Is there any plans to support flake8 3+ ?
Thnx
It would be nice to force requiring __future__.print_function
only when a print
is found.
And/Or somehow expose a 'uses print' flag per module, in a way that xZise/flake8-future-import#1 could use the flag.
Thanks for writing this plugin!
I noticed that while setup.py
says the license of the package is MIT, there is no license file in the repository (and thus no license included in the package either).
Hi
I'm not sure what's going on here, but I can't seem to get the plugin to work:
root@9eab8a7ef08d:/code# flake8 --version
c3.5.0 (flake8-blind-except: 0.1.1, flake8-print: 3.0.1, flake8_pep3101: 0.1, import-order: 0.15, mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 2.7.6 on Linux
root@9eab8a7ef08d:/code# cat test.py
import this
print "hello world"
a = b
root@9eab8a7ef08d:/code# flake8 test.py
test.py:1:1: F401 'this' imported but unused
test.py:5:5: F821 undefined name 'b'
Reports a false positive for this multiline string at least:
email_msg = """MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
"""
I have a similar problem as pypa/pip#4792
Warning Using setup_requires is discouraged in favor of PEP-518
https://setuptools.readthedocs.io/en/latest/setuptools.html
Currently it requires the #noqa
on the same line as the print statement
print("foo" # noqa
" bar")
See the issue I opened on flake8, which was promptly closed and redirected here: PyCQA/flake8#1563
Comparing these two lines:
https://github.com/orsinium-labs/flake8-todos/blob/master/setup.py#L39
https://github.com/JBKahn/flake8-print/blob/master/pyproject.toml#L45
We see that flake8-print
and flake8-todos
are both attempting to reserve T00
for their error codes. This prevents anyone from installing both plugins.
Could one of these tools please change their error code prefix? I would open this issue on flake8-todos
, but it appears no one can open issues there.
We've frequently had to catch print
statements in PRs that should use loggers, and this seems like exactly the tool we need! 😃
However, would it be possible to make this available as a pre-commit hook (docs)? We would want to catch print
statements before the commit is created, even if the developer forgets to run flake8
locally.
test.py
file with the following contents:def fail((a, b, c)):
pass
flake8 test.py
(with flake8-print
installed).Expected: flake8 run passes without raising any issues.
Actual: flake8 fails with the following traceback:
(venv) [test_flake8_print] flake8 test.py
Traceback (most recent call last):
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/bin/flake8", line 11, in <module>
sys.exit(main())
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/main/cli.py", line 16, in main
app.run(argv)
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/main/application.py", line 396, in run
self._run(argv)
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/main/application.py", line 384, in _run
self.run_checks()
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/main/application.py", line 310, in run_checks
self.file_checker_manager.run()
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/checker.py", line 319, in run
self.run_parallel()
File "/Users/wojcikstefan/Repos/test_flake8_print/venv/lib/python2.7/site-packages/flake8/checker.py", line 288, in run_parallel
for ret in pool_map:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 668, in next
raise value
AttributeError: 'Tuple' object has no attribute 'id'
I traced the problem back to this line:
Line 50 in e5d3812
When the function definition contains a tuple parameter one of the arg
s is an _ast.Tuple
object, not the expected _ast.Name
. _ast.Tuple
doesn't have the id
attribute, hence arg.id
raises the AttributeError
seen in the traceback.
Note that tuple parameter unpacking has been removed in Python 3 via PEP 3113, so this is only a bug in handling Python 2 files.
This might be related to #40 but violation is different, so I'm making new issue.
Our builds on CI server started to fail just when 3.1.3
was released. Seems like inline # nopep8
comment has no effect anymore and on such line:
...
print(foo) # nopep8
...
results in violation error:
my_project/print_mailer.py:35:9: T001 print found.
It was just bump of patch part of semver so CI server fetched the most recent version and older version was present on my local machine. Took me a while to track down what was causing this, but everything seems to be working fine wit 3.1.1
.
It would be great if this project supported building to wheels as this would speed up installation for users and mean that they didn't need to run setup.py
.
I'd be happy to put together a PR for this at some point if you'd be open to merging it.
Likely I'd base this on one of my existing projects which does build to wheels and introduce a script which wrapped the build and upload steps (with the help of the twine
package).
After updating to 3.1.3
I have new violations.
Let's start with the regular print
usage:
print(1)
Output:
» flake8 ex.py
ex.py
1:1 T001 print found.
print(1)
^
Full list of violations and explanations:
https://wemake-python-stylegui.de/en/0.12.5/pages/usage/violations/
That's ok. Now, let's ignore this violation with a noqa
comment:
print(1) # noqa: T001
Output:
» flake8 ex.py
ex.py
1:1 T002 Python 2.x reserved word print used.
print(1) # noqa: T001
^
Full list of violations and explanations:
https://wemake-python-stylegui.de/en/0.12.5/pages/usage/violations/
That's not what I expect to happen. I expect that after my noqa
comment is created I will receive 0 violations. Moreover, I am not sure how comment can change print
violation code.
This all happed somewhere at this point: wemake-services/wemake-python-styleguide#964
With 3.1.1
everything works as it should.
This is a best practice, and also required for use with https://pypi.org/project/flake8-noqa/ - I think the implemenation is as simple as deleting these lines:
Lines 105 to 108 in 539dd76
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.