atugushev / flake8-printf-formatting Goto Github PK
View Code? Open in Web Editor NEWflake8 plugin that forbids printf-style string formatting
License: MIT License
flake8 plugin that forbids printf-style string formatting
License: MIT License
(Partially copied from MichaelKim0407/flake8-use-fstring#22, as this plugin also suffers from the same issue.)
The following code triggers a MOD001 error:
a = 42
b"y=%d" % a
However, using an f-string with a byte string is a syntax error:
>>> bf"y={a}"
File "<stdin>", line 1
bf"y={a}"
^^^^^^^
SyntaxError: invalid syntax
And .format()
is not an option either:
>>> b"y={}".format(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'format'
The only other option is .join()
or string concatenation, which are less elegant IMO.
(I could also do "y={}".format(a).encode("utf-8")
, but that doesn't always work right if there are byte literals in the string.)
Generally I prefer using f-strings or .format()
, but when the string contains {
and/or }
I find it cleaner to just use %
-formatting instead:
"bar_format": "{l_bar}{bar:%d}{r_bar}" % (columns - 42),
The alternatives are much harder to read:
"bar_format": f"{{l_bar}}{{bar:{columns - 42}}}{{r_bar}}"
"bar_format": "{{l_bar}}{{bar:{:d}}}{{r_bar}}".format(columns - 42)
It would be nice to have an option to automatically allow %
-formatting in such cases without having to add # noqa
.
Classes Num
, Str
, Bytes
, NameConstant
, and Ellipsis
are considered deprecated in favor of ast.Constant
since python 3.8.
Consider something like:
isinstance(node.left.value, (bytes, str))
In Python 3.8, the importlib_metadata package was implemented in the standard library as importlib.metadata
(Python docs)
You may want to add this snippet to flake8_printf_formatting.py
and update the install requires in setup.cfg
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata # python_version<'3.8'
I can also submit a PR if that would be of interest
For searchability, this is the error from flake8: flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "MOD" due to No module named 'importlib_metadata'.
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.