xzise / flake8-string-format Goto Github PK
View Code? Open in Web Editor NEWCheck that indexed parameters are used in strings
License: MIT License
Check that indexed parameters are used in strings
License: MIT License
While this isnt pretty, and they might be interested in changing it as it only occurs in a few modules, it is causing false positives.
'''API Function: insert(search_type, data, metadata={}, parent_key=None, info={}, use_id=False, triggers=True)
...
'''
Modules with this syntax (All start with 'API Function: ' or 'Function: ')
https://github.com/Southpaw-TACTIC/TACTIC/blob/master/src/client/tactic_client_lib/tactic_server_stub.py
https://github.com/Southpaw-TACTIC/TACTIC/blob/master/src/pyasm/application/common/application.py
https://github.com/Southpaw-TACTIC/TACTIC/blob/master/src/pyasm/interpreter/tactic_client_lib/tactic_server_stub.py
https://github.com/Southpaw-TACTIC/TACTIC/blob/master/src/pyasm/prod/service/api_xmlrpc.py
I'm not sure if this plugin is intended to check f-string (Python 3.6+).
I personally would like to have a rule that forbids to use f-strings without any variables/expressions/formatting inside, for example:
print(f"Hello world!") # should be forbidden, convert to plain string, remove f prefix
print(f"2 + 2 is {2 + 2}") # ok
Perhaps you might consider this within scope of your plugin:
PyCQA/pycodestyle#424
I thought my code was ok until I ran it and found output like:
{0} branch does not exist locally
Looking at the code, it was obvious:
print('{0} branch does not exist locally')
I forgot to format the string: .format(branch)
Would it be possible to warn about this kind of errors?
link_regex = re.compile(r'\[\[(?P<title>[^\]|[<>{}]*)(\|.*?)?\]\]')
produces
./pywikibot/__init__.py:643:25: P103 other string does contain unindexed parameters
flake8_string_format.py
42: def visit_Str(self, node):
45: def visit_Bytes(self, node):
Generates the below warnings in Python 3.8
test_flake8_string_format.py: 301 tests with warnings
/usr/lib/python3.8/ast.py:360: PendingDeprecationWarning: visit_Str is deprecated; add visit_Constant
return visitor(node)
test_flake8_string_format.py: 144 tests with warnings
/usr/lib/python3.8/ast.py:360: PendingDeprecationWarning: visit_Bytes is deprecated; add visit_Constant
return visitor(node)
Actual docstrings are most likely not a problem for this. Could also add a parameter to enable that too.
Question is how to detect a docstring? It could check for class or function definition and ignore the first Expr?
The MW uses {{…}}
for templates and {{:Bla}}
is a valid name which shouldn't be considered a problem. One way to mitigate (at the moment) would be to use {{ :Bla}}
because Python doesn't allow { }.format(42)
.
Another way would be to ignore constellations where there are an even number of brackets on each side as they aren't parsed but its number gets halved.
I'm getting this in Travis CI on all Python versions. Unfortunately I don't seem to be able to reproduce it locally. Any ideas?
Traceback (most recent call last):
File "/home/travis/virtualenv/python2.7.9/bin/flake8", line 11, in <module>
sys.exit(main())
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flake8/main.py", line 28, in main
flake8_style = get_style_guide(parse_argv=True)
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flake8/engine.py", line 234, in get_style_guide
styleguide = StyleGuide(**kwargs)
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/flake8/engine.py", line 154, in __init__
self._styleguide = kwargs.pop('styleguide', NoQAStyleGuide(**kwargs))
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/pycodestyle.py", line 1833, in __init__
arglist, parse_argv, config_file, parser)
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/pycodestyle.py", line 2120, in process_options
options = read_config(options, args, arglist, parser)
File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/pycodestyle.py", line 2076, in read_config
assert opt_type in ('store_true', 'store_false')
AssertionError
Two cases in TACTIC
src/tactic/ui/panel/custom_layout_wdg.py:611:36: P103 other string does contain unindexed parameters
if not plugin or plugin == '{}':
...
src/pyasm/common/common.py:732:26: P103 other string does contain unindexed parameters
elif char in '''!@$%^&*()={}[]:"|;'\\<>?''':
If a whitelist is added, many string methods such as str.strip
are known to be safe.
P101 format string does contain unindexed parameters
is raised by: '{:%Y/%m/}'.format(datetime.now())
One of the strings that cause error:
'{}.{}.{}-rc.{}'.format(*parts[:3], pre + 1)
Traceback:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "/home/gram/.local/share/dephell/venvs/dephell-nLn6/flake8/lib/python3.7/site-packages/flake8/checker.py", line 669, in _run_checks
return checker.run_checks()
File "/home/gram/.local/share/dephell/venvs/dephell-nLn6/flake8/lib/python3.7/site-packages/flake8/checker.py", line 608, in run_checks
self.run_ast_checks()
File "/home/gram/.local/share/dephell/venvs/dephell-nLn6/flake8/lib/python3.7/site-packages/flake8/checker.py", line 504, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/home/gram/.local/share/dephell/venvs/dephell-nLn6/flake8/lib/python3.7/site-packages/flake8_string_format.py", line 299, in run
assert not has_starargs or isinstance(call.args[-1], ast.Starred)
AssertionError
Expected behavior: plugin should just ignore code lines that cannot be processed.
I'm having false positives of P102 docstring does contain unindexed parameters
because of this line:
{'description': '', 'extension': {}, 'time': ��1N|�r�, 'tid': '\x03\xca1N|\xa9r\x99', 'serial': '\x03\xca1N|\xa9r\x99', 'user_name': '', 'size': 1042}
This line is used in the docstring to give some documentation of the motivation of some code decisions in the module.
This is somewhat similar to #7.
Hi, how to work with such case:
code line from piece with ORM model
document_uri = Column(JSONB, nullable=False, server_default="{}")
produces error: P103 other string does contain unindexed parameters
but it's not recognized by python as parameter in string, it's recognized just as is - {}
I would like to forbid to call any method inside format:
f'{foo()} bzz
This convention we are trying to follow with the team, so if it's ok I can handle it?
don't you think it a good idea or ?
I've been testing this on a few very large repos, and finding the occasional error
In roundup (http://www.roundup-tracker.org/index.html), only the following:
test/test_templating.py:350:0: P103 other string does contain unindexed parameters
test/test_mailgw.py:2826:65: P103 other string does contain unindexed parameters
test/test_mailgw.py:2842:65: P103 other string does contain unindexed parameters
test/test_mailgw.py:2857:65: P103 other string does contain unindexed parameters
http://sourceforge.net/p/roundup/code/ci/default/tree/test/test_templating.py
http://sourceforge.net/p/roundup/code/ci/default/tree/test/test_mailgw.py
Definitely within the realms of reasonable use of noqa, but maybe worth fixing.
I would like to request changing the error code prefix from P
to STRF
.
This plugin is currently conflicting with flake8-exact-pin
which also uses the P
prefix for flake8 error codes.
As of flake8 v3.0, plugins are no longer required to have error codes exactly four characters long and the flake8 documentation recommends using a longer prefix to avoid conflicts with other plugins:
Flake8 requires each entry point to be unique amongst all plugins installed in the users environment. Selecting an entry point that is already used can cause plugins to be deactivated without warning!
Please Note: Your entry point does not need to be exactly 4 characters as of Flake8 3.0. Consider using an entry point with 3 letters followed by 3 numbers (i.e.
ABC123
).
flake8-strings
is already using error prefix STR
which is why I have suggested STRF
.
With python 3.5, when I let flake8
with flake8-string-format
run over this:
a = ['bla']
print("foo{}bar".format(*a))
I get:
Traceback (most recent call last):
File "./.venv-flakes/bin/flake8", line 11, in <module>
sys.exit(main())
File ".../site-packages/flake8/main.py", line 33, in main
report = flake8_style.check_files()
File ".../site-packages/flake8/engine.py", line 181, in check_files
return self._retry_serial(self._styleguide.check_files, paths=paths)
File ".../site-packages/flake8/engine.py", line 172, in _retry_serial
return func(*args, **kwargs)
File ".../site-packages/pep8.py", line 1842, in check_files
runner(path)
File ".../site-packages/flake8/engine.py", line 126, in input_file
return fchecker.check_all(expected=expected, line_offset=line_offset)
File ".../site-packages/pep8.py", line 1574, in check_all
self.check_ast()
File ".../site-packages/pep8.py", line 1521, in check_ast
for lineno, offset, text, check in checker.run():
File ".../site-packages/flake8_string_format.py", line 288, in run
assert isinstance(call.args, ast.Starred) is bool(has_starargs)
AssertionError
It works fine with 3.4.
With 3.5, it seems like call.args
is a list of ast.Starred
objects, and
thus the isinstance()
check fails:
(Pdb) has_starargs
1
(Pdb) isinstance(call.args, ast.Starred)
False
(Pdb) call.args
[<_ast.Starred object at 0x7fb97df31f28>]
matplotlib 1.4.x supposedly supports Python 2.6, however it has quite a few use of {}
.
In /doc/make.py, line 264 is reported as an error, but this plugin doesnt catch the case a few lines nearby on line 261, 269, and 274.
It would be nice to forbid {0}
.format(...), {}
.format(...) and so on calls, str
should be used instead.
If the project is maintained and feature request will be accepted, I would be happy to make pull request.
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.